]> git.donarmstrong.com Git - lilypond.git/blob - python/lilylib.py
release commit
[lilypond.git] / python / lilylib.py
1 ################################################################
2 # lilylib.py -- options and stuff
3
4 # source file of the GNU LilyPond music typesetter
5 #
6 # (c) 1998--2006 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 #                 Jan Nieuwenhuizen <janneke@gnu.org>
8
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\) *(
10 ###  replace:\1ly.\2 (
11
12 ### subst: \(help_summary\|keep_temp_dir_p\|option_definitions\|original_dir\|program_name\|pseudo_filter_p\|temp_dir\|verbose_p\)
13
14 import __main__
15 import glob
16 import os
17 import re
18 import shutil
19 import string
20 import sys
21 import optparse
22
23 ################################################################
24 # Users of python modules should include this snippet
25 # and customize variables below.
26
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)
31
32 # If set, LILYPONDPREFIX must take prevalence
33 # if datadir is not set, we're doing a build and LILYPONDPREFIX
34
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:
41                 datadir= datadir[:-1]
42
43 sys.path.insert (0, os.path.join (datadir, 'python'))
44
45
46
47
48 localedir = '@localedir@'
49 try:
50         import gettext
51         gettext.bindtextdomain ('lilypond', localedir)
52         gettext.textdomain ('lilypond')
53         _ = gettext.gettext
54 except:
55         def _ (s):
56                 return s
57 underscore = _
58 progress = sys.stderr.write 
59
60
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)
66
67 def error_log (name):
68         name = re.sub('[^a-z]','x', name)
69         return tempfile.mktemp ('%s.errorlog' % name)
70
71
72 def system (cmd, ignore_error = 0, progress_p = 0, be_verbose=0):
73         
74         '''System CMD.  If IGNORE_ERROR, do not complain when CMD
75 returns non zero.  If PROGRESS_P, always show progress.
76
77 RETURN VALUE
78
79 Exit status of CMD '''
80
81         name = command_name (cmd)
82         error_log_file = ''
83
84         if be_verbose:
85                 progress_p = 1
86                 progress (_ ("Invoking `%s\'") % cmd)
87         else:
88                 progress ( _("Running %s...") % name)
89
90         redirect = ''
91         if not progress_p:
92                 error_log_file = error_log (name)
93                 redirect = ' 1>/dev/null 2>' + error_log_file
94
95         status = os.system (cmd + redirect)
96         signal = 0x0f & status
97         exit_status = status >> 8
98         
99         if status:
100                 
101                 exit_type =  'status %d' % exit_status
102                 if signal:
103                         exit_type = 'signal %d' % signal 
104                 
105                 msg = _ ("`%s\' failed (%s)") % (name, exit_type)
106                 if ignore_error:
107                         if be_verbose:
108                                 warning (msg + ' ' + _ ("(ignored)"))
109                 else:
110                         error (msg)
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 ())
114                         if error_log_file:
115                                 os.unlink (error_log_file)
116                         exit (1)
117
118         if error_log_file:
119                 os.unlink (error_log_file)
120         progress ('\n')
121         return status
122
123 def strip_extension (f, ext):
124         (p, e) = os.path.splitext (f)
125         if e == ext:
126                 e = ''
127         return p + e
128
129
130 def search_exe_path (name):
131         p = os.environ['PATH']
132         exe_paths = string.split (p, ':')
133         for e in exe_paths:
134                 full = os.path.join (e, name)
135                 if os.path.exists (full):
136                         return full
137         return None
138
139
140 def print_environment ():
141         for (k,v) in os.environ.items ():
142                 sys.stderr.write ("%s=\"%s\"\n" % (k, v)) 
143
144
145 def ps_page_count (ps_name):
146         header = open (ps_name).read (1024)
147         m = re.search ('\n%%Pages: ([0-9]+)', header)
148         if m:
149                 return string.atoi (m.group (1))
150         return 0
151
152 class NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
153     def format_heading(self, heading):
154             if heading:
155                     return heading[0].upper() + heading[1:] + ':\n'
156             return ''
157     def format_option_strings(self, option):
158             sep = ' '
159             if option._short_opts and option._long_opts:
160                     sep = ','
161
162             metavar = ''
163             if option.takes_value():
164                     metavar = '=%s' % option.metavar or option.dest.upper()
165
166             return "%3s%s %s%s" % (" ".join (option._short_opts),
167                                    sep,
168                                    " ".join (option._long_opts),
169                                    metavar)
170
171     def format_usage(self, usage):
172         return _("Usage: %s\n") % usage
173     
174     def format_description(self, description):
175             return description
176
177 def get_option_parser (*args, **kwargs): 
178         p = optparse.OptionParser (*args, **kwargs)
179         p.formatter = NonDentedHeadingFormatter () 
180         return p