]> git.donarmstrong.com Git - lilypond.git/blobdiff - python/lilylib.py
* scripts/lilypond-book.py (is_derived_class): use numbers
[lilypond.git] / python / lilylib.py
index 67485321f750dc4b9765cb0f7c95aad922f9a16c..7bac5a445f256ee8e3746aa68b542ff3abd00e93 100644 (file)
@@ -16,6 +16,7 @@ import shutil
 import string
 import sys
 import tempfile
+import glob
 
 ################################################################
 # Users of python modules should include this snippet
@@ -28,6 +29,7 @@ import tempfile
 
 # If set, LILYPONDPREFIX must take prevalence
 # if datadir is not set, we're doing a build and LILYPONDPREFIX
+
 import getopt, os, sys
 datadir = '@local_lilypond_datadir@'
 if not os.path.isdir (datadir):
@@ -58,7 +60,7 @@ if __name__ == '__main__':
        help_summary = _ ("lilylib module")
 
        option_definitions = [
-               ('', 'h', 'help', _ ("this help")),
+               ('', 'h', 'help', _ ("print this help")),
                ]
 
        from lilylib import *
@@ -99,12 +101,8 @@ except:
                return s
 underscore = _
 
-program_version = '@TOPLEVEL_VERSION@'
-if program_version == '@' + 'TOPLEVEL_VERSION' + '@':
-       program_version = '1.7.14'
-
 def identify (port):
-       port.write ('%s (GNU LilyPond) %s\n' % (__main__.program_name, program_version))
+       port.write ('%s (GNU LilyPond) %s\n' % (__main__.program_name, __main__.program_version))
 
 def warranty ():
        identify (sys.stdout)
@@ -155,9 +153,9 @@ def option_help_str (o):
        if o[1]:
                sh = '-%s' % o[1]
 
-       sep = ' '
+       sep = '  '
        if o[1] and o[2]:
-               sep = ','
+               sep = ', '
                
        long = ''
        if o[2]:
@@ -189,7 +187,7 @@ def options_help_str (opts):
        return str
 
 def help ():
