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>
18 ################################################################
19 # Users of python modules should include this snippet
20 # and customize variables below.
22 # We'll suffer this path init stuff as long as we don't install our
23 # python packages in <prefix>/lib/pythonx.y (and don't kludge around
24 # it as we do with teTeX on Red Hat Linux: set some environment var
25 # (PYTHONPATH) in profile)
27 # If set, LILYPOND_DATADIR must take prevalence
28 # if datadir is not set, we're doing a build and LILYPOND_DATADIR
30 datadir = '@local_lilypond_datadir@'
31 if not os.path.isdir (datadir):
32 datadir = '@lilypond_datadir@'
33 if os.environ.has_key ('LILYPOND_DATADIR') :
34 datadir = os.environ['LILYPOND_DATADIR']
35 while datadir[-1] == os.sep:
38 sys.path.insert (0, os.path.join (datadir, 'python'))
41 # Python 2.5 only accepts strings with proper Python internal encoding
42 # (i.e. ASCII or Unicode) when writing to stdout/stderr, so we must
43 # use ugettext iso gettext, and encode the string when writing to
46 localedir = '@localedir@'
49 t = gettext.translation ('lilypond', localedir)
56 # Urg, Python 2.4 does not define stderr/stdout encoding
57 # Maybe guess encoding from LANG/LC_ALL/LC_CTYPE?
59 def encoded_write(f, s):
60 f.write (s.encode (f.encoding or 'utf_8'))
62 # ugh, Python 2.5 optparse requires Unicode strings in some argument
63 # functions, and refuse them in some other places
64 def display_encode (s):
65 return s.encode (sys.stderr.encoding or 'utf_8')
68 encoded_write (sys.stderr, s)
70 progress = stderr_write
72 # Modified version of the commands.mkarg(x), which always uses
73 # double quotes (since Windows can't handle the single quotes:
83 def command_name (cmd):
84 # Strip all stuf after command,
85 # deal with "((latex ) >& 1 ) .." too
86 cmd = re.match ('([\(\)]*)([^\\\ ]*)', cmd).group (2)
87 return os.path.basename (cmd)
89 def subprocess_system (cmd,
96 show_progress= progress_p
97 name = command_name (cmd)
102 progress (_ ("Invoking `%s\'") % cmd)
104 progress ( _("Running %s...") % name)
107 stdout_setting = None
108 if not show_progress:
109 stdout_setting = subprocess.PIPE
111 proc = subprocess.Popen (cmd,
113 universal_newlines=True,
114 stdout=stdout_setting,
115 stderr=stdout_setting)
122 log = proc.communicate ()
123 retval = proc.returncode
127 print >>sys.stderr, 'command failed:', cmd
129 print >>sys.stderr, "Child was terminated by signal", -retval
131 print >>sys.stderr, "Child returned", retval
134 print >>sys.stderr, "Error ignored"
136 if not show_progress:
143 def ossystem_system (cmd,
150 name = command_name (cmd)
153 progress (_ ("Invoking `%s\'") % cmd)
155 progress ( _("Running %s...") % name)
157 retval = os.system (cmd)
159 print >>sys.stderr, 'command failed:', cmd
161 print >>sys.stderr, "Child was terminated by signal", -retval
163 print >>sys.stderr, "Child returned", retval
166 print >>sys.stderr, "Error ignored"
173 system = subprocess_system
174 if sys.platform == 'mingw32':
176 ## subprocess x-compile doesn't work.
177 system = ossystem_system
179 def strip_extension (f, ext):
180 (p, e) = os.path.splitext (f)
186 def search_exe_path (name):
187 p = os.environ['PATH']
188 exe_paths = string.split (p, ':')
190 full = os.path.join (e, name)
191 if os.path.exists (full):
196 def print_environment ():
197 for (k,v) in os.environ.items ():
198 sys.stderr.write ("%s=\"%s\"\n" % (k, v))
200 class NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
201 def format_heading(self, heading):
203 return heading[0].upper() + heading[1:] + ':\n'
205 def format_option_strings(self, option):
207 if option._short_opts and option._long_opts:
211 if option.takes_value():
212 metavar = '=%s' % option.metavar or option.dest.upper()
214 return "%3s%s %s%s" % (" ".join (option._short_opts),
216 " ".join (option._long_opts),
219 def format_usage(self, usage):
220 return _("Usage: %s") % usage + '\n'
222 def format_description(self, description):
225 def get_option_parser (*args, **kwargs):
226 p = optparse.OptionParser (*args, **kwargs)
227 p.formatter = NonDentedHeadingFormatter ()