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\)
24 ################################################################
25 # Users of python modules should include this snippet
26 # and customize variables below.
28 # We'll suffer this path init stuff as long as we don't install our
29 # python packages in <prefix>/lib/pythonx.y (and don't kludge around
30 # it as we do with teTeX on Red Hat Linux: set some environment var
31 # (PYTHONPATH) in profile)
33 # If set, LILYPONDPREFIX must take prevalence
34 # if datadir is not set, we're doing a build and LILYPONDPREFIX
36 datadir = '@local_lilypond_datadir@'
37 if not os.path.isdir (datadir):
38 datadir = '@lilypond_datadir@'
39 if os.environ.has_key ('LILYPONDPREFIX') :
40 datadir = os.environ['LILYPONDPREFIX']
41 while datadir[-1] == os.sep:
44 sys.path.insert (0, os.path.join (datadir, 'python'))
49 localedir = '@localedir@'
52 gettext.bindtextdomain ('lilypond', localedir)
53 gettext.textdomain ('lilypond')
59 progress = sys.stderr.write
62 def command_name (cmd):
63 # Strip all stuf after command,
64 # deal with "((latex ) >& 1 ) .." too
65 cmd = re.match ('([\(\)]*)([^\\\ ]*)', cmd).group (2)
66 return os.path.basename (cmd)
69 name = re.sub('[^a-z]','x', name)
70 return tempfile.mktemp ('%s.errorlog' % name)
73 def system (cmd, ignore_error = 0, progress_p = 0, be_verbose=0):
75 '''System CMD. If IGNORE_ERROR, do not complain when CMD
76 returns non zero. If PROGRESS_P, always show progress.
80 Exit status of CMD '''
82 name = command_name (cmd)
87 progress (_ ("Invoking `%s\'") % cmd)
89 progress ( _("Running %s...") % name)
93 error_log_file = error_log (name)
94 redirect = ' 1>/dev/null 2>' + error_log_file
96 status = os.system (cmd + redirect)
97 signal = 0x0f & status
98 exit_status = status >> 8
102 exit_type = 'status %d' % exit_status
104 exit_type = 'signal %d' % signal
106 msg = _ ("`%s\' failed (%s)") % (name, exit_type)
109 warning (msg + ' ' + _ ("(ignored)"))
112 if not progress_p and error_log_file:
113 error (_ ("The error log is as follows:"))
114 sys.stderr.write (open (error_log_file).read ())
116 os.unlink (error_log_file)
120 os.unlink (error_log_file)
124 def strip_extension (f, ext):
125 (p, e) = os.path.splitext (f)
131 def search_exe_path (name):
132 p = os.environ['PATH']
133 exe_paths = string.split (p, ':')
135 full = os.path.join (e, name)
136 if os.path.exists (full):
141 def print_environment ():
142 for (k,v) in os.environ.items ():
143 sys.stderr.write ("%s=\"%s\"\n" % (k, v))
146 def ps_page_count (ps_name):
147 header = open (ps_name).read (1024)
148 m = re.search ('\n%%Pages: ([0-9]+)', header)
150 return string.atoi (m.group (1))
153 class NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
154 def format_heading(self, heading):
156 return heading[0].upper() + heading[1:] + ':\n'
158 def format_option_strings(self, option):
160 if option._short_opts and option._long_opts:
164 if option.takes_value():
165 metavar = '=%s' % option.metavar or option.dest.upper()
167 return "%3s%s %s%s" % (" ".join (option._short_opts),
169 " ".join (option._long_opts),
172 def format_usage(self, usage):
173 return _("Usage: %s\n") % usage
175 def format_description(self, description):
178 def get_option_parser (*args, **kwargs):
179 p = optparse.OptionParser (*args, **kwargs)
180 p.formatter = NonDentedHeadingFormatter ()