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 # Python 2.5 only accepts strings with proper Python internal encoding
38 # (i.e. ASCII or Unicode) when writing to stdout/stderr, so we must
39 # use ugettext iso gettext, and encode the string when writing to
42 localedir = '@localedir@'
45 t = gettext.translation ('lilypond', localedir)
52 # Urg, Python 2.4 does not define stderr/stdout encoding
53 # Maybe guess encoding from LANG/LC_ALL/LC_CTYPE?
55 def encoded_write(f, s):
56 f.write (s.encode (f.encoding or 'utf_8'))
58 # ugh, Python 2.5 optparse requires Unicode strings in some argument
59 # functions, and refuse them in some other places
60 def display_encode (s):
61 return s.encode (sys.stderr.encoding or 'utf_8')
64 encoded_write (sys.stderr, s)
66 progress = stderr_write
68 def require_python_version ():
69 if sys.hexversion < 0x02040000:
70 stderr_write ("Python 2.4 or newer is required to run this program.\n\
71 Please upgrade Python from http://python.org/download/, and if you use MacOS X,\n\
72 please read 'Setup for MacOS X' in Application Usage.")
73 os.system ("open http://python.org/download/")
76 # Modified version of the commands.mkarg(x), which always uses
77 # double quotes (since Windows can't handle the single quotes:
87 def command_name (cmd):
88 # Strip all stuf after command,
89 # deal with "((latex ) >& 1 ) .." too
90 cmd = re.match ('([\(\)]*)([^\\\ ]*)', cmd).group (2)
91 return os.path.basename (cmd)
93 def subprocess_system (cmd,
100 show_progress= progress_p
101 name = command_name (cmd)
106 progress (_ ("Invoking `%s\'") % cmd)
108 progress ( _("Running %s...") % name)
111 stdout_setting = None
112 if not show_progress:
113 stdout_setting = subprocess.PIPE
115 proc = subprocess.Popen (cmd,
117 universal_newlines=True,
118 stdout=stdout_setting,
119 stderr=stdout_setting)
126 log = proc.communicate ()
127 retval = proc.returncode
131 print >>sys.stderr, 'command failed:', cmd
133 print >>sys.stderr, "Child was terminated by signal", -retval
135 print >>sys.stderr, "Child returned", retval
138 print >>sys.stderr, "Error ignored"
140 if not show_progress:
147 def ossystem_system (cmd,
154 name = command_name (cmd)
157 progress (_ ("Invoking `%s\'") % cmd)
159 progress ( _("Running %s...") % name)
161 retval = os.system (cmd)
163 print >>sys.stderr, 'command failed:', cmd
165 print >>sys.stderr, "Child was terminated by signal", -retval
167 print >>sys.stderr, "Child returned", retval
170 print >>sys.stderr, "Error ignored"
177 system = subprocess_system
178 if sys.platform == 'mingw32':
180 ## subprocess x-compile doesn't work.
181 system = ossystem_system
183 def strip_extension (f, ext):
184 (p, e) = os.path.splitext (f)
190 def search_exe_path (name):
191 p = os.environ['PATH']
192 exe_paths = p.split (':')
194 full = os.path.join (e, name)
195 if os.path.exists (full):
200 def print_environment ():
201 for (k,v) in os.environ.items ():
202 sys.stderr.write ("%s=\"%s\"\n" % (k, v))
204 class NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
205 def format_heading(self, heading):
207 return heading[0].upper() + heading[1:] + ':\n'
209 def format_option_strings(self, option):
211 if option._short_opts and option._long_opts:
215 if option.takes_value():
216 metavar = '=%s' % option.metavar or option.dest.upper()
218 return "%3s%s %s%s" % (" ".join (option._short_opts),
220 " ".join (option._long_opts),
223 def format_usage(self, usage):
224 return _("Usage: %s") % usage + '\n'
226 def format_description(self, description):
229 def get_option_parser (*args, **kwargs):
230 p = optparse.OptionParser (*args, **kwargs)
231 p.formatter = NonDentedHeadingFormatter ()