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')
60 def command_name (cmd):
61 # Strip all stuf after command,
62 # deal with "((latex ) >& 1 ) .." too
63 cmd = re.match ('([\(\)]*)([^\\\ ]*)', cmd).group (2)
64 return os.path.basename (cmd)
67 name = re.sub('[^a-z]','x', name)
68 return tempfile.mktemp ('%s.errorlog' % name)
71 def system (cmd, ignore_error = 0, progress_p = 0, be_verbose=0):
73 '''System CMD. If IGNORE_ERROR, do not complain when CMD
74 returns non zero. If PROGRESS_P, always show progress.
78 Exit status of CMD '''
80 name = command_name (cmd)
85 progress (_ ("Invoking `%s\'") % cmd)
87 progress ( _("Running %s...") % name)
91 error_log_file = error_log (name)
92 redirect = ' 1>/dev/null 2>' + error_log_file
94 status = os.system (cmd + redirect)
95 signal = 0x0f & status
96 exit_status = status >> 8
100 exit_type = 'status %d' % exit_status
102 exit_type = 'signal %d' % signal
104 msg = _ ("`%s\' failed (%s)") % (name, exit_type)
107 warning (msg + ' ' + _ ("(ignored)"))
110 if not progress_p and error_log_file:
111 error (_ ("The error log is as follows:"))
112 sys.stderr.write (open (error_log_file).read ())
114 os.unlink (error_log_file)
118 os.unlink (error_log_file)
122 def strip_extension (f, ext):
123 (p, e) = os.path.splitext (f)
129 def search_exe_path (name):
130 p = os.environ['PATH']
131 exe_paths = string.split (p, ':')
133 full = os.path.join (e, name)
134 if os.path.exists (full):
139 def print_environment ():
140 for (k,v) in os.environ.items ():
141 sys.stderr.write ("%s=\"%s\"\n" % (k, v))
144 def ps_page_count (ps_name):
145 header = open (ps_name).read (1024)
146 m = re.search ('\n%%Pages: ([0-9]+)', header)
148 return string.atoi (m.group (1))
151 class NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
152 def format_heading(self, heading):
154 return heading[0].upper() + heading[1:] + ':\n'
156 def format_option_strings(self, option):
158 if option._short_opts and option._long_opts:
162 if option.takes_value():
163 metavar = '=%s' % option.metavar or option.dest.upper()
165 return "%3s%s %s%s" % (" ".join (option._short_opts),
167 " ".join (option._long_opts),
170 def format_usage(self, usage):
171 return _("Usage: %s\n") % usage
173 def format_description(self, description):
176 def get_option_parser (*args, **kwargs):
177 p = optparse.OptionParser (*args, **kwargs)
178 p.formatter = NonDentedHeadingFormatter ()