]> git.donarmstrong.com Git - lilypond.git/blob - python/lilylib.py
* python/lilylib.py (ps_page_count): remove make_ps_images().
[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
59
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)
65
66 def error_log (name):
67         name = re.sub('[^a-z]','x', name)
68         return tempfile.mktemp ('%s.errorlog' % name)
69
70
71 def system (cmd, ignore_error = 0, progress_p = 0, be_verbose=0):
72         
73         '''System CMD.  If IGNORE_ERROR, do not complain when CMD
74 returns non zero.  If PROGRESS_P, always show progress.
75
76 RETURN VALUE
77
78 Exit status of CMD '''
79
80         name = command_name (cmd)
81         error_log_file = ''
82
83         if be_verbose:
84                 progress_p = 1
85                 progress (_ ("Invoking `%s\'") % cmd)
86         else:
87                 progress ( _("Running %s...") % name)
88
89         redirect = ''
90         if not progress_p:
91                 error_log_file = error_log (name)
92                 redirect = ' 1>/dev/null 2>' + error_log_file
93
94         status = os.system (cmd + redirect)
95         signal = 0x0f & status
96         exit_status = status >> 8
97         
98         if status:
99                 
100                 exit_type =  'status %d' % exit_status
101                 if signal:
102                         exit_type = 'signal %d' % signal 
103                 
104                 msg = _ ("`%s\' failed (%s)") % (name, exit_type)
105                 if ignore_error:
106                         if be_verbose:
107                                 warning (msg + ' ' + _ ("(ignored)"))
108                 else:
109                         error (msg)
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 ())
113                         if error_log_file:
114                                 os.unlink (error_log_file)
115                         exit (1)
116
117         if error_log_file:
118                 os.unlink (error_log_file)
119         progress ('\n')
120         return status
121
122 def strip_extension (f, ext):
123         (p, e) = os.path.splitext (f)
124         if e == ext:
125                 e = ''
126         return p + e
127
128
129 def search_exe_path (name):
130         p = os.environ['PATH']
131         exe_paths = string.split (p, ':')
132         for e in exe_paths:
133                 full = os.path.join (e, name)
134                 if os.path.exists (full):
135                         return full
136         return None
137
138
139 def print_environment ():
140         for (k,v) in os.environ.items ():
141                 sys.stderr.write ("%s=\"%s\"\n" % (k, v)) 
142
143
144 def ps_page_count (ps_name):
145         header = open (ps_name).read (1024)
146         m = re.search ('\n%%Pages: ([0-9]+)', header)
147         if m:
148                 return string.atoi (m.group (1))
149         return 0
150
151 class NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
152     def format_heading(self, heading):
153             if heading:
154                     return heading[0].upper() + heading[1:] + ':\n'
155             return ''
156     def format_option_strings(self, option):
157             sep = ' '
158             if option._short_opts and option._long_opts:
159                     sep = ','
160
161             metavar = ''
162             if option.takes_value():
163                     metavar = '=%s' % option.metavar or option.dest.upper()
164
165             return "%3s%s %s%s" % (" ".join (option._short_opts),
166                                    sep,
167                                    " ".join (option._long_opts),
168                                    metavar)
169
170     def format_usage(self, usage):
171         return _("Usage: %s\n") % usage
172     
173     def format_description(self, description):
174             return description
175
176 def get_option_parser (*args, **kwargs): 
177         p = optparse.OptionParser (*args, **kwargs)
178         p.formatter = NonDentedHeadingFormatter () 
179         return p