]> git.donarmstrong.com Git - lilypond.git/blob - python/lilylib.py
(print_environment): move ps_page_count to lilypond-book.py
[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 import __main__
10 import glob
11 import os
12 import re
13 import shutil
14 import string
15 import sys
16 import optparse
17 import subprocess
18
19 ## windows mingw cross compile doesn't have selectmodule.so
20 have_select = True
21 try:
22         import select
23 except ImportError:
24         have_select = False
25         
26 ################################################################
27 # Users of python modules should include this snippet
28 # and customize variables below.
29
30 # We'll suffer this path init stuff as long as we don't install our
31 # python packages in <prefix>/lib/pythonx.y (and don't kludge around
32 # it as we do with teTeX on Red Hat Linux: set some environment var
33 # (PYTHONPATH) in profile)
34
35 # If set, LILYPONDPREFIX must take prevalence
36 # if datadir is not set, we're doing a build and LILYPONDPREFIX
37
38 datadir = '@local_lilypond_datadir@'
39 if not os.path.isdir (datadir):
40         datadir = '@lilypond_datadir@'
41 if os.environ.has_key ('LILYPONDPREFIX') :
42         datadir = os.environ['LILYPONDPREFIX']
43         while datadir[-1] == os.sep:
44                 datadir= datadir[:-1]
45
46 sys.path.insert (0, os.path.join (datadir, 'python'))
47
48
49
50
51 localedir = '@localedir@'
52 try:
53         import gettext
54         gettext.bindtextdomain ('lilypond', localedir)
55         gettext.textdomain ('lilypond')
56         _ = gettext.gettext
57 except:
58         def _ (s):
59                 return s
60 underscore = _
61 progress = sys.stderr.write 
62
63
64 def command_name (cmd):
65         # Strip all stuf after command,
66         # deal with "((latex ) >& 1 ) .." too
67         cmd = re.match ('([\(\)]*)([^\\\ ]*)', cmd).group (2)
68         return os.path.basename (cmd)
69
70 def system (cmd,
71               ignore_error=False,
72               progress_p=True,
73               be_verbose=False,
74               log_file=None):
75         
76         show_progress= progress_p 
77         name = command_name (cmd)
78         error_log_file = ''
79
80         if be_verbose:
81                 show_progress = 1
82                 progress (_ ("Invoking `%s\'") % cmd)
83         else:
84                 progress ( _("Running %s...") % name)
85
86
87         stdout_setting = None
88         if not show_progress:
89                 stdout_setting = subprocess.PIPE
90                 
91         proc = subprocess.Popen (cmd,
92                                  shell=True,
93                                  universal_newlines=True,
94                                  stdout=stdout_setting,
95                                  stderr=stdout_setting)
96
97         log = ''
98
99         if not have_select:
100                 show_progress = True
101
102         if show_progress:
103                 retval = proc.wait()
104         else:
105                 log = proc.communicate ()
106                 retval = proc.returncode
107
108
109         if retval:
110                 print >>sys.stderr, 'command failed:', cmd
111                 if retval < 0:
112                     print >>sys.stderr, "Child was terminated by signal", -retval
113                 elif retval > 0:
114                     print >>sys.stderr, "Child returned", retval
115
116                 if ignore_error:
117                         print >>sys.stderr, "Error ignored"
118                 else:
119                         if not show_progress:
120                                 print log[0]
121                                 print log[1]
122                         sys.exit (1)
123
124         return abs (retval)
125
126 def strip_extension (f, ext):
127         (p, e) = os.path.splitext (f)
128         if e == ext:
129                 e = ''
130         return p + e
131
132
133 def search_exe_path (name):
134         p = os.environ['PATH']
135         exe_paths = string.split (p, ':')
136         for e in exe_paths:
137                 full = os.path.join (e, name)
138                 if os.path.exists (full):
139                         return full
140         return None
141
142
143 def print_environment ():
144         for (k,v) in os.environ.items ():
145                 sys.stderr.write ("%s=\"%s\"\n" % (k, v)) 
146
147
148 class NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
149     def format_heading(self, heading):
150             if heading:
151                     return heading[0].upper() + heading[1:] + ':\n'
152             return ''
153     def format_option_strings(self, option):
154             sep = ' '
155             if option._short_opts and option._long_opts:
156                     sep = ','
157
158             metavar = ''
159             if option.takes_value():
160                     metavar = '=%s' % option.metavar or option.dest.upper()
161
162             return "%3s%s %s%s" % (" ".join (option._short_opts),
163                                    sep,
164                                    " ".join (option._long_opts),
165                                    metavar)
166
167     def format_usage(self, usage):
168         return _("Usage: %s\n") % usage
169     
170     def format_description(self, description):
171             return description
172
173 def get_option_parser (*args, **kwargs): 
174         p = optparse.OptionParser (*args, **kwargs)
175         p.formatter = NonDentedHeadingFormatter () 
176         return p