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
56 def command_name (cmd):
57 # Strip all stuf after command,
58 # deal with "((latex ) >& 1 ) .." too
59 cmd = re.match ('([\(\)]*)([^\\\ ]*)', cmd).group (2)
60 return os.path.basename (cmd)
70 show_progress= progress_p
71 name = command_name (cmd)
76 progress (_ ("Invoking `%s\'") % cmd)
78 progress ( _("Running %s...") % name)
83 stdout_setting = subprocess.PIPE
85 proc = subprocess.Popen (cmd,
87 universal_newlines=True,
88 stdout=stdout_setting,
89 stderr=stdout_setting)
96 log = proc.communicate ()
97 retval = proc.returncode
101 print >>sys.stderr, 'command failed:', cmd
103 print >>sys.stderr, "Child was terminated by signal", -retval
105 print >>sys.stderr, "Child returned", retval
108 print >>sys.stderr, "Error ignored"
110 if not show_progress:
117 def strip_extension (f, ext):
118 (p, e) = os.path.splitext (f)
124 def search_exe_path (name):
125 p = os.environ['PATH']
126 exe_paths = string.split (p, ':')
128 full = os.path.join (e, name)
129 if os.path.exists (full):
134 def print_environment ():
135 for (k,v) in os.environ.items ():
136 sys.stderr.write ("%s=\"%s\"\n" % (k, v))
138 class NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
139 def format_heading(self, heading):
141 return heading[0].upper() + heading[1:] + ':\n'
143 def format_option_strings(self, option):
145 if option._short_opts and option._long_opts:
149 if option.takes_value():
150 metavar = '=%s' % option.metavar or option.dest.upper()
152 return "%3s%s %s%s" % (" ".join (option._short_opts),
154 " ".join (option._long_opts),
157 def format_usage(self, usage):
158 return _("Usage: %s\n") % usage
160 def format_description(self, description):
163 def get_option_parser (*args, **kwargs):
164 p = optparse.OptionParser (*args, **kwargs)
165 p.formatter = NonDentedHeadingFormatter ()