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/>.
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?
52 sys.setdefaultencoding ('utf-8')
54 sys.stdout = codecs.getwriter ('utf8') (sys.stdout)
55 sys.stderr = codecs.getwriter ('utf8') (sys.stderr)
57 def encoded_write(f, s):
58 f.write (s.encode (f.encoding or 'utf-8', 'replace'))
60 # ugh, Python 2.5 optparse requires Unicode strings in some argument
61 # functions, and refuse them in some other places
62 def display_encode (s):
63 return s.encode (sys.stderr.encoding or 'utf-8', 'replace')
66 program_version = '@TOPLEVEL_VERSION@'
67 program_name = os.path.basename (sys.argv[0])
70 # Check if program_version contains @ characters. This will be the case if
71 # the .py file is called directly while building the lilypond documentation.
72 # If so, try to check for the env var LILYPOND_VERSION, which is set by our
73 # makefiles and use its value.
74 at_re = re.compile (r'@')
75 if at_re.match (program_version):
76 if os.environ.has_key('LILYPOND_VERSION'):
77 program_version = os.environ['LILYPOND_VERSION']
79 program_version = "unknown"
82 encoded_write (sys.stderr, s)
85 stderr_write (program_name + ": " + _ ("warning: %s") % s + '\n')
88 stderr_write (program_name + ": " + _ ("error: %s") % s + '\n')
90 progress = stderr_write
93 def require_python_version ():
94 if sys.hexversion < 0x02040000:
95 stderr_write ("Python 2.4 or newer is required to run this program.\n\
96 Please upgrade Python from http://python.org/download/, and if you use MacOS X,\n\
97 please read 'Setup for MacOS X' in Application Usage.")
98 os.system ("open http://python.org/download/")
101 # Modified version of the commands.mkarg(x), which always uses
102 # double quotes (since Windows can't handle the single quotes:
112 def command_name (cmd):
113 # Strip all stuf after command,
114 # deal with "((latex ) >& 1 ) .." too
115 cmd = re.match ('([\(\)]*)([^\\\ ]*)', cmd).group (2)
116 return os.path.basename (cmd)
118 def subprocess_system (cmd,
122 redirect_output=False,
126 show_progress= progress_p
127 name = command_name (cmd)
131 progress (_ ("Processing %s.ly") % log_file)
135 progress (_ ("Invoking `%s\'") % cmd)
137 progress ( _("Running %s...") % name)
139 stdout_setting = None
140 stderr_setting = None
141 if not show_progress:
142 stdout_setting = subprocess.PIPE
145 stdout_filename = ' '.join([log_file, '.log'])
146 stderr_filename = ' '.join([log_file, '.err.log'])
147 stdout_setting = open(stdout_filename, 'w')
148 stderr_setting = open(stderr_filename, 'w')
150 proc = subprocess.Popen (cmd,
152 universal_newlines=True,
153 stdout=stdout_setting,
154 stderr=stderr_setting)
159 while proc.poll()==None:
161 retval = proc.returncode
162 stdout_setting.close()
163 stderr_setting.close()
168 log = proc.communicate ()
169 retval = proc.returncode
172 print >>sys.stderr, 'command failed:', cmd
174 print >>sys.stderr, "Child was terminated by signal", -retval
176 print >>sys.stderr, "Child returned", retval
179 print >>sys.stderr, "Error ignored"
181 if not show_progress:
188 def ossystem_system (cmd,
195 name = command_name (cmd)
198 progress (_ ("Invoking `%s\'") % cmd)
200 progress ( _("Running %s...") % name)
202 retval = os.system (cmd)
204 print >>sys.stderr, 'command failed:', cmd
206 print >>sys.stderr, "Child was terminated by signal", -retval
208 print >>sys.stderr, "Child returned", retval
211 print >>sys.stderr, "Error ignored"
218 system = subprocess_system
219 if sys.platform == 'mingw32':
221 ## subprocess x-compile doesn't work.
222 system = ossystem_system
224 def strip_extension (f, ext):
225 (p, e) = os.path.splitext (f)
231 def search_exe_path (name):
232 p = os.environ['PATH']
233 exe_paths = p.split (':')
235 full = os.path.join (e, name)
236 if os.path.exists (full):
241 def print_environment ():
242 for (k,v) in os.environ.items ():
243 sys.stderr.write ("%s=\"%s\"\n" % (k, v))
245 class NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
246 def format_heading(self, heading):
248 return heading[0].upper() + heading[1:] + ':\n'
250 def format_option_strings(self, option):
252 if option._short_opts and option._long_opts:
256 if option.takes_value():
257 metavar = '=%s' % option.metavar or option.dest.upper()
259 return "%3s%s %s%s" % (" ".join (option._short_opts),
261 " ".join (option._long_opts),
264 # Only use one level of indentation (even for groups and nested groups),
265 # since we don't indent the headeings, either
267 self.current_indent = self.indent_increment
272 self.current_indent = ''
275 def format_usage(self, usage):
276 return _("Usage: %s") % usage + '\n'
278 def format_description(self, description):
281 def get_option_parser (*args, **kwargs):
282 p = optparse.OptionParser (*args, **kwargs)
283 p.formatter = NonDentedHeadingFormatter ()
284 p.formatter.set_parser (p)