lilypond-1.3.11
[lilypond.git] / bin / mf-deps.py
blobb259b59748cb5e37c2699737e9db038b52f71320
1 #!@PYTHON@
2 # -*- python -*-
4 #
5 # mf-deps.py -- dependencies for metafont
6 #
7 # source file of the GNU LilyPond music typesetter
8 #
9 # (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
13 """look into metafont source for dependencies.
15 fixme: python path
16 fixme: error reporting
17 fixem: python sucks slightly: why doesn't it have closures?
19 """
21 import sys
22 import os
24 lilypath =''
25 try:
26 lilypath = os.environ['LILYPOND_SOURCEDIR'] + '/'
27 except KeyError:
28 try:
29 lilypath = os.environ['top_srcdir'] + '/'
30 except KeyError:
31 print 'Please set LILYPOND_SOURCEDIR to the toplevel source, eg LILYPOND_SOURCEDIR=/home/foobar/lilypond-1.2.3/'
33 sys.exit(1)
35 lilypath = lilypath + '/bin/'
36 sys.path.append(lilypath)
38 from regex import * ;
39 from regsub import * ;
40 import sys;
41 import os;
44 version = "0.2"
45 input_re = compile('^[ \t]*input *\([^;]+\);')
46 postfixes = ['log', 'dvi', '2602gf', 'tfm']
48 def identify ():
49 return "mf-deps.py %s" % version
53 def print_one(x):
54 print x
56 def line_concat(x,y):
57 return x + '\n' + y
59 class Targetdeps:
60 """Dependencies for one toplevel sourcefile
62 """
64 def __init__(self,nm):
65 split = os.path.splitext(nm)
66 self.basename=split[0];
67 self.depfiles = []
68 self.checkdep(nm)
70 def checkdep(self, name):
71 split = os.path.splitext(name)
72 name = split[0] + '.mf';
74 if name not in self.depfiles:
75 self.get_filedeps(name)
77 def get_filedeps(self,filename):
78 # print sys.stderr.write( 'checking ' + filename + '\n');
79 try:
80 file = open(filename)
81 except IOError:
82 # print sys.stderr.write( 'can\'t open ' + filename + '\n')
83 return
85 self.depfiles.append(filename)
86 lines = file.readlines()
87 file.close()
88 for line in lines:
89 if input_re.search (line) <> -1:
90 self.checkdep( input_re.group(1))
92 def target_string(self):
93 # ugh. Closures, anyone?
94 # targets = map (lambda x,y = self.basename: 'out/' + y + '.' + x, postfixes)
95 targets = map (lambda x,y = self.basename: y + '.' + x, postfixes)
96 depstring = reduce(lambda x,y: x + ' ' + y, self.depfiles)
97 dependencies = map (lambda x, y=depstring: x + ': ' + y, targets)
99 return reduce(line_concat, dependencies)
101 print "# Automatically generated by %s\n" % identify ()
103 for file in sys.argv[1:]: # skip programname
104 t = Targetdeps(file)
105 print t.target_string()