-       ls = [(_ ("Usage: %s [OPTION]... FILE") % __main__.program_name),
+       ls = [(_ ("Usage: %s [OPTIONS]... FILE") % __main__.program_name),
              ('\n\n'),
              (__main__.help_summary),
              ('\n\n'),
@@ -197,9 +195,30 @@ def help ():
              ('\n'),
              (options_help_str (__main__.option_definitions)),
              ('\n\n'),
-             (_ ("Report bugs to %s") % 'bug-lilypond@gnu.org'),
+             (_ ("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 ():
        
@@ -215,16 +234,23 @@ def setup_temp ():
        return __main__.temp_dir
 
 def command_name (cmd):
-       return re.match ('^[ \t]*([^ \t]*)', cmd).group (1)
+       return re.match ('^[^a-z0-9_-]*([a-z0-9_-]*)', cmd).group (1)
 
 def error_log (name):
-       return os.path.join (__main__.temp_dir, '%s.errorlog' % 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)
-               redirect = ' 2>%s' % error_log (command_name (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 ()
@@ -236,12 +262,25 @@ def read_pipe (cmd, mode = 'r'):
 
        if status:
                error (_ ("`%s\' failed (%d)") % (cmd, exit_status))
+               
                if not __main__.verbose_p:
-                       error (_ ("The error log is as follows:"))
-                       sys.stderr.write (open (error_log (command_name (cmd)).read ()))
+                       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 (status)
+               
        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):
@@ -254,7 +293,8 @@ RETURN VALUE
 Exit status of CMD '''
 
        name = command_name (cmd)
-
+       error_log_file = ''
+       
        if __main__.verbose_p:
                progress_p = 1
                progress (_ ("Invoking `%s\'") % cmd)
@@ -263,10 +303,11 @@ Exit status of CMD '''
 
        redirect = ''
        if not progress_p:
-               redirect = ' 1>/dev/null 2>' + error_log (name)
+               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
@@ -283,11 +324,15 @@ Exit status of CMD '''
                                warning (msg + ' ' + _ ("(ignored)"))
                else:
                        error (msg)
-                       if not progress_p:
+                       if not progress_p and error_log_file:
                                error (_ ("The error log is as follows:"))
-                               sys.stderr.write (open (error_log (name)).read ())
+                               sys.stderr.write (error_log_file).read ()
+                       if error_log_file:
+                               os.unlink (error_log_file)
                        exit (status)
 
+       if error_log_file:
+               os.unlink (error_log_file)
        progress ('\n')
        return status
 
@@ -309,8 +354,10 @@ def strip_extension (f, ext):
 
 def cp_to_dir (pattern, dir):
        "Copy files matching re PATTERN from cwd to DIR"
+       
        # Duh.  Python style portable: cp *.EXT OUTDIR
        # system ('cp *.%s %s' % (ext, outdir), 1)
+
        files = filter (lambda x, p=pattern: re.match (p, x), os.listdir ('.'))
        map (lambda x, d=dir: shutil.copy2 (x, os.path.join (d, x)), files)
 
@@ -319,6 +366,7 @@ def cp_to_dir (pattern, dir):
 #
 # On most platforms, this is equivalent to
 #`normpath(join(os.getcwd()), PATH)'.  *Added in Python version 1.5.2*
+
 if os.path.__dict__.has_key ('abspath'):
        abspath = os.path.abspath
 else:
@@ -358,6 +406,7 @@ def setup_environment ():
                #       * clean TEXINPUTS, MFINPUTS, TFMFONTS,
                #         as these take prevalence over $TEXMF
                #         and thus may break tex run?
+               
                'TEXMF' : "{%s,%s}" % (datadir, texmf) ,
                'GS_FONTPATH' : type1_paths,
                'GS_LIB' : datadir + '/ps',
@@ -396,49 +445,62 @@ def get_bbox (filename):
        
        return gr
 
-def make_preview (name):
-       ## ly2dvi/lilypond-book discrepancy
-       preview_ps = name + '.preview.ps'
-       if not os.path.isfile (preview_ps):
-               preview_ps = name + '.eps'
-       bbox = get_bbox (preview_ps)
-       trans_ps = name + '.trans.ps'
-       png = name + '.png'
+
+def make_ps_images (ps_name, resolution = 90):
+       ## todo:
+       ## have better algorithm for deciding when to crop page,
+       ## and when to show full page
+       base = re.sub (r'\.e?ps', '', ps_name)
        
-       margin = 0
-       fo = open (trans_ps, 'w')
-       fo.write ('%d %d translate\n' % (-bbox[0] + margin,
-                                        -bbox[1] + margin))
-       fo.close ()
+       header = open (ps_name).read (1024)
+       multi_page = re.search ('\n%%Pages: ', header)
+       cmd = ''
+
+       if multi_page == None:
+               bbox = get_bbox (ps_name)
+               trans_ps = ps_name + '.trans.ps'
+               output_file = re.sub (r'\.e?ps', '.png', ps_name)
        
-       x = (2* margin + bbox[2] - bbox[0]) \
-           * __main__.preview_resolution / 72.0
-       y = (2* margin + bbox[3] - bbox[1]) \
-           * __main__.preview_resolution / 72.0
-       if x == 0:
-               x = 1
-       if y == 0:
-               y = 1
-
-       cmd = r'''gs -g%dx%d -sDEVICE=pnggray  -dTextAlphaBits=4 -dGraphicsAlphaBits=4  -q -sOutputFile=%s -r%d -dNOPAUSE %s %s -c quit ''' % \
-             (x, y, png, __main__.preview_resolution, trans_ps, preview_ps)
+
+
+               # need to have margin, otherwise edges of letters will
+               # be cropped off.
+
+               margin = 3 
+               fo = open (trans_ps, 'w')
+               fo.write ('%d %d translate\n' % (-bbox[0] + margin,
+                                                -bbox[1] + margin))
+               fo.close ()
+
+               x = (2* margin + bbox[2] - bbox[0]) \
+                   * resolution / 72.0
+               y = (2* margin + bbox[3] - bbox[1]) \
+                   * resolution / 72.0
+               if x == 0:
+                       x = 1
+               if y == 0:
+                       y = 1
+
+               cmd = r'''gs -g%dx%d -sDEVICE=pnggray  -dTextAlphaBits=4 -dGraphicsAlphaBits=4  -q -sOutputFile=%s -r%d -dNOPAUSE %s %s -c quit ''' % \
+                     (x, y, output_file, resolution, trans_ps, ps_name)
+
+               rms = glob.glob (base + '-page*.png')
+               map (os.unlink, rms)
+       else:
+               output_file = re.sub (r'\.e?ps', '-page%d.png', ps_name)
+               rmfile = base + '.png'
+               if os.path.isfile (rmfile):
+                       os.unlink (rmfile)
+               
+               cmd = r'''gs -s  -sDEVICE=pnggray  -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -q -sOutputFile=%s -dNOPAUSE -r%d %s -c quit''' % (output_file,
+                                                                                                                                     resolution, ps_name)
        
        status = system (cmd)
        signal = 0xf & status
        exit_status = status >> 8
+
        
        if status:
                os.unlink (png)
                error (_ ("Removing output file"))
                exit (1)
-
-def make_page_images (name, resolution = 90):
-
-       """ Generate images for
-       all pages in the PS file NAME. NAME should be the basename
-       (not including the extension.).
-       """
-       
-       cmd = 'gs -sDEVICE=pnggray -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sOutputFile="%s-page%%d.png" -r%d -dNOPAUSE %s -c quit'
-       cmd = cmd % (name, resolution, name + '.ps')
-       system (cmd)