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