]> git.donarmstrong.com Git - lilypond.git/blob - scripts/update-lily.py
patch::: 1.3.144.jcn3
[lilypond.git] / scripts / update-lily.py
1 #!@PYTHON@
2 #
3 # update-lily.py -- lilypond autobuilder
4
5 # source file of the GNU LilyPond music typesetter
6 #
7 # download and rebuild latest lilypond or from specified url
8
9
10 '''
11 TODO:
12     * more flexible build/ftp/patches/releases paths
13     * flexible build command
14     * show only?
15 '''
16
17 import os
18 import fnmatch
19 import stat
20 import string
21 import re
22 import getopt
23 import sys
24 import __main__
25 import operator
26 import tempfile
27
28 try:
29         import gettext
30         gettext.bindtextdomain ('lilypond', '@localedir@')
31         gettext.textdomain('lilypond')
32         _ = gettext.gettext
33 except:
34         def _ (s):
35                 return s
36
37 sys.path.append ('@datadir@/python')
38 import gettext
39 gettext.bindtextdomain ('lilypond', '@localedir@')
40 gettext.textdomain('lilypond')
41 _ = gettext.gettext
42
43
44 program_name = 'build-lily'
45 package_name = 'lilypond'
46 help_summary = _("Fetch and rebuild from latest source package")
47 obuild_command = '(./configure --prefix=$HOME/usr && make all web) >> log.txt 2>&1'
48 build_command = '''
49 cd $HOME/usr/src &&
50 [ -d %n-%v ] && exit 1;
51 (
52 tar xzf %t &&
53 rm -f building &&
54 ln -s %n-%v building &&
55 cd %n-%v &&
56 ./configure --prefix=$HOME/usr && make all web
57 ) >> log.txt 2>&1 &&
58 rm -f %n &&
59 ln -s %n%-%v %n
60 '''
61
62 release_dir = build_root + '/releases'
63 patch_dir = build_root + '/patches'
64
65 url = 'file:/home/ftp/pub/gnu/LilyPond/development/lilypond-*.tar.gz'
66 url = 'ftp://appel.lilypond.org/pub/gnu/LilyPond/development/lilypond-*.tar.gz'
67 url = 'ftp://ftp.cs.uu.nl/pub/GNU/LilyPond/development/lilypond-*.tar.gz'
68
69 remove_previous_p = 0
70
71
72 # lily_py.py -- options and stuff
73
74 # source file of the GNU LilyPond music typesetter
75
76 # BEGIN Library for these?
77 # cut-n-paste from ly2dvi
78
79 program_version = '@TOPLEVEL_VERSION@'
80 if program_version == '@' + 'TOPLEVEL_VERSION' + '@':
81         program_version = '1.3.142'
82
83
84 original_dir = os.getcwd ()
85 temp_dir = '%s.dir' % program_name
86 keep_temp_dir_p = 0
87 verbose_p = 0
88
89 def identify ():
90         sys.stdout.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
91
92 def warranty ():
93         identify ()
94         sys.stdout.write ('\n')
95         sys.stdout.write (_ ('Copyright (c) %s by' % ' 2001'))
96         sys.stdout.write ('\n')
97         sys.stdout.write ('  Han-Wen Nienhuys')
98         sys.stdout.write ('  Jan Nieuwenhuizen')
99         sys.stdout.write ('\n')
100         sys.stdout.write (_ (r'''
101 Distributed under terms of the GNU General Public License. It comes with
102 NO WARRANTY.'''))
103         sys.stdout.write ('\n')
104
105 def progress (s):
106         sys.stderr.write (s + '\n')
107
108 def warning (s):
109         sys.stderr.write (_ ("warning: ") + s)
110         sys.stderr.write ('\n')
111         
112                 
113 def error (s):
114         sys.stderr.write (_ ("error: ") + s)
115         sys.stderr.write ('\n')
116         raise _ ("Exiting ... ")
117
118 def getopt_args (opts):
119         '''Construct arguments (LONG, SHORT) for getopt from  list of options.'''
120         short = ''
121         long = []
122         for o in opts:
123                 if o[1]:
124                         short = short + o[1]
125                         if o[0]:
126                                 short = short + ':'
127                 if o[2]:
128                         l = o[2]
129                         if o[0]:
130                                 l = l + '='
131                         long.append (l)
132         return (short, long)
133
134 def option_help_str (o):
135         '''Transform one option description (4-tuple ) into neatly formatted string'''
136         sh = '  '       
137         if o[1]:
138                 sh = '-%s' % o[1]
139
140         sep = ' '
141         if o[1] and o[2]:
142                 sep = ','
143                 
144         long = ''
145         if o[2]:
146                 long= '--%s' % o[2]
147
148         arg = ''
149         if o[0]:
150                 if o[2]:
151                         arg = '='
152                 arg = arg + o[0]
153         return '  ' + sh + sep + long + arg
154
155
156 def options_help_str (opts):
157         '''Convert a list of options into a neatly formatted string'''
158         w = 0
159         strs =[]
160         helps = []
161
162         for o in opts:
163                 s = option_help_str (o)
164                 strs.append ((s, o[3]))
165                 if len (s) > w:
166                         w = len (s)
167
168         str = ''
169         for s in strs:
170                 str = str + '%s%s%s\n' % (s[0], ' ' * (w - len(s[0])  + 3), s[1])
171         return str
172
173 def help ():
174         sys.stdout.write (_ ("Usage: %s [OPTION]... FILE") % program_name)
175         sys.stdout.write ('\n\n')
176         sys.stdout.write (help_summary)
177         sys.stdout.write ('\n\n')
178         sys.stdout.write (_ ("Options:"))
179         sys.stdout.write ('\n')
180         sys.stdout.write (options_help_str (option_definitions))
181         sys.stdout.write ('\n\n')
182         sys.stdout.write (_ ("Report bugs to %s") % 'bug-gnu-music@gnu.org')
183         sys.stdout.write ('\n')
184         sys.exit (0)
185
186
187 def setup_temp ():
188         global temp_dir
189         if not keep_temp_dir_p:
190                 temp_dir = tempfile.mktemp (program_name)
191         try:
192                 os.mkdir (temp_dir, 0777)
193         except OSError:
194                 pass
195                 
196         
197 def system (cmd, ignore_error = 0):
198         if verbose_p:
199                 progress (_ ("Invoking `%s\'") % cmd)
200         st = os.system (cmd)
201         if st:
202                 msg =  ( _ ("error: ") + _ ("command exited with value %d") % st)
203                 if ignore_error:
204                         sys.stderr.write (msg + ' ' + _ ("(ignored)") + ' ')
205                 else:
206                         error (msg)
207
208         return st
209
210
211 def cleanup_temp ():
212         if not keep_temp_dir_p:
213                 if verbose_p:
214                         progress (_ ("Cleaning %s...") % temp_dir)
215                 system ('rm -rf %s' % temp_dir)
216
217
218 def set_setting (dict, key, val):
219         try:
220                 val = string.atof (val)
221         except ValueError:
222                 #warning (_ ("invalid value: %s") % `val`)
223                 pass
224
225         try:
226                 dict[key].append (val)
227         except KeyError:
228                 warning (_ ("no such setting: %s") % `key`)
229                 dict[key] = [val]
230
231 # END Library
232
233 option_definitions = [
234         ('DIR', 'b', 'build-root', _ ("unpack and build in DIR [%s]") % build_root),
235         ('COMMAND', 'c', 'command', _ ("execute COMMAND, subtitute:") \
236          + '\n                            ' + _ ("%%n: package name") \
237          + '\n                            ' + _ ("%%v: package version") \
238          + '\n                            ' + _ ("%%t: tarball") \
239          ),
240         ('', 'h', 'help', _ ("this help")),
241         ('', 'k', 'keep', _ ("keep all output, and name the directory %s") % temp_dir),
242         ('', 'r', 'remove-previous', _ ("remove previous build")),
243         ('', 'V', 'verbose', _ ("verbose")),
244         ('', 'v', 'version', _ ("print version number")),
245         ('URL', 'u', 'url', _ ("fetch and build URL [%s]") % url),
246         ('', 'w', 'warranty', _ ("show warranty and copyright")),
247         ]
248
249 def list_file (user, passwd, host, dir, file):
250         match = []
251         for i in os.listdir (dir):
252                 if fnmatch.fnmatch (i, file):
253                         match.append (i)
254         return match
255
256 list_ = list_file
257
258 #
259 # ugh: use ftp module.
260 #
261 def list_ftp (user, passwd, host, dir, file):
262         if user == 'None':
263                 user = 'anonymous'
264         if passwd == 'None':
265                 passwd = program_name
266
267         command = '''
268 open -u%s,%s -p21 %s
269 set passive-mode off
270 cd "%s"
271 ls -1 "%s"
272 ''' % (user, passwd, host, dir, file)
273         temp = tempfile.mktemp (program_name)
274         f = open (temp, 'w')
275         f.write (command)
276         f.close ()
277         p = os.popen ('lftp -f %s' % temp, 'r')
278         s = p.read ()
279         status = p.close ()
280         return string.split (s[:-1], '\n')
281         
282 def split_url (url):
283         m = re.match ('([^:/]*)(:)?(/*([^:]*):)?(/*([^@]*)@)?(//([^/]*))?(.*)/(.*)',
284                       url)
285         if not m:
286                 error ("can't parse url: %s " % url)
287         return (m.group (1), m.group (4), m.group (6), m.group (8),
288                 m.group (9), m.group (10))
289         
290 def list_url (url):
291         s = "list_%s ('%s', '%s', '%s', '%s', '%s')" % split_url (url)
292         return eval (s)
293
294 def version_tuple_to_str (t):
295         if t[3]:
296                 my = '.%s%d' % (t[3], t[4])
297         else:
298                 my = ''
299         return ('%d.%d.%d' % t[0:3]) + my
300
301 def version_str_to_tuple (s):
302         t = string.split (s, '.')
303         if len (t) >= 4:
304                 my_name = t[3][:-1]
305                 my_number = string.atoi (t[3][-1])
306         else:
307                 my_name = None
308                 my_number = None
309         return (string.atoi (t[0]), string.atoi (t[1]), string.atoi (t[2]),
310                 my_name, my_number)
311
312 def split_package (p):
313         m = re.match ('(.*)-([0-9]*.*).tar.gz', p)
314         return (m.group (1), version_str_to_tuple (m.group (2)))
315
316 def join_package (t):
317         return t[0] + '-' + version_tuple_to_str (t[1])
318
319 def copy_file (user, passwd, host, dir, file):
320         os.system ('cp %s/%s .' % (dir, file))
321
322 copy_ = copy_file
323
324 def copy_ftp (user, passwd, host, dir, file):
325         if user == 'None':
326                 user = 'anonymous'
327         if passwd == 'None':
328                 passwd = program_name
329
330         command = '''
331 open -u%s,%s -p21 %s
332 set passive-mode off
333 cd "%s"
334 get "%s"
335 ''' % (user, passwd, host, dir, file)
336         temp = tempfile.mktemp (program_name)
337         f = open (temp, 'w')
338         f.write (command)
339         f.close ()
340         p = os.popen ('lftp -f %s' % temp, 'r')
341         s = p.read ()
342         status = p.close ()
343         
344 def copy_url (url, dir):
345         os.chdir (dir)
346         s = "copy_%s ('%s', '%s', '%s', '%s', '%s')" % split_url (url)
347         eval (s)
348
349
350 def find_latest (url):
351         progress (_ ("listing %s...") % url)
352         list = map (split_package, list_url (url))
353         list.sort ()
354         return join_package (list[-1])
355
356 def build (p):
357         tar_ball = p + '.tar.gz'
358         (tar_name, tar_version) = split_package (tar_ball)
359         
360         expand = {
361                 '%n' : tar_name,
362                 '%t' : tar_ball,
363                 '%v' : version_tuple_to_str (tar_version),
364                 }
365
366         c = build_command
367         for i in expand.keys ():
368                 c = re.sub (i, expand[i], c)
369         return system (c)
370
371 (sh, long) = getopt_args (__main__.option_definitions)
372 try:
373         (options, files) = getopt.getopt (sys.argv[1:], sh, long)
374 except:
375         help ()
376         sys.exit (2)
377         
378 for opt in options:     
379         o = opt[0]
380         a = opt[1]
381
382         if 0:
383                 pass
384         elif o == '--help' or o == '-h':
385                 help ()
386         elif o == '--buid-root' or o == '-b':
387                 build_root = a
388         elif o == '--command' or o == '-c':
389                 build_command = a
390         elif o == '--remove-previous' or o == '-r':
391                 remove_previous_p = 1
392         elif o == '--url' or o == '-u':
393                 url = a
394         elif o == '--verbose' or o == '-V':
395                 verbose_p = 1
396         elif o == '--version' or o == '-v':
397                 identify ()
398                 sys.exit (0)
399         elif o == '--warranty' or o == '-w':
400                 warranty ()
401                 sys.exit (0)
402                 
403 if 1:
404         latest = find_latest (url)
405
406         if os.path.isdir ('%s/%s' % (build_root, latest)):
407                 progress (_ ("latest is %s") % latest)
408                 progress (_ ("relax, %s is up to date" % package_name))
409                 sys.exit (0)
410
411         get_base = url[:string.rindex (url, '/')] + '/'
412         if os.path.isdir (patch_dir):
413                 os.chdir (patch_dir)
414                 if not os.path.isfile (latest + '.diff.gz'):
415                         get = get_base + latest + '.diff.gz'
416                         progress (_ ("fetching %s...") % get)
417                         copy_url (get, '.')
418
419         if not os.path.isdir (build_root):
420                 build_root = temp_dir
421         if not os.path.isdir (release_dir):
422                 release_dir = temp_dir
423                 setup_temp ()
424                 
425         os.chdir (release_dir)
426         if not os.path.isfile (latest + '.tar.gz'):
427                 get = get_base + latest + '.tar.gz'
428                 progress (_ ("fetching %s...") % get)
429                 copy_url (get, '.')
430
431         if os.path.isdir (os.path.join (build_command, package_name)):
432                 os.chdir (os.path.join (build_command, package_name))
433                 previous = os.getcwd ()
434         else:
435                 previous = 0
436
437         progress (_ ("building %s...") % get)
438         os.chdir (build_root)
439         if not build (latest) and previous and remove_previous_p:
440                 system ('rm -rf %s' % os.path.join (build_root, previous))
441                 
442         os.chdir (original_dir)
443         if release_dir != temp_dir:
444                 cleanup_temp ()
445         sys.exit (0)
446