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