Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 061ec39 | 1993-06-05 18:02:52 +0000 | [diff] [blame] | 2 | |
| 3 | # Update a bunch of files according to a script. |
| 4 | # The input file contains lines of the form <filename>:<lineno>:<text>, |
| 5 | # meaning that the given line of the given file is to be replaced |
| 6 | # by the given text. This is useful for performing global substitutions |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 7 | # on grep output: |
Guido van Rossum | 061ec39 | 1993-06-05 18:02:52 +0000 | [diff] [blame] | 8 | |
| 9 | import os |
| 10 | import sys |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 11 | import re |
Guido van Rossum | 061ec39 | 1993-06-05 18:02:52 +0000 | [diff] [blame] | 12 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 13 | pat = '^([^: \t\n]+):([1-9][0-9]*):' |
| 14 | prog = re.compile(pat) |
Guido van Rossum | 061ec39 | 1993-06-05 18:02:52 +0000 | [diff] [blame] | 15 | |
| 16 | class FileObj: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 17 | def __init__(self, filename): |
| 18 | self.filename = filename |
| 19 | self.changed = 0 |
| 20 | try: |
| 21 | self.lines = open(filename, 'r').readlines() |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 22 | except IOError as msg: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 23 | print '*** Can\'t open "%s":' % filename, msg |
| 24 | self.lines = None |
| 25 | return |
| 26 | print 'diffing', self.filename |
Guido van Rossum | 061ec39 | 1993-06-05 18:02:52 +0000 | [diff] [blame] | 27 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 28 | def finish(self): |
| 29 | if not self.changed: |
| 30 | print 'no changes to', self.filename |
| 31 | return |
| 32 | try: |
| 33 | os.rename(self.filename, self.filename + '~') |
| 34 | fp = open(self.filename, 'w') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 35 | except (os.error, IOError) as msg: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 36 | print '*** Can\'t rewrite "%s":' % self.filename, msg |
| 37 | return |
| 38 | print 'writing', self.filename |
| 39 | for line in self.lines: |
| 40 | fp.write(line) |
| 41 | fp.close() |
| 42 | self.changed = 0 |
Guido van Rossum | 061ec39 | 1993-06-05 18:02:52 +0000 | [diff] [blame] | 43 | |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 44 | def process(self, lineno, rest): |
| 45 | if self.lines is None: |
| 46 | print '(not processed): %s:%s:%s' % ( |
| 47 | self.filename, lineno, rest), |
| 48 | return |
| 49 | i = eval(lineno) - 1 |
| 50 | if not 0 <= i < len(self.lines): |
| 51 | print '*** Line number out of range: %s:%s:%s' % ( |
| 52 | self.filename, lineno, rest), |
| 53 | return |
| 54 | if self.lines[i] == rest: |
| 55 | print '(no change): %s:%s:%s' % ( |
| 56 | self.filename, lineno, rest), |
| 57 | return |
| 58 | if not self.changed: |
| 59 | self.changed = 1 |
| 60 | print '%sc%s' % (lineno, lineno) |
| 61 | print '<', self.lines[i], |
| 62 | print '---' |
| 63 | self.lines[i] = rest |
| 64 | print '>', self.lines[i], |
Guido van Rossum | 061ec39 | 1993-06-05 18:02:52 +0000 | [diff] [blame] | 65 | |
| 66 | def main(): |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 67 | if sys.argv[1:]: |
| 68 | try: |
| 69 | fp = open(sys.argv[1], 'r') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 70 | except IOError as msg: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 71 | print 'Can\'t open "%s":' % sys.argv[1], msg |
| 72 | sys.exit(1) |
| 73 | else: |
| 74 | fp = sys.stdin |
| 75 | curfile = None |
| 76 | while 1: |
| 77 | line = fp.readline() |
| 78 | if not line: |
| 79 | if curfile: curfile.finish() |
| 80 | break |
| 81 | n = prog.match(line) |
| 82 | if n < 0: |
| 83 | print 'Funny line:', line, |
| 84 | continue |
| 85 | filename, lineno = prog.group(1, 2) |
Neal Norwitz | 3bd844e | 2006-08-29 04:39:12 +0000 | [diff] [blame] | 86 | if not curfile or filename != curfile.filename: |
Tim Peters | e6ddc8b | 2004-07-18 05:56:09 +0000 | [diff] [blame] | 87 | if curfile: curfile.finish() |
| 88 | curfile = FileObj(filename) |
| 89 | curfile.process(lineno, line[n:]) |
Guido van Rossum | 061ec39 | 1993-06-05 18:02:52 +0000 | [diff] [blame] | 90 | |
Johannes Gijsbers | 7a8c43e | 2004-09-11 16:34:35 +0000 | [diff] [blame] | 91 | if __name__ == "__main__": |
| 92 | main() |