1 ###############################################################
2 # lilylib.py -- options and stuff
4 # source file of the GNU LilyPond music typesetter
6 # (c) 1998--2008 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.
22 # Python 2.5 only accepts strings with proper Python internal encoding
23 # (i.e. ASCII or Unicode) when writing to stdout/stderr, so we must
24 # use ugettext iso gettext, and encode the string when writing to
27 localedir = '@localedir@'
30 t = gettext.translation ('lilypond', localedir)
37 # Urg, Python 2.4 does not define stderr/stdout encoding
38 # Maybe guess encoding from LANG/LC_ALL/LC_CTYPE?
40 def encoded_write(f, s):
41 f.write (s.encode (f.encoding or 'utf_8'))
43 # ugh, Python 2.5 optparse requires Unicode strings in some argument
44 # functions, and refuse them in some other places
45 def display_encode (s):
46 return s.encode (sys.stderr.encoding or 'utf_8')
49 encoded_write (sys.stderr, s)
51 progress = stderr_write
53 def require_python_version ():
54 if sys.hexversion < 0x02040000:
55 stderr_write ("Python 2.4 or newer is required to run this program.\n\
56 Please upgrade Python from http://python.org/download/, and if you use MacOS X,\n\
57 please read 'Setup for MacOS X' in Application Usage.")
58 os.system ("open http://python.org/download/")
61 # Modified version of the commands.mkarg(x), which always uses
62 # double quotes (since Windows can't handle the single quotes:
72 def command_name (cmd):
73 # Strip all stuf after command,
74 # deal with "((latex ) >& 1 ) .." too
75 cmd = re.match ('([\(\)]*)([^\\\ ]*)', cmd).group (2)
76 return os.path.basename (cmd)
78 def subprocess_system (cmd,
85 show_progress= progress_p
86 name = command_name (cmd)
91 progress (_ ("Invoking `%s\'") % cmd)
93 progress ( _("Running %s...") % name)
98 stdout_setting = subprocess.PIPE
100 proc = subprocess.Popen (cmd,
102 universal_newlines=True,
103 stdout=stdout_setting,
104 stderr=stdout_setting)
111 log = proc.communicate ()
112 retval = proc.returncode
116 print >>sys.stderr, 'command failed:', cmd
118 print >>sys.stderr, "Child was terminated by signal", -retval
120 print >>sys.stderr, "Child returned", retval
123 print >>sys.stderr, "Error ignored"
125 if not show_progress:
132 def ossystem_system (cmd,
139 name = command_name (cmd)
142 progress (_ ("Invoking `%s\'") % cmd)
144 progress ( _("Running %s...") % name)
146 retval = os.system (cmd)
148 print >>sys.stderr, 'command failed:', cmd
150 print >>sys.stderr, "Child was terminated by signal", -retval
152 print >>sys.stderr, "Child returned", retval
155 print >>sys.stderr, "Error ignored"
162 system = subprocess_system
163 if sys.platform == 'mingw32':
165 ## subprocess x-compile doesn't work.
166 system = ossystem_system
168 def strip_extension (f, ext):
169 (p, e) = os.path.splitext (f)
175 def search_exe_path (name):
176 p = os.environ['PATH']
177 exe_paths = p.split (':')
179 full = os.path.join (e, name)
180 if os.path.exists (full):
185 def print_environment ():
186 for (k,v) in os.environ.items ():
187 sys.stderr.write ("%s=\"%s\"\n" % (k, v))
189 class NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
190 def format_heading(self, heading):
192 return heading[0].upper() + heading[1:] + ':\n'
194 def format_option_strings(self, option):
196 if option._short_opts and option._long_opts:
200 if option.takes_value():
201 metavar = '=%s' % option.metavar or option.dest.upper()
203 return "%3s%s %s%s" % (" ".join (option._short_opts),
205 " ".join (option._long_opts),
208 def format_usage(self, usage):
209 return _("Usage: %s") % usage + '\n'
211 def format_description(self, description):
214 def get_option_parser (*args, **kwargs):
215 p = optparse.OptionParser (*args, **kwargs)
216 p.formatter = NonDentedHeadingFormatter ()