Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 1d97417 | 1992-12-09 23:14:40 +0000 | [diff] [blame] | 2 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 3 | # Fix Python source files to avoid using |
| 4 | # def method(self, (arg1, ..., argn)): |
Guido van Rossum | 1d97417 | 1992-12-09 23:14:40 +0000 | [diff] [blame] | 5 | # instead of the more rational |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 6 | # def method(self, arg1, ..., argn): |
Guido van Rossum | 1d97417 | 1992-12-09 23:14:40 +0000 | [diff] [blame] | 7 | # |
| 8 | # Command line arguments are files or directories to be processed. |
| 9 | # Directories are searched recursively for files whose name looks |
| 10 | # like a python module. |
| 11 | # Symbolic links are always ignored (except as explicit directory |
| 12 | # arguments). Of course, the original file is kept as a back-up |
| 13 | # (with a "~" attached to its name). |
| 14 | # It complains about binaries (files containing null bytes) |
| 15 | # and about files that are ostensibly not Python files: if the first |
| 16 | # line starts with '#!' and does not contain the string 'python'. |
| 17 | # |
| 18 | # Changes made are reported to stdout in a diff-like format. |
| 19 | # |
| 20 | # Undoubtedly you can do this using find and sed or perl, but this is |
| 21 | # a nice example of Python code that recurses down a directory tree |
| 22 | # and uses regular expressions. Also note several subtleties like |
| 23 | # preserving the file's mode and avoiding to even write a temp file |
| 24 | # when no changes are needed for a file. |
| 25 | # |
| 26 | # NB: by changing only the function fixline() you can turn this |
| 27 | # into a program for a different change to Python programs... |
| 28 | |
| 29 | import sys |
| 30 | import regex |
| 31 | import os |
| 32 | from stat import * |
Guido van Rossum | 1d97417 | 1992-12-09 23:14:40 +0000 | [diff] [blame] | 33 | |
| 34 | err = sys.stderr.write |
| 35 | dbg = err |
| 36 | rep = sys.stdout.write |
| 37 | |
| 38 | def main(): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 39 | bad = 0 |
| 40 | if not sys.argv[1:]: # No arguments |
| 41 | err('usage: ' + sys.argv[0] + ' file-or-directory ...\n') |
| 42 | sys.exit(2) |
| 43 | for arg in sys.argv[1:]: |
| 44 | if os.path.isdir(arg): |
| 45 | if recursedown(arg): bad = 1 |
| 46 | elif os.path.islink(arg): |
| 47 | err(arg + ': will not process symbolic links\n') |
| 48 | bad = 1 |
| 49 | else: |
| 50 | if fix(arg): bad = 1 |
| 51 | sys.exit(bad) |
Guido van Rossum | 1d97417 | 1992-12-09 23:14:40 +0000 | [diff] [blame] | 52 | |
| 53 | ispythonprog = regex.compile('^[a-zA-Z0-9_]+\.py$') |
| 54 | def ispython(name): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 55 | return ispythonprog.match(name) >= 0 |
Guido van Rossum | 1d97417 | 1992-12-09 23:14:40 +0000 | [diff] [blame] | 56 | |
| 57 | def recursedown(dirname): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 58 | dbg('recursedown(' + `dirname` + ')\n') |
| 59 | bad = 0 |
| 60 | try: |
| 61 | names = os.listdir(dirname) |
| 62 | except os.error, msg: |
| 63 | err(dirname + ': cannot list directory: ' + `msg` + '\n') |
| 64 | return 1 |
| 65 | names.sort() |
| 66 | subdirs = [] |
| 67 | for name in names: |
| 68 | if name in (os.curdir, os.pardir): continue |
| 69 | fullname = os.path.join(dirname, name) |
| 70 | if os.path.islink(fullname): pass |
| 71 | elif os.path.isdir(fullname): |
| 72 | subdirs.append(fullname) |
| 73 | elif ispython(name): |
| 74 | if fix(fullname): bad = 1 |
| 75 | for fullname in subdirs: |
| 76 | if recursedown(fullname): bad = 1 |
| 77 | return bad |
Guido van Rossum | 1d97417 | 1992-12-09 23:14:40 +0000 | [diff] [blame] | 78 | |
| 79 | def fix(filename): |
Tim Peters | 79b334b | 2001-01-17 09:13:33 +0000 | [diff] [blame] | 80 | ## dbg('fix(' + `filename` + ')\n') |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 81 | try: |
| 82 | f = open(filename, 'r') |
| 83 | except IOError, msg: |
| 84 | err(filename + ': cannot open: ' + `msg` + '\n') |
| 85 | return 1 |
| 86 | head, tail = os.path.split(filename) |
| 87 | tempname = os.path.join(head, '@' + tail) |
| 88 | g = None |
| 89 | # If we find a match, we rewind the file and start over but |
| 90 | # now copy everything to a temp file. |
| 91 | lineno = 0 |
| 92 | while 1: |
| 93 | line = f.readline() |
| 94 | if not line: break |
| 95 | lineno = lineno + 1 |
| 96 | if g is None and '\0' in line: |
| 97 | # Check for binary files |
| 98 | err(filename + ': contains null bytes; not fixed\n') |
| 99 | f.close() |
| 100 | return 1 |
| 101 | if lineno == 1 and g is None and line[:2] == '#!': |
| 102 | # Check for non-Python scripts |
Walter Dörwald | aaab30e | 2002-09-11 20:36:02 +0000 | [diff] [blame] | 103 | words = line[2:].split() |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 104 | if words and regex.search('[pP]ython', words[0]) < 0: |
| 105 | msg = filename + ': ' + words[0] |
| 106 | msg = msg + ' script; not fixed\n' |
| 107 | err(msg) |
| 108 | f.close() |
| 109 | return 1 |
| 110 | while line[-2:] == '\\\n': |
| 111 | nextline = f.readline() |
| 112 | if not nextline: break |
| 113 | line = line + nextline |
| 114 | lineno = lineno + 1 |
| 115 | newline = fixline(line) |
| 116 | if newline != line: |
| 117 | if g is None: |
| 118 | try: |
| 119 | g = open(tempname, 'w') |
| 120 | except IOError, msg: |
| 121 | f.close() |
| 122 | err(tempname+': cannot create: '+\ |
| 123 | `msg`+'\n') |
| 124 | return 1 |
| 125 | f.seek(0) |
| 126 | lineno = 0 |
| 127 | rep(filename + ':\n') |
| 128 | continue # restart from the beginning |
| 129 | rep(`lineno` + '\n') |
| 130 | rep('< ' + line) |
| 131 | rep('> ' + newline) |
| 132 | if g is not None: |
| 133 | g.write(newline) |
Guido van Rossum | 1d97417 | 1992-12-09 23:14:40 +0000 | [diff] [blame] | 134 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 135 | # End of file |
| 136 | f.close() |
| 137 | if not g: return 0 # No changes |
Guido van Rossum | 1d97417 | 1992-12-09 23:14:40 +0000 | [diff] [blame] | 138 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 139 | # Finishing touch -- move files |
| 140 | |
| 141 | # First copy the file's mode to the temp file |
| 142 | try: |
| 143 | statbuf = os.stat(filename) |
| 144 | os.chmod(tempname, statbuf[ST_MODE] & 07777) |
| 145 | except os.error, msg: |
| 146 | err(tempname + ': warning: chmod failed (' + `msg` + ')\n') |
| 147 | # Then make a backup of the original file as filename~ |
| 148 | try: |
| 149 | os.rename(filename, filename + '~') |
| 150 | except os.error, msg: |
| 151 | err(filename + ': warning: backup failed (' + `msg` + ')\n') |
| 152 | # Now move the temp file to the original file |
| 153 | try: |
| 154 | os.rename(tempname, filename) |
| 155 | except os.error, msg: |
| 156 | err(filename + ': rename failed (' + `msg` + ')\n') |
| 157 | return 1 |
| 158 | # Return succes |
| 159 | return 0 |
Guido van Rossum | 1d97417 | 1992-12-09 23:14:40 +0000 | [diff] [blame] | 160 | |
| 161 | |
| 162 | fixpat = '^[ \t]+def +[a-zA-Z0-9_]+ *( *self *, *\(( *\(.*\) *)\) *) *:' |
| 163 | fixprog = regex.compile(fixpat) |
| 164 | |
| 165 | def fixline(line): |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 166 | if fixprog.match(line) >= 0: |
| 167 | (a, b), (c, d) = fixprog.regs[1:3] |
| 168 | line = line[:a] + line[c:d] + line[b:] |
| 169 | return line |
Guido van Rossum | 1d97417 | 1992-12-09 23:14:40 +0000 | [diff] [blame] | 170 | |
| 171 | |
| 172 | main() |