]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/lilylib.py.in
10ee45aa9ce39a2bdaad3e67a3f57e2285f3ed6c
[lilypond.git] / buildscripts / lilylib.py.in
1 # lilylib.py -- options and stuff
2
3 # source file of the GNU LilyPond music typesetter
4
5 import os
6 from __main__ import *
7
8 try:
9         import gettext
10         gettext.bindtextdomain ('lilypond', '@localedir@')
11         gettext.textdomain ('lilypond')
12         _ = gettext.gettext
13 except:
14         def _ (s):
15                 return s
16
17 program_version = '@TOPLEVEL_VERSION@'
18 if program_version == '@' + 'TOPLEVEL_VERSION' + '@':
19         program_version = '1.5.17'
20
21
22 original_dir = os.getcwd ()
23 temp_dir = os.path.join (original_dir,  '%s.dir' % program_name)
24
25 errorport = sys.stderr
26 keep_temp_dir_p = 0
27 verbose_p = 0
28
29
30 def identify ():
31         sys.stdout.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
32
33 def warranty ():
34         identify ()
35         sys.stdout.write ('\n')
36         sys.stdout.write (_ ('Copyright (c) %s by' % ' 2001'))
37         sys.stdout.write ('\n')
38         sys.stdout.write ('  Han-Wen Nienhuys')
39         sys.stdout.write ('  Jan Nieuwenhuizen')
40         sys.stdout.write ('\n')
41         sys.stdout.write (_ (r'''
42 Distributed under terms of the GNU General Public License. It comes with
43 NO WARRANTY.'''))
44         sys.stdout.write ('\n')
45
46 def progress (s):
47         errorport.write (s + '\n')
48
49 def warning (s):
50         progress (_ ("warning: ") + s)
51                 
52 def error (s):
53
54
55         '''Report the error S.  Exit by raising an exception. Please
56         do not abuse by trying to catch this error. If you do not want
57         a stack trace, write to the output directly.
58
59         RETURN VALUE
60
61         None
62         
63         '''
64         
65         progress (_ ("error: ") + s)
66         raise _ ("Exiting ... ")
67
68 def getopt_args (opts):
69         '''Construct arguments (LONG, SHORT) for getopt from  list of options.'''
70         short = ''
71         long = []
72         for o in opts:
73                 if o[1]:
74                         short = short + o[1]
75                         if o[0]:
76                                 short = short + ':'
77                 if o[2]:
78                         l = o[2]
79                         if o[0]:
80                                 l = l + '='
81                         long.append (l)
82         return (short, long)
83
84 def option_help_str (o):
85         '''Transform one option description (4-tuple ) into neatly formatted string'''
86         sh = '  '       
87         if o[1]:
88                 sh = '-%s' % o[1]
89
90         sep = ' '
91         if o[1] and o[2]:
92                 sep = ','
93                 
94         long = ''
95         if o[2]:
96                 long= '--%s' % o[2]
97
98         arg = ''
99         if o[0]:
100                 if o[2]:
101                         arg = '='
102                 arg = arg + o[0]
103         return '  ' + sh + sep + long + arg
104
105
106 def options_help_str (opts):
107         '''Convert a list of options into a neatly formatted string'''
108         w = 0
109         strs =[]
110         helps = []
111
112         for o in opts:
113                 s = option_help_str (o)
114                 strs.append ((s, o[3]))
115                 if len (s) > w:
116                         w = len (s)
117
118         str = ''
119         for s in strs:
120                 str = str + '%s%s%s\n' % (s[0], ' ' * (w - len(s[0])  + 3), s[1])
121         return str
122
123 def help ():
124         ls = [(_ ("Usage: %s [OPTION]... FILE") % program_name),
125                 ('\n\n'),
126                 (help_summary),
127                 ('\n\n'),
128                 (_ ("Options:")),
129                 ('\n'),
130                 (options_help_str (option_definitions)),
131                 ('\n\n'),
132                 (_ ("Report bugs to %s") % 'bug-lilypond@gnu.org'),
133                 ('\n')]
134         map (sys.stdout.write, ls)
135         
136 def setup_temp ():
137         """
138         Create a temporary directory, and return its name. 
139         """
140         global temp_dir
141         if not keep_temp_dir_p:
142                 temp_dir = tempfile.mktemp (program_name)
143         try:
144                 os.mkdir (temp_dir, 0777)
145         except OSError:
146                 pass
147
148         return temp_dir
149
150
151 def system (cmd, ignore_error = 0):
152         """Run CMD. If IGNORE_ERROR is set, don't complain when CMD returns non zero.
153
154         RETURN VALUE
155
156         Exit status of CMD
157         """
158         
159         if verbose_p:
160                 progress (_ ("Invoking `%s\'") % cmd)
161         st = os.system (cmd)
162         if st:
163                 name = re.match ('[ \t]*([^ \t]*)', cmd).group (1)
164                 msg = name + ': ' + _ ("command exited with value %d") % st
165                 if ignore_error:
166                         warning (msg + ' ' + _ ("(ignored)") + ' ')
167                 else:
168                         error (msg)
169
170         return st
171
172
173 def cleanup_temp ():
174         if not keep_temp_dir_p:
175                 if verbose_p:
176                         progress (_ ("Cleaning %s...") % temp_dir)
177                 shutil.rmtree (temp_dir)
178
179
180 def strip_extension (f, ext):
181         (p, e) = os.path.splitext (f)
182         if e == ext:
183                 e = ''
184         return p + e
185
186 # END Library
187