1 # This file is part of LilyPond, the GNU music typesetter.
3 # Copyright (C) 1998--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
4 # Jan Nieuwenhuizen <janneke@gnu.org>
6 # LilyPond is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # LilyPond is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
28 ################################################################
29 # Users of python modules should include this snippet
30 # and customize variables below.
33 # Python 2.5 only accepts strings with proper Python internal encoding
34 # (i.e. ASCII or Unicode) when writing to stdout/stderr, so we must
35 # use ugettext iso gettext, and encode the string when writing to
38 localedir = '@localedir@'
41 t = gettext.translation ('lilypond', localedir)
48 # Urg, Python 2.4 does not define stderr/stdout encoding
49 # Maybe guess encoding from LANG/LC_ALL/LC_CTYPE?
51 def encoded_write(f, s):
53 .decode (sys.stderr.encoding or locale.getdefaultlocale()[1])
54 .encode (f.encoding or 'utf_8'))
56 # ugh, Python 2.5 optparse requires Unicode strings in some argument
57 # functions, and refuse them in some other places
58 def display_encode (s):
59 return s.encode (sys.stderr.encoding or 'utf_8')
62 encoded_write (sys.stderr, s)
64 progress = stderr_write
66 def require_python_version ():
67 if sys.hexversion < 0x02040000:
68 stderr_write ("Python 2.4 or newer is required to run this program.\n\
69 Please upgrade Python from http://python.org/download/, and if you use MacOS X,\n\
70 please read 'Setup for MacOS X' in Application Usage.")
71 os.system ("open http://python.org/download/")
74 # Modified version of the commands.mkarg(x), which always uses
75 # double quotes (since Windows can't handle the single quotes:
85 def command_name (cmd):
86 # Strip all stuf after command,
87 # deal with "((latex ) >& 1 ) .." too
88 cmd = re.match ('([\(\)]*)([^\\\ ]*)', cmd).group (2)
89 return os.path.basename (cmd)
91 def subprocess_system (cmd,
98 show_progress= progress_p
99 name = command_name (cmd)
104 progress (_ ("Invoking `%s\'") % cmd)
106 progress ( _("Running %s...") % name)
109 stdout_setting = None
110 if not show_progress:
111 stdout_setting = subprocess.PIPE
113 proc = subprocess.Popen (cmd,
115 universal_newlines=True,
116 stdout=stdout_setting,
117 stderr=stdout_setting)
124 log = proc.communicate ()
125 retval = proc.returncode
129 print >>sys.stderr, 'command failed:', cmd
131 print >>sys.stderr, "Child was terminated by signal", -retval
133 print >>sys.stderr, "Child returned", retval
136 print >>sys.stderr, "Error ignored"
138 if not show_progress:
145 def ossystem_system (cmd,
152 name = command_name (cmd)
155 progress (_ ("Invoking `%s\'") % cmd)
157 progress ( _("Running %s...") % name)
159 retval = os.system (cmd)
161 print >>sys.stderr, 'command failed:', cmd
163 print >>sys.stderr, "Child was terminated by signal", -retval
165 print >>sys.stderr, "Child returned", retval
168 print >>sys.stderr, "Error ignored"
175 system = subprocess_system
176 if sys.platform == 'mingw32':
178 ## subprocess x-compile doesn't work.
179 system = ossystem_system
181 def strip_extension (f, ext):
182 (p, e) = os.path.splitext (f)
188 def search_exe_path (name):
189 p = os.environ['PATH']
190 exe_paths = p.split (':')
192 full = os.path.join (e, name)
193 if os.path.exists (full):
198 def print_environment ():
199 for (k,v) in os.environ.items ():
200 sys.stderr.write ("%s=\"%s\"\n" % (k, v))
202 class NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
203 def format_heading(self, heading):
205 return heading[0].upper() + heading[1:] + ':\n'
207 def format_option_strings(self, option):
209 if option._short_opts and option._long_opts:
213 if option.takes_value():
214 metavar = '=%s' % option.metavar or option.dest.upper()
216 return "%3s%s %s%s" % (" ".join (option._short_opts),
218 " ".join (option._long_opts),
221 def format_usage(self, usage):
222 return _("Usage: %s") % usage + '\n'
224 def format_description(self, description):
227 def get_option_parser (*args, **kwargs):
228 p = optparse.OptionParser (*args, **kwargs)
229 p.formatter = NonDentedHeadingFormatter ()