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
13 pat
= '^([^: \t\n]+):([1-9][0-9]*):'
14 prog
= re
.compile(pat
)
17 def __init__(self
, filename
):
18 self
.filename
= filename
21 self
.lines
= open(filename
, 'r').readlines()
23 print '*** Can\'t open "%s":' % filename
, msg
26 print 'diffing', self
.filename
30 print 'no changes to', self
.filename
33 os
.rename(self
.filename
, self
.filename
+ '~')
34 fp
= open(self
.filename
, 'w')
35 except (os
.error
, IOError), msg
:
36 print '*** Can\'t rewrite "%s":' % self
.filename
, msg
38 print 'writing', self
.filename
39 for line
in self
.lines
:
44 def process(self
, lineno
, rest
):
45 if self
.lines
is None:
46 print '(not processed): %s:%s:%s' % (
47 self
.filename
, lineno
, rest
),
50 if not 0 <= i
< len(self
.lines
):
51 print '*** Line number out of range: %s:%s:%s' % (
52 self
.filename
, lineno
, rest
),
54 if self
.lines
[i
] == rest
:
55 print '(no change): %s:%s:%s' % (
56 self
.filename
, lineno
, rest
),
60 print '%sc%s' % (lineno
, lineno
)
61 print '<', self
.lines
[i
],
64 print '>', self
.lines
[i
],
69 fp
= open(sys
.argv
[1], 'r')
71 print 'Can\'t open "%s":' % sys
.argv
[1], msg
79 if curfile
: curfile
.finish()
83 print 'Funny line:', line
,
85 filename
, lineno
= prog
.group(1, 2)
86 if not curfile
or filename
<> curfile
.filename
:
87 if curfile
: curfile
.finish()
88 curfile
= FileObj(filename
)
89 curfile
.process(lineno
, line
[n
:])
91 if __name__
== "__main__":