1 ################################################################
2 # lilylib.py -- options and stuff
4 # source file of the GNU LilyPond music typesetter
6 # (c) 1998--2006 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 # Jan Nieuwenhuizen <janneke@gnu.org>
9 ### subst:\(^\|[^._a-z]\)\(abspath\|identify\|warranty\|progress\|warning\|error\|exit\|getopt_args\|option_help_str\|options_help_str\|help\|setup_temp\|read_pipe\|system\|cleanup_temp\|strip_extension\|cp_to_dir\|mkdir_p\|init\) *(
12 ### subst: \(help_summary\|keep_temp_dir_p\|option_definitions\|original_dir\|program_name\|pseudo_filter_p\|temp_dir\|verbose_p\)
23 ################################################################
24 # Users of python modules should include this snippet
25 # and customize variables below.
27 # We'll suffer this path init stuff as long as we don't install our
28 # python packages in <prefix>/lib/pythonx.y (and don't kludge around
29 # it as we do with teTeX on Red Hat Linux: set some environment var
30 # (PYTHONPATH) in profile)
32 # If set, LILYPONDPREFIX must take prevalence
33 # if datadir is not set, we're doing a build and LILYPONDPREFIX
35 datadir = '@local_lilypond_datadir@'
36 if not os.path.isdir (datadir):
37 datadir = '@lilypond_datadir@'
38 if os.environ.has_key ('LILYPONDPREFIX') :
39 datadir = os.environ['LILYPONDPREFIX']
40 while datadir[-1] == os.sep:
43 sys.path.insert (0, os.path.join (datadir, 'python'))
48 localedir = '@localedir@'
51 gettext.bindtextdomain ('lilypond', localedir)
52 gettext.textdomain ('lilypond')
58 progress = sys.stderr.write
61 def command_name (cmd):
62 # Strip all stuf after command,
63 # deal with "((latex ) >& 1 ) .." too
64 cmd = re.match ('([\(\)]*)([^\\\ ]*)', cmd).group (2)
65 return os.path.basename (cmd)
68 name = re.sub('[^a-z]','x', name)
69 return tempfile.mktemp ('%s.errorlog' % name)
72 def system (cmd, ignore_error = 0, progress_p = 0, be_verbose=0):
74 '''System CMD. If IGNORE_ERROR, do not complain when CMD
75 returns non zero. If PROGRESS_P, always show progress.
79 Exit status of CMD '''
81 name = command_name (cmd)
86 progress (_ ("Invoking `%s\'") % cmd)
88 progress ( _("Running %s...") % name)
92 error_log_file = error_log (name)
93 redirect = ' 1>/dev/null 2>' + error_log_file
95 status = os.system (cmd + redirect)
96 signal = 0x0f & status
97 exit_status = status >> 8
101 exit_type = 'status %d' % exit_status
103 exit_type = 'signal %d' % signal
105 msg = _ ("`%s\' failed (%s)") % (name, exit_type)
108 warning (msg + ' ' + _ ("(ignored)"))
111 if not progress_p and error_log_file:
112 error (_ ("The error log is as follows:"))
113 sys.stderr.write (open (error_log_file).read ())
115 os.unlink (error_log_file)
119 os.unlink (error_log_file)
123 def strip_extension (f, ext):
124 (p, e) = os.path.splitext (f)
130 def search_exe_path (name):
131 p = os.environ['PATH']
132 exe_paths = string.split (p, ':')
134 full = os.path.join (e, name)
135 if os.path.exists (full):
140 def print_environment ():
141 for (k,v) in os.environ.items ():
142 sys.stderr.write ("%s=\"%s\"\n" % (k, v))
145 def ps_page_count (ps_name):
146 header = open (ps_name).read (1024)
147 m = re.search ('\n%%Pages: ([0-9]+)', header)
149 return string.atoi (m.group (1))
152 class NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
153 def format_heading(self, heading):
155 return heading[0].upper() + heading[1:] + ':\n'
157 def format_option_strings(self, option):
159 if option._short_opts and option._long_opts:
163 if option.takes_value():
164 metavar = '=%s' % option.metavar or option.dest.upper()
166 return "%3s%s %s%s" % (" ".join (option._short_opts),
168 " ".join (option._long_opts),
171 def format_usage(self, usage):
172 return _("Usage: %s\n") % usage
174 def format_description(self, description):
177 def get_option_parser (*args, **kwargs):
178 p = optparse.OptionParser (*args, **kwargs)
179 p.formatter = NonDentedHeadingFormatter ()