1 ###############################################################
2 # lilylib.py -- options and stuff
4 # source file of the GNU LilyPond music typesetter
6 # (c) 1998--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 # Jan Nieuwenhuizen <janneke@gnu.org>
17 ################################################################
18 # Users of python modules should include this snippet
19 # and customize variables below.
21 # We'll suffer this path init stuff as long as we don't install our
22 # python packages in <prefix>/lib/pythonx.y (and don't kludge around
23 # it as we do with teTeX on Red Hat Linux: set some environment var
24 # (PYTHONPATH) in profile)
26 # If set, LILYPOND_DATADIR must take prevalence
27 # if datadir is not set, we're doing a build and LILYPOND_DATADIR
29 datadir
= '@local_lilypond_datadir@'
30 if not os
.path
.isdir (datadir
):
31 datadir
= '@lilypond_datadir@'
32 if os
.environ
.has_key ('LILYPOND_DATADIR') :
33 datadir
= os
.environ
['LILYPOND_DATADIR']
34 while datadir
[-1] == os
.sep
:
37 sys
.path
.insert (0, os
.path
.join (datadir
, 'python'))
40 # Python 2.5 only accepts strings with proper Python internal encoding
41 # (i.e. ASCII or Unicode) when writing to stdout/stderr, so we must
42 # use ugettext iso gettext, and encode the string when writing to
45 localedir
= '@localedir@'
48 t
= gettext
.translation ('lilypond', localedir
)
55 # Urg, Python 2.4 does not define stderr/stdout encoding
56 # Maybe guess encoding from LANG/LC_ALL/LC_CTYPE?
58 def encoded_write(f
, s
):
59 f
.write (s
.encode (f
.encoding
or 'utf_8'))
61 # ugh, Python 2.5 optparse requires Unicode strings in some argument
62 # functions, and refuse them in some other places
63 def display_encode (s
):
64 return s
.encode (sys
.stderr
.encoding
or 'utf_8')
67 encoded_write (sys
.stderr
, s
)
69 progress
= stderr_write
71 # Modified version of the commands.mkarg(x), which always uses
72 # double quotes (since Windows can't handle the single quotes:
82 def command_name (cmd
):
83 # Strip all stuf after command,
84 # deal with "((latex ) >& 1 ) .." too
85 cmd
= re
.match ('([\(\)]*)([^\\\ ]*)', cmd
).group (2)
86 return os
.path
.basename (cmd
)
88 def subprocess_system (cmd
,
95 show_progress
= progress_p
96 name
= command_name (cmd
)
101 progress (_ ("Invoking `%s\'") % cmd
)
103 progress ( _("Running %s...") % name
)
106 stdout_setting
= None
107 if not show_progress
:
108 stdout_setting
= subprocess
.PIPE
110 proc
= subprocess
.Popen (cmd
,
112 universal_newlines
=True,
113 stdout
=stdout_setting
,
114 stderr
=stdout_setting
)
121 log
= proc
.communicate ()
122 retval
= proc
.returncode
126 print >>sys
.stderr
, 'command failed:', cmd
128 print >>sys
.stderr
, "Child was terminated by signal", -retval
130 print >>sys
.stderr
, "Child returned", retval
133 print >>sys
.stderr
, "Error ignored"
135 if not show_progress
:
142 def ossystem_system (cmd
,
149 name
= command_name (cmd
)
152 progress (_ ("Invoking `%s\'") % cmd
)
154 progress ( _("Running %s...") % name
)
156 retval
= os
.system (cmd
)
158 print >>sys
.stderr
, 'command failed:', cmd
160 print >>sys
.stderr
, "Child was terminated by signal", -retval
162 print >>sys
.stderr
, "Child returned", retval
165 print >>sys
.stderr
, "Error ignored"
172 system
= subprocess_system
173 if sys
.platform
== 'mingw32':
175 ## subprocess x-compile doesn't work.
176 system
= ossystem_system
178 def strip_extension (f
, ext
):
179 (p
, e
) = os
.path
.splitext (f
)
185 def search_exe_path (name
):
186 p
= os
.environ
['PATH']
187 exe_paths
= p
.split (':')
189 full
= os
.path
.join (e
, name
)
190 if os
.path
.exists (full
):
195 def print_environment ():
196 for (k
,v
) in os
.environ
.items ():
197 sys
.stderr
.write ("%s=\"%s\"\n" % (k
, v
))
199 class NonDentedHeadingFormatter (optparse
.IndentedHelpFormatter
):
200 def format_heading(self
, heading
):
202 return heading
[0].upper() + heading
[1:] + ':\n'
204 def format_option_strings(self
, option
):
206 if option
._short
_opts
and option
._long
_opts
:
210 if option
.takes_value():
211 metavar
= '=%s' % option
.metavar
or option
.dest
.upper()
213 return "%3s%s %s%s" % (" ".join (option
._short
_opts
),
215 " ".join (option
._long
_opts
),
218 def format_usage(self
, usage
):
219 return _("Usage: %s") % usage
+ '\n'
221 def format_description(self
, description
):
224 def get_option_parser (*args
, **kwargs
):
225 p
= optparse
.OptionParser (*args
, **kwargs
)
226 p
.formatter
= NonDentedHeadingFormatter ()