]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/pytt.py
Merge commit 'origin/dev/jneeman' into systems-per-page
[lilypond.git] / scripts / build / pytt.py
1 #! /usr/bin/python
2
3 '''
4     Copyright (c) 2008--2009
5     Jan Nieuwenhuizen <janneke@gnu.org>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2, or (at your option)
10     any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 '''
21
22 import os
23 import re
24 import stat
25 import sys
26
27 dry_run = False
28
29 def pytt (from_re, to, file_name):
30     s = file (file_name).read ()
31     name = os.path.basename (file_name)
32     base, ext = os.path.splitext (name)
33     t = re.sub (from_re, to % locals (), s)
34     if s != t:
35         if dry_run:
36             sys.stdout.write (t)
37         else:
38             stat_info = os.stat (file_name)
39             mode = stat.S_IMODE (stat_info[stat.ST_MODE])
40             os.system ('mv --backup=t %(file_name)s %(file_name)s~' % locals ())
41             file (file_name, 'w').write (t)
42             os.chmod (file_name, mode)
43
44 def main ():
45     from_re = re.compile (sys.argv[1], re.MULTILINE)
46     to = sys.argv[2]
47     if not sys.argv[3:] or sys.argv[3] == '-':
48         sys.stdout.write (re.sub (from_re, to, sys.stdin.read ()))
49     else:
50         for f in sys.argv[3:]:
51             pytt (from_re, to, f)
52     
53 if __name__ == '__main__':
54     main ()