1 # This file is part of LilyPond, the GNU music typesetter.
3 # Copyright (C) 1998--2011 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/>.
27 ################################################################
28 # Users of python modules should include this snippet
29 # and customize variables below.
32 # Python 2.5 only accepts strings with proper Python internal encoding
33 # (i.e. ASCII or Unicode) when writing to stdout/stderr, so we must
34 # use ugettext iso gettext, and encode the string when writing to
37 localedir = '@localedir@'
40 t = gettext.translation ('lilypond', localedir)
47 # Urg, Python 2.4 does not define stderr/stdout encoding
48 # Maybe guess encoding from LANG/LC_ALL/LC_CTYPE?
50 def encoded_write(f, s):
51 f.write (s.encode (f.encoding or 'utf_8'))
53 # ugh, Python 2.5 optparse requires Unicode strings in some argument
54 # functions, and refuse them in some other places
55 def display_encode (s):
56 return s.encode (sys.stderr.encoding or 'utf_8')
59 program_version = '@TOPLEVEL_VERSION@'
60 program_name = os.path.basename (sys.argv[0])
63 # Check if program_version contains @ characters. This will be the case if
64 # the .py file is called directly while building the lilypond documentation.
65 # If so, try to check for the env var LILYPOND_VERSION, which is set by our
66 # makefiles and use its value.
67 at_re = re.compile (r'@')
68 if at_re.match (program_version):
69 if os.environ.has_key('LILYPOND_VERSION'):
70 program_version = os.environ['LILYPOND_VERSION']
72 program_version = "unknown"
75 encoded_write (sys.stderr, s)
78 stderr_write (program_name + ": " + _ ("warning: %s") % s + '\n')
81 stderr_write (program_name + ": " + _ ("error: %s") % s + '\n')
83 progress = stderr_write
86 def require_python_version ():
87 if sys.hexversion < 0x02040000:
88 stderr_write ("Python 2.4 or newer is required to run this program.\n\
89 Please upgrade Python from http://python.org/download/, and if you use MacOS X,\n\
90 please read 'Setup for MacOS X' in Application Usage.")
91 os.system ("open http://python.org/download/")
94 # Modified version of the commands.mkarg(x), which always uses
95 # double quotes (since Windows can't handle the single quotes:
105 def command_name (cmd):
106 # Strip all stuf after command,
107 # deal with "((latex ) >& 1 ) .." too
108 cmd = re.match ('([\(\)]*)([^\\\ ]*)', cmd).group (2)
109 return os.path.basename (cmd)
111 def subprocess_system (cmd,
118 show_progress= progress_p
119 name = command_name (cmd)
124 progress (_ ("Invoking `%s\'") % cmd)
126 progress ( _("Running %s...") % name)
129 stdout_setting = None
130 if not show_progress:
131 stdout_setting = subprocess.PIPE
133 proc = subprocess.Popen (cmd,
135 universal_newlines=True,
136 stdout=stdout_setting,
137 stderr=stdout_setting)
144 log = proc.communicate ()
145 retval = proc.returncode
149 print >>sys.stderr, 'command failed:', cmd
151 print >>sys.stderr, "Child was terminated by signal", -retval
153 print >>sys.stderr, "Child returned", retval
156 print >>sys.stderr, "Error ignored"
158 if not show_progress:
165 def ossystem_system (cmd,
172 name = command_name (cmd)
175 progress (_ ("Invoking `%s\'") % cmd)
177 progress ( _("Running %s...") % name)
179 retval = os.system (cmd)
181 print >>sys.stderr, 'command failed:', cmd
183 print >>sys.stderr, "Child was terminated by signal", -retval
185 print >>sys.stderr, "Child returned", retval
188 print >>sys.stderr, "Error ignored"
195 system = subprocess_system
196 if sys.platform == 'mingw32':
198 ## subprocess x-compile doesn't work.
199 system = ossystem_system
201 def strip_extension (f, ext):
202 (p, e) = os.path.splitext (f)
208 def search_exe_path (name):
209 p = os.environ['PATH']
210 exe_paths = p.split (':')
212 full = os.path.join (e, name)
213 if os.path.exists (full):
218 def print_environment ():
219 for (k,v) in os.environ.items ():
220 sys.stderr.write ("%s=\"%s\"\n" % (k, v))
222 class NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
223 def format_heading(self, heading):
225 return heading[0].upper() + heading[1:] + ':\n'
227 def format_option_strings(self, option):
229 if option._short_opts and option._long_opts:
233 if option.takes_value():
234 metavar = '=%s' % option.metavar or option.dest.upper()
236 return "%3s%s %s%s" % (" ".join (option._short_opts),
238 " ".join (option._long_opts),
241 # Only use one level of indentation (even for groups and nested groups),
242 # since we don't indent the headeings, either
244 self.current_indent = self.indent_increment
249 self.current_indent = ''
252 def format_usage(self, usage):
253 return _("Usage: %s") % usage + '\n'
255 def format_description(self, description):
258 def get_option_parser (*args, **kwargs):
259 p = optparse.OptionParser (*args, **kwargs)
260 p.formatter = NonDentedHeadingFormatter ()
261 p.formatter.set_parser (p)