1 ###############################################################
2 # lilylib.py -- options and stuff
4 # source file of the GNU LilyPond music typesetter
6 # (c) 1998--2006 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, LILYPONDPREFIX must take prevalence
28 # if datadir is not set, we're doing a build and LILYPONDPREFIX
30 datadir = '@local_lilypond_datadir@'
31 if not os.path.isdir (datadir):
32 datadir = '@lilypond_datadir@'
33 if os.environ.has_key ('LILYPONDPREFIX') :
34 datadir = os.environ['LILYPONDPREFIX']
35 while datadir[-1] == os.sep:
38 sys.path.insert (0, os.path.join (datadir, 'python'))
43 localedir = '@localedir@'
46 gettext.bindtextdomain ('lilypond', localedir)
47 gettext.textdomain ('lilypond')
53 progress = sys.stderr.write
55 # Modified version of the commands.mkarg(x), which always uses
56 # double quotes (since Windows can't handle the single quotes:
66 def command_name (cmd):
67 # Strip all stuf after command,
68 # deal with "((latex ) >& 1 ) .." too
69 cmd = re.match ('([\(\)]*)([^\\\ ]*)', cmd).group (2)
70 return os.path.basename (cmd)
80 show_progress= progress_p
81 name = command_name (cmd)
86 progress (_ ("Invoking `%s\'") % cmd)
88 progress ( _("Running %s...") % name)
93 stdout_setting = subprocess.PIPE
95 proc = subprocess.Popen (cmd,
97 universal_newlines=True,
98 stdout=stdout_setting,
99 stderr=stdout_setting)
106 log = proc.communicate ()
107 retval = proc.returncode
111 print >>sys.stderr, 'command failed:', cmd
113 print >>sys.stderr, "Child was terminated by signal", -retval
115 print >>sys.stderr, "Child returned", retval
118 print >>sys.stderr, "Error ignored"
120 if not show_progress:
127 def strip_extension (f, ext):
128 (p, e) = os.path.splitext (f)
134 def search_exe_path (name):
135 p = os.environ['PATH']
136 exe_paths = string.split (p, ':')
138 full = os.path.join (e, name)
139 if os.path.exists (full):
144 def print_environment ():
145 for (k,v) in os.environ.items ():
146 sys.stderr.write ("%s=\"%s\"\n" % (k, v))
148 class NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
149 def format_heading(self, heading):
151 return heading[0].upper() + heading[1:] + ':\n'
153 def format_option_strings(self, option):
155 if option._short_opts and option._long_opts:
159 if option.takes_value():
160 metavar = '=%s' % option.metavar or option.dest.upper()
162 return "%3s%s %s%s" % (" ".join (option._short_opts),
164 " ".join (option._long_opts),
167 def format_usage(self, usage):
168 return _("Usage: %s\n") % usage
170 def format_description(self, description):
173 def get_option_parser (*args, **kwargs):
174 p = optparse.OptionParser (*args, **kwargs)
175 p.formatter = NonDentedHeadingFormatter ()