1 ################################################################
2 # lilylib.py -- options and stuff
4 # source file of the GNU LilyPond music typesetter
6 # (c) 1998--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 # Jan Nieuwenhuizen <janneke@gnu.org>
9 ### subst:\(^\|[^._a-z]\)\(abspath\|identify\|warranty\|progress\|warning\|error\|exit\|getopt_args\|option_help_str\|options_help_str\|help\|setup_temp\|read_pipe\|system\|cleanup_temp\|strip_extension\|cp_to_dir\|mkdir_p\|init\) *(
12 ### subst: \(help_summary\|keep_temp_dir_p\|option_definitions\|original_dir\|program_name\|pseudo_filter_p\|temp_dir\|verbose_p\)
19 ################################################################
20 # Users of python modules should include this snippet
21 # and customize variables below.
23 # We'll suffer this path init stuff as long as we don't install our
24 # python packages in <prefix>/lib/pythonx.y (and don't kludge around
25 # it as we do with teTeX on Red Hat Linux: set some environment var
26 # (PYTHONPATH) in profile)
28 # If set, LILYPONDPREFIX must take prevalence
29 # if datadir is not set, we're doing a build and LILYPONDPREFIX
30 import getopt, os, sys
31 datadir = '@local_lilypond_datadir@'
32 if not os.path.isdir (datadir):
33 datadir = '@lilypond_datadir@'
34 if os.environ.has_key ('LILYPONDPREFIX') :
35 datadir = os.environ['LILYPONDPREFIX']
36 while datadir[-1] == os.sep:
39 sys.path.insert (0, os.path.join (datadir, 'python'))
42 if __name__ == '__main__':
47 program_name = 'unset'
48 original_dir = os.getcwd ()
49 temp_dir = os.path.join (original_dir, '%s.dir' % program_name)
53 help_summary = _ ("lilylib module")
55 option_definitions = [
56 ('', 'h', 'help', _ ("this help")),
60 ################################################################
62 # Handle bug in Python 1.6-2.1
64 # there are recursion limits for some patterns in Python 1.6 til 2.1.
65 # fix this by importing pre instead. Fix by Mats.
67 # todo: should check Python version first.
75 # Attempt to fix problems with limited stack size set by Python!
76 # Sets unlimited stack size. Note that the resource module only
77 # is available on UNIX.
80 resource.setrlimit (resource.RLIMIT_STACK, (-1, -1))
86 gettext.bindtextdomain ('lilypond', localedir)
87 gettext.textdomain ('lilypond')
95 program_version = '1.6.6'
96 if program_version == '@' + 'TOPLEVEL_VERSION' + '@':
97 program_version = '1.5.54'
100 port.write ('%s (GNU LilyPond) %s\n' % (__main__.program_name, program_version))
104 identify (sys.stdout)
105 sys.stdout.write ('\n')
106 sys.stdout.write (_ ('Copyright (c) %s by' % ' 1998--2002'))
107 sys.stdout.write ('\n')
108 sys.stdout.write (' Han-Wen Nienhuys')
109 sys.stdout.write (' Jan Nieuwenhuizen')
110 sys.stdout.write ('\n\n')
111 sys.stdout.write ('\n')
112 sys.stdout.write (_ ("Distributed under terms of the GNU General Public License. It comes with NO WARRANTY."))
113 sys.stdout.write ('\n')
119 sys.stderr.write (__main__.program_name + ":" + _ ("warning: ") + s + '\n')
122 sys.stderr.write (__main__.program_name + ":" + _ ("error: ") + s + '\n')
125 if __main__.verbose_p:
126 raise _ ('Exiting (%d)...') % i
130 def getopt_args (opts):
131 '''Construct arguments (LONG, SHORT) for getopt from list of options.'''
146 def option_help_str (o):
147 '''Transform one option description (4-tuple ) into neatly formatted string'''
165 return ' ' + sh + sep + long + arg
168 def options_help_str (opts):
169 '''Convert a list of options into a neatly formatted string'''
175 s = option_help_str (o)
176 strs.append ((s, o[3]))
182 str = str + '%s%s%s\n' % (s[0], ' ' * (w - len(s[0]) + 3), s[1])
186 ls = [(_ ("Usage: %s [OPTION]... FILE") % __main__.program_name),
188 (__main__.help_summary),
192 (options_help_str (__main__.option_definitions)),
194 (_ ("Report bugs to %s") % 'bug-lilypond@gnu.org'),
196 map (sys.stdout.write, ls)
200 ''' Create a temporary directory, and return its name. '''
202 if not __main__.keep_temp_dir_p:
203 __main__.temp_dir = tempfile.mktemp (__main__.program_name)
205 os.mkdir (__main__.temp_dir, 0777)
209 return __main__.temp_dir
211 def command_name (cmd):
212 return re.match ('^[ \t]*([^ \t]*)', cmd).group (1)
214 def error_log (name):
215 return os.path.join (__main__.temp_dir, '%s.errorlog' % name)
217 def read_pipe (cmd, mode = 'r'):
219 if __main__.verbose_p:
220 progress (_ ("Opening pipe `%s\'") % cmd)
221 redirect = ' 2>%s' % error_log (command_name (cmd))
222 pipe = os.popen (cmd + redirect, mode)
223 output = pipe.read ()
224 status = pipe.close ()
225 # successful pipe close returns 'None'
228 signal = 0x0f & status
229 exit_status = status >> 8
232 error (_ ("`%s\' failed (%d)") % (cmd, exit_status))
233 if not __main__.verbose_p:
234 error (_ ("The error log is as follows:"))
235 sys.stderr.write (open (error_log (command_name (cmd)).read ()))
237 if __main__.verbose_p:
241 def system (cmd, ignore_error = 0):
243 ''' Run CMD. If IGNORE_ERROR is set, do not complain when CMD
248 Exit status of CMD '''
250 name = command_name (cmd)
253 if __main__.verbose_p:
254 progress (_ ("Invoking `%s\'") % cmd)
255 if __main__.pseudo_filter_p:
256 redirect = ' 1> /dev/null'
258 progress ( _("Running %s...") % name)
259 redirect = ' 1> /dev/null 2>%s' % error_log (name)
261 status = os.system (cmd + redirect)
262 signal = 0x0f & status
263 exit_status = status >> 8
266 msg = _ ("`%s\' failed (%d)") % (name, status / exit_status)
268 if __main__.verbose_p:
269 warning (msg + ' ' + _ ("(ignored)"))
272 if not __main__.verbose_p:
273 error (_ ("The error log is as follows:"))
274 sys.stderr.write (open (error_log (name)).read ())
281 if not __main__.keep_temp_dir_p:
282 if __main__.verbose_p:
283 progress (_ ("Cleaning %s...") % __main__.temp_dir)
284 shutil.rmtree (__main__.temp_dir)
285 if __main__.verbose_p:
289 def strip_extension (f, ext):
290 (p, e) = os.path.splitext (f)
296 def cp_to_dir (pattern, dir):
297 "Copy files matching re PATTERN from cwd to DIR"
298 # Duh. Python style portable: cp *.EXT OUTDIR
299 # system ('cp *.%s %s' % (ext, outdir), 1)
300 files = filter (lambda x, p=pattern: re.match (p, x), os.listdir ('.'))
301 map (lambda x, d=dir: shutil.copy2 (x, os.path.join (d, x)), files)
304 # Python < 1.5.2 compatibility
306 # On most platforms, this is equivalent to
307 #`normpath(join(os.getcwd()), PATH)'. *Added in Python version 1.5.2*
308 if os.path.__dict__.has_key ('abspath'):
309 abspath = os.path.abspath
312 return os.path.normpath (os.path.join (os.getcwd (), path))
314 if os.__dict__.has_key ('makedirs'):
315 makedirs = os.makedirs
317 def makedirs (dir, mode=0777):
318 system ('mkdir -p %s' % dir)
321 def mkdir_p (dir, mode=0777):
322 if not os.path.isdir (dir):