lilypond-1.3.130
[lilypond.git] / bin / lilypython.py
blob210865bacb0ee23616c1f20c87bfbfa147babd7b
1 #!/usr/bin/python
3 #
4 # lily-python.py -- implement general LilyPond-wide python stuff
5 #
6 # source file of the GNU LilyPond music typesetter
7 #
8 # (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
9 #
11 import posix
12 import pwd
13 import regex
14 import regsub
15 from string import *
16 from flower import *
17 import sys
18 import os
19 import getopt
22 version_re = regex.compile('\\version *\"\(.*\)\"')
23 # now used as shell script in configure too!
24 # make_assign_re = regex.compile('^\([A-Z_]*\) *= *\(.*\)$')
25 make_assign_re = regex.compile('^\([A-Z_]*\)=\(.*\)$')
27 def mailaddress():
28 try:
29 return os.environ['MAILADDRESS']
30 except KeyError:
31 return '(address unknown)'
33 def version_tuple(file):
34 lines = file.readlines()
36 mi = pa = mj = 0
37 mp = ''
39 for l in lines:
40 if make_assign_re.search(l) <> -1:
41 nm = make_assign_re.group(1)
42 val = make_assign_re.group(2)
43 # if nm == 'TOPLEVEL_MAJOR_VERSION':
44 if nm == 'MAJOR_VERSION':
45 mj = atoi(val)
46 # elif nm == 'TOPLEVEL_MINOR_VERSION':
47 elif nm == 'MINOR_VERSION':
48 mi = atoi(val)
49 # elif nm == 'TOPLEVEL_PATCH_LEVEL':
50 elif nm == 'PATCH_LEVEL':
51 pa = atoi(val)
52 # elif nm == 'TOPLEVEL_MY_PATCH_LEVEL':
53 elif nm == 'MY_PATCH_LEVEL':
54 mp = val
55 return (mj,mi,pa,mp)
57 def next_version(tup):
58 return (tup[0], tup[1], tup[2] + 1, tup[3]);
60 def prev_version(tup):
61 t = tup
62 if t[3]:
63 t3name = regsub.sub('[0-9]*$', '', t[3])
64 t3level = atoi(regsub.sub('[^0-9]*', '', t[3]))
65 if t3level and t3level - 1 > 0:
66 return (tup[0], tup[1], tup[2], t3name + `t3level - 1`);
67 else:
68 return (tup[0], tup[1], tup[2], '');
69 elif t[2] == 0 :
70 return (tup[0], tup[1] -1, tup[2], '');
71 else:
72 return (tup[0], tup[1], tup[2] - 1, '');
74 def dirname(v):
75 return 'lilypond-' + version_tuple_to_str(v)
77 def tarball(v):
78 return dirname(v) + '.tar.gz'
80 def released_tarball(v):
81 return lilydirs.release_dir + tarball(v)
83 def tuple_to_list(tup):
84 l=[]
85 for x in tup:
86 l.append[x]
87 return l
89 def version_str_to_tuple(str):
90 t = split(str, '.')
91 try:
92 mypatch = t[3]
93 except IndexError:
94 mypatch = ''
96 return (atoi(t[0]), atoi(t[1]), atoi(t[2]), mypatch)
98 def guess_mudela_version(filename):
99 f = open (filename)
100 lines = f.readlines()
101 f.close()
102 for l in lines:
103 if version_re.search(l) <> -1:
104 return version_re.group(1)
106 return ''
108 def version_tuple_to_str(tup):
109 mypatch =''
110 if tup[3]:
111 mypatch = '.' + tup[3]
113 return ('%d.%d.%d' % tup[0:3]) + mypatch
115 class Lilydirs:
116 def __init__(self):
117 try:
118 self.topdir = os.environ['LILYPOND_SOURCEDIR'] + '/'
120 except KeyError:
121 print 'Please set LILYPOND_SOURCEDIR to the toplevel source, eg LILYPOND_SOURCEDIR=/home/foobar/lilypond-1.2.3/'
122 sys.exit(1)
124 try:
125 self.groupdir = os.environ['LILYPOND_GROUPDIR'] + '/'
126 except KeyError:
127 self.groupdir = self.topdir + '../'
129 self.release_dir = self.groupdir + '/releases/'
130 self.patch_dir = self.groupdir + '/patches/'
132 def version_tuple(self):
133 f = open (self.topdir + 'VERSION')
134 v = version_tuple(f)
135 f.close ()
136 return v
140 lilydirs = Lilydirs()
142 if __name__ == '__main__':
143 v= lilydirs.version_tuple()
144 print v, prev_version(v), next_version(v)
145 mv = guess_mudela_version(lilydirs.topdir + 'init/symbol.ly')
146 pv=(0,1,1,'jcn4')
147 print version_tuple_to_str(pv), prev_version(pv), next_version(pv)
148 print version_tuple_to_str((0,1,1,''))
149 print mv, version_str_to_tuple(mv)
153 def dump_file(f, s):
154 i = open(f, 'w')
155 i.write(s)
156 i.close ()
158 def gulp_file(f):
159 i = open(f)
160 i.seek (0, 2)
161 len = i.tell ()
162 i.seek (0,0)
163 return i.read (len)
166 header_regex = regex.compile('\\header[ \t\n]*{\([^}]*\)}')
167 header_entry_regex = regex.compile('[\n\t ]*\([^\n\t ]+\)[\n\t ]*=[\n \t]*\([^;]+\)[\n \t]*;')
170 # FIXME breaks on multiple strings.
172 def read_mudela_header (fn):
173 s = gulp_file(fn)
174 s = regsub.gsub('%.*$', '', s)
175 s = regsub.gsub('\n', ' ', s)
177 dict = {}
178 if header_regex.search(s) <> -1:
179 h = header_regex.group(1)
180 else:
181 return dict
183 while regex.search('=', h) <> -1:
185 if header_entry_regex.search (h) == -1:
187 raise 'format error'
189 h = regsub.sub(header_entry_regex, '', h)
190 left = header_entry_regex.group(1)
191 right = header_entry_regex.group(2)
193 right = regsub.gsub('\([^\\]\)\"', '\\1', right)
194 right = regsub.gsub('^"', '', right)
195 left = regsub.gsub('\([^\\]\)\"', '', left)
196 left = regsub.gsub('^"', '', left)
198 dict[left] = right
200 return dict