-def identify (port):
- port.write ('%s (GNU LilyPond) %s\n' % (__main__.program_name, __main__.program_version))
-
-def warranty ():
- identify (sys.stdout)
- sys.stdout.write ('\n')
- sys.stdout.write (_ ("Copyright (c) %s by") % '1998--2004')
- sys.stdout.write ('\n')
- map (lambda x: sys.stdout.write (' %s\n' % x), __main__.copyright)
- sys.stdout.write ('\n')
- sys.stdout.write (_ ("Distributed under terms of the GNU General Public License."))
- sys.stdout.write ('\n')
- sys.stdout.write (_ ("It comes with NO WARRANTY."))
- sys.stdout.write ('\n')
-
-def progress (s):
- sys.stderr.write (s)
-
-def warning (s):
- sys.stderr.write (__main__.program_name + ": " + _ ("warning: %s") % s + '\n')
-
-def error (s):
- sys.stderr.write (__main__.program_name + ": " + _ ("error: %s") % s + '\n')
-
-def exit (i):
- if __main__.verbose_p:
- raise _ ('Exiting (%d)...') % i
- else:
- sys.exit (i)
-
-def getopt_args (opts):
- '''Construct arguments (LONG, SHORT) for getopt from list of options.'''
- short = ''
- long = []
- for o in opts:
- if o[1]:
- short = short + o[1]
- if o[0]:
- short = short + ':'
- if o[2]:
- l = o[2]
- if o[0]:
- l = l + '='
- long.append (l)
- return (short, long)
-
-def option_help_str (o):
- '''Transform one option description (4-tuple ) into neatly formatted string'''
- sh = ' '
- if o[1]:
- sh = '-%s' % o[1]
-
- sep = ' '
- if o[1] and o[2]:
- sep = ', '
-
- long = ''
- if o[2]:
- long= '--%s' % o[2]
-
- arg = ''
- if o[0]:
- if o[2]:
- arg = '='
- arg = arg + o[0]
- return ' ' + sh + sep + long + arg
-
-
-def options_help_str (opts):
- '''Convert a list of options into a neatly formatted string'''
- w = 0
- strs =[]
- helps = []
-
- for o in opts:
- s = option_help_str (o)
- strs.append ((s, o[3]))
- if len (s) > w:
- w = len (s)
-
- str = ''
- for s in strs:
- str = str + '%s%s%s\n' % (s[0], ' ' * (w - len(s[0]) + 3), s[1])
- return str
-
-def help ():
- ls = [(_ ("Usage: %s [OPTIONS]... FILE") % __main__.program_name),
- ('\n\n'),
- (__main__.help_summary),
- ('\n\n'),
- (_ ("Options:")),
- ('\n'),
- (options_help_str (__main__.option_definitions)),
- ('\n\n'),
- (_ ("Report bugs to %s.") % 'bug-lilypond@gnu.org'),
- ('\n')]
- map (sys.stdout.write, ls)
-
-def lilypond_version (binary):
- p = read_pipe ('%s --version ' % binary)
-
- ls = p.split ('\n')
- v= '<not found>'
- for l in ls:
- m = re.search ('GNU LilyPond ([0-9a-z.]+)', p)
- if m:
- v = m.group (1)
-
- return v
-
-def lilypond_version_check (binary, req):
- if req[0] <> '@' :
- v = lilypond_version (binary)
- if v <> req:
- error (_("Binary %s has version %s, looking for version %s") % \
- (binary, v, req))
- sys.exit (1)
-
-
-def setup_temp ():
-
- ''' Create a temporary directory, and return its name. '''
-
- if not __main__.keep_temp_dir_p:
- __main__.temp_dir = tempfile.mktemp (__main__.program_name)
- try:
- os.mkdir (__main__.temp_dir, 0700)
- except OSError:
- pass
-
- return __main__.temp_dir
-
-def command_name (cmd):
-
- # deal with "((latex ) >& 1 ) .." too
- cmd = re.match ("([\(\)]*)([^ ]*)", cmd).group(2)
- return os.path.basename (cmd)
-
-def error_log (name):
- name = re.sub('[^a-z]','x', name)
- return tempfile.mktemp ('%s.errorlog' % name)
-
-def read_pipe (cmd, mode = 'r'):
-
-
- redirect = ''
- error_log_file = ''
- if __main__.verbose_p:
- progress (_ ("Opening pipe `%s\'") % cmd)
- else:
- error_log_file = error_log (command_name (cmd))
- redirect = ' 2>%s' % error_log_file
-
- pipe = os.popen (cmd + redirect, mode)
- output = pipe.read ()
- status = pipe.close ()
- # successful pipe close returns 'None'
- if not status:
- status = 0
- signal = 0x0f & status
- exit_status = status >> 8
-
- if status:
- error (_ ("`%s\' failed (%d)") % (cmd, exit_status))
-
- if not __main__.verbose_p:
- contents = open (error_log_file).read ()
- if contents:
- error (_ ("The error log is as follows:"))
- sys.stderr.write (contents)
-
- # Ugh. code dup
- if error_log_file:
- os.unlink (error_log_file)
-
- exit (1)
-
- if __main__.verbose_p:
- progress ('\n')
-
- if error_log_file:
- os.unlink (error_log_file)
-
- return output
-
-def system (cmd, ignore_error = 0, progress_p = 0):
-
- '''System CMD. If IGNORE_ERROR, do not complain when CMD
-returns non zero. If PROGRESS_P, always show progress.
-
-RETURN VALUE
-
-Exit status of CMD '''
-
- name = command_name (cmd)
- error_log_file = ''
-
- if __main__.verbose_p:
- progress_p = 1
- progress (_ ("Invoking `%s\'") % cmd)
- else:
- progress ( _("Running %s...") % name)
-
- redirect = ''
- if not progress_p:
- error_log_file = error_log (name)
- redirect = ' 1>/dev/null 2>' + error_log_file
- elif __main__.pseudo_filter_p:
- redirect = ' 1>/dev/null'
-
- status = os.system (cmd + redirect)
- signal = 0x0f & status
- exit_status = status >> 8
-
- if status:
-
- exit_type = 'status %d' % exit_status
- if signal:
- exit_type = 'signal %d' % signal
-
- msg = _ ("`%s\' failed (%s)") % (name, exit_type)
- if ignore_error:
- if __main__.verbose_p:
- warning (msg + ' ' + _ ("(ignored)"))
- else:
- error (msg)
- if not progress_p and error_log_file:
- error (_ ("The error log is as follows:"))
- sys.stderr.write (open (error_log_file).read ())
- if error_log_file:
- os.unlink (error_log_file)
- exit (1)
-
- if error_log_file:
- os.unlink (error_log_file)
- progress ('\n')
- return status
-
-def cleanup_temp ():
- if not __main__.keep_temp_dir_p:
- if __main__.verbose_p:
- progress (_ ("Cleaning %s...") % __main__.temp_dir)
- shutil.rmtree (__main__.temp_dir)
- if __main__.verbose_p:
- progress ('\n')