]> git.donarmstrong.com Git - lilypond.git/commitdiff
* python/lilylib.py (ps_page_count): remove make_ps_images().
authorhanwen <hanwen>
Thu, 2 Mar 2006 19:55:59 +0000 (19:55 +0000)
committerhanwen <hanwen>
Thu, 2 Mar 2006 19:55:59 +0000 (19:55 +0000)
(ps_page_count): remove mkdir_p
(system): remove cleanup_temp.
(exit): remove lilypond_version_(check)
(error_log): remove pseudo_filter_p
remove read_pipe
remove cp_to_dir

* scripts/lilypond-book.py (Lilypond_snippet.compose_ly): don't
use __main__

ChangeLog
python/lilylib.py
scripts/lilypond-book.py

index 6050eb70f1d7ab82ec8338ccce07f9a9ca674240..3a8f95fdec299fcee60cfecc8e0a8568091af0f6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2006-03-02  Han-Wen Nienhuys  <hanwen@xs4all.nl>
 
+       * python/lilylib.py (ps_page_count): remove make_ps_images().
+       (ps_page_count): remove mkdir_p
+       (system): remove cleanup_temp.
+       (exit): remove lilypond_version_(check)
+       (error_log): remove pseudo_filter_p
+       remove read_pipe
+       remove cp_to_dir
+
        * scripts/lilypond-book.py (Lilypond_snippet.compose_ly): don't
        use __main__
 
index 8d51dd57eda071f94f31fb177faeb5a8aec8c4d1..165e1aef6c16f81e8822852e81481aa54c2c3c2d 100644 (file)
@@ -56,43 +56,6 @@ except:
                return s
 underscore = _
 
-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):
-       be_verbose = get_global_option('verbose_p', 'verbose')
-       if be_verbose:
-               raise _ ('Exiting (%d)...') % i
-       else:
-               sys.exit (i)
-       
-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 command_name (cmd):
        # Strip all stuf after command,
@@ -104,56 +67,8 @@ 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 be_verbose:
-               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 be_verbose:
-                       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 be_verbose:
-               progress ('\n')
-
-       if error_log_file:
-               os.unlink (error_log_file)
-               
-       return output
-
-def get_global_option (old, new):
-       try:
-               return __main__.__dict__[old]
-       except KeyError:
-               return __main__.global_options.__dict__[new]
-
-def system (cmd, ignore_error = 0, progress_p = 0):
+def system (cmd, ignore_error = 0, progress_p = 0, be_verbose=0):
        
        '''System CMD.  If IGNORE_ERROR, do not complain when CMD
 returns non zero.  If PROGRESS_P, always show progress.
@@ -165,10 +80,6 @@ Exit status of CMD '''
        name = command_name (cmd)
        error_log_file = ''
 
-       ## UGH
-       be_verbose = get_global_option('verbose_p', 'verbose')
-       pseudo_filter = get_global_option ('pseudo_filter_p', 'pseudo_filter')
-       
        if be_verbose:
                progress_p = 1
                progress (_ ("Invoking `%s\'") % cmd)
@@ -179,8 +90,6 @@ Exit status of CMD '''
        if not progress_p:
                error_log_file = error_log (name)
                redirect = ' 1>/dev/null 2>' + error_log_file
-       elif pseudo_filter:
-               redirect = ' 1>/dev/null'
 
        status = os.system (cmd + redirect)
        signal = 0x0f & status
@@ -210,16 +119,6 @@ Exit status of CMD '''
        progress ('\n')
        return status
 
-def cleanup_temp ():
-       be_verbose = get_global_option('verbose_p', 'verbose')
-       if not __main__.keep_temp_dir_p:
-               if be_verbose:
-                       progress (_ ("Cleaning %s...") % __main__.temp_dir)
-               shutil.rmtree (__main__.temp_dir)
-               if be_verbose:
-                       progress ('\n')
-
-
 def strip_extension (f, ext):
        (p, e) = os.path.splitext (f)
        if e == ext:
@@ -227,16 +126,6 @@ def strip_extension (f, ext):
        return p + e
 
 
-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)
-
-
 def search_exe_path (name):
        p = os.environ['PATH']
        exe_paths = string.split (p, ':')
@@ -247,10 +136,6 @@ def search_exe_path (name):
        return None
 
 
-def mkdir_p (dir, mode=0777):
-       if not os.path.isdir (dir):
-               makedirs (dir, mode)
-
 def print_environment ():
        for (k,v) in os.environ.items ():
                sys.stderr.write ("%s=\"%s\"\n" % (k, v)) 
@@ -263,66 +148,6 @@ def ps_page_count (ps_name):
                return string.atoi (m.group (1))
        return 0
 
-def make_ps_images (ps_name, resolution = 90, papersize = "a4",
-                   rename_page1_p = 0):
-       base = os.path.basename (re.sub (r'\.e?ps', '', ps_name))
-       header = open (ps_name).read (1024)
-
-       png1 = base + '.png'
-       pngn = base + '-page%d.png'
-       output_file = pngn
-       multi_page = re.search ('\n%%Pages: ', header)
-
-       # png16m is because Lily produces color nowadays.
-       if not multi_page:
-
-               # GS can produce empty 2nd page if pngn is used.
-               output_file = png1
-               cmd = r'''gs\
-               -dEPSCrop\
-               -dGraphicsAlphaBits=4\
-               -dNOPAUSE\
-               -dTextAlphaBits=4\
-               -sDEVICE=png16m\
-               -sOutputFile='%(output_file)s'\
-               -sPAPERSIZE=%(papersize)s\
-               -q\
-               -r%(resolution)d\
-               '%(ps_name)s'\
-               -c showpage\
-               -c quit ''' % vars ()
-       else:
-               cmd = r'''gs\
-               -s\
-               -dGraphicsAlphaBits=4\
-               -dNOPAUSE\
-               -dTextAlphaBits=4\
-               -sDEVICE=png16m\
-               -sOutputFile='%(output_file)s'\
-               -sPAPERSIZE=%(papersize)s\
-               -q\
-               -r%(resolution)d\
-               '%(ps_name)s'\
-               -c quit''' % vars ()
-
-       remove = glob.glob (png1) + glob.glob (base + '-page*.png')
-       map (os.unlink, remove)
-
-       status = system (cmd)
-       signal = 0xf & status
-       exit_status = status >> 8
-
-       if status:
-               remove = glob.glob (png1) + glob.glob (base + '-page*.png')
-               map (os.unlink, remove)
-               error (_ ("%s exited with status: %d") % ('GS', status))
-               exit (1)
-
-       if rename_page1_p and multi_page:
-               os.rename (pngn % 1, png1)
-       files = glob.glob (png1) + glob.glob (re.sub ('%d', '*', pngn))
-       return files
-
 class NonDentedHeadingFormatter (optparse.IndentedHelpFormatter):
     def format_heading(self, heading):
            if heading:
index 48a16f49c365d36f8b52067e561d9b97d49e95b8..cf2008deccd71c6b6abb2d9cefdd3cd7cb11d989 100644 (file)
@@ -97,10 +97,25 @@ Example usage:
 authors = ('Jan Nieuwenhuizen <janneke@gnu.org>',
             'Han-Wen Nienhuys <hanwen@cs.uu.nl>')
 
+       
+################################################################
+def exit (i):
+       if global_options.verbose:
+               raise _ ('Exiting (%d)...') % i
+       else:
+               sys.exit (i)
 
 def identify ():
        sys.stdout.write ('%s (GNU LilyPond) %s\n' % (program_name, program_version))
 
+progress=sys.stderr.write
+
+def warning (s):
+       sys.stderr.write (program_name + ": " + _ ("warning: %s") % s + '\n')
+
+def error (s):
+       sys.stderr.write (program_name + ": " + _ ("error: %s") % s + '\n')
+
 def warranty ():
        identify ()
        sys.stdout.write ('''
@@ -706,8 +721,8 @@ def find_file (name):
                if os.path.exists (full):
                        return full
                
-       ly.error (_ ("file not found: %s") % name + '\n')
-       ly.exit (1)
+       error (_ ("file not found: %s") % name + '\n')
+       exit (1)
        return ''
 
 def verbatim_html (s):
@@ -939,17 +954,17 @@ class Lilypond_snippet (Snippet):
                          classic_lilypond_book_compatibility (key, value)
                        if c_key:
                                if c_value:
-                                       ly.warning \
+                                       warning \
                                          (_ ("deprecated ly-option used: %s=%s" \
                                            % (key, value)))
-                                       ly.warning \
+                                       warning \
                                          (_ ("compatibility mode translation: %s=%s" \
                                            % (c_key, c_value)))
                                else:
-                                       ly.warning \
+                                       warning \
                                          (_ ("deprecated ly-option used: %s" \
                                            % key))
-                                       ly.warning \
+                                       warning \
                                          (_ ("compatibility mode translation: %s" \
                                            % c_key))
 
@@ -969,7 +984,7 @@ class Lilypond_snippet (Snippet):
                                        break
 
                        if not found and key not in simple_options:
-                               ly.warning (_ ("ignoring unknown ly option: %s") % key)
+                               warning (_ ("ignoring unknown ly option: %s") % key)
 
                # URGS
                if RELATIVE in override.keys () and override[RELATIVE]:
@@ -1304,7 +1319,7 @@ def find_toplevel_snippets (s, types):
 
 def filter_pipe (input, cmd):
        if global_options.verbose:
-               ly.progress (_ ("Opening filter `%s'") % cmd)
+               progress (_ ("Opening filter `%s'") % cmd)
 
        (stdin, stdout, stderr) = os.popen3 (cmd)
        stdin.write (input)
@@ -1321,14 +1336,14 @@ def filter_pipe (input, cmd):
        signal = 0x0f & status
        if status or (not output and error):
                exit_status = status >> 8
-               ly.error (_ ("`%s' failed (%d)") % (cmd, exit_status))
-               ly.error (_ ("The error log is as follows:"))
+               error (_ ("`%s' failed (%d)") % (cmd, exit_status))
+               error (_ ("The error log is as follows:"))
                sys.stderr.write (error)
                sys.stderr.write (stderr.read ())
-               ly.exit (status)
+               exit (status)
 
        if global_options.verbose:
-               ly.progress ('\n')
+               progress ('\n')
 
        return output
 
@@ -1357,7 +1372,7 @@ def process_snippets (cmd, ly_snippets, texstr_snippets, png_snippets):
                                    ignore_error = 1, progress_p = 1)
 
                if status:
-                       ly.error ('Process %s exited unsuccessfully.' % cmd)
+                       error ('Process %s exited unsuccessfully.' % cmd)
                        raise Compile_error
 
        # UGH
@@ -1487,17 +1502,17 @@ def do_process_cmd (chunks, input_name):
                            and x.png_is_outdated (),
                  chunks)
 
-       ly.progress (_ ("Writing snippets..."))
+       progress (_ ("Writing snippets..."))
        map (Lilypond_snippet.write_ly, ly_outdated)
-       ly.progress ('\n')
+       progress ('\n')
 
        if ly_outdated:
-               ly.progress (_ ("Processing..."))
-               ly.progress ('\n')
+               progress (_ ("Processing..."))
+               progress ('\n')
                process_snippets (global_options.process_cmd, ly_outdated, texstr_outdated, png_outdated)
        else:
-               ly.progress (_ ("All snippets are up to date..."))
-       ly.progress ('\n')
+               progress (_ ("All snippets are up to date..."))
+       progress ('\n')
 
 def guess_format (input_filename):
        format = None
@@ -1506,9 +1521,9 @@ def guess_format (input_filename):
                # FIXME
                format = ext2format[e]
        else:
-               ly.error (_ ("can't determine format for: %s" \
+               error (_ ("can't determine format for: %s" \
                             % input_filename))
-               ly.exit (1)
+               exit (1)
        return format
 
 def write_if_updated (file_name, lines):
@@ -1517,15 +1532,15 @@ def write_if_updated (file_name, lines):
                oldstr = f.read ()
                new_str = string.join (lines, '')
                if oldstr == new_str:
-                       ly.progress (_ ("%s is up to date.") % file_name)
-                       ly.progress ('\n')
+                       progress (_ ("%s is up to date.") % file_name)
+                       progress ('\n')
                        return
        except:
                pass
 
-       ly.progress (_ ("Writing `%s'...") % file_name)
+       progress (_ ("Writing `%s'...") % file_name)
        open (file_name, 'w').writelines (lines)
-       ly.progress ('\n')
+       progress ('\n')
 
 def note_input_file (name, inputs=[]):
        ## hack: inputs is mutable!
@@ -1541,9 +1556,7 @@ def do_file (input_filename):
                if os.path.exists (input_filename):
                        input_fullname = input_filename
                elif global_options.format == LATEX and ly.search_exe_path ('kpsewhich'): 
-                       # urg python interface to libkpathsea?
-                       input_fullname = ly.read_pipe ('kpsewhich '
-                                                      + input_filename)[:-1]
+                       input_fullname = os.read_pipe ('kpsewhich ' + input_filename).read()[:-1]
                else:
                        input_fullname = find_file (input_filename)
 
@@ -1571,14 +1584,14 @@ def do_file (input_filename):
                        if os.path.exists (input_filename) \
                           and os.path.exists (output_filename) \
                           and os.path.samefile (output_filename, input_fullname):
-                          ly.error (
+                          error (
                           _ ("Output would overwrite input file; use --output."))
-                          ly.exit (2)
+                          exit (2)
 
        try:
-               ly.progress (_ ("Reading %s...") % input_fullname)
+               progress (_ ("Reading %s...") % input_fullname)
                source = in_handle.read ()
-               ly.progress ('\n')
+               progress ('\n')
 
                set_default_options (source)
 
@@ -1595,7 +1608,7 @@ def do_file (input_filename):
                        'include',
                        'lilypond',
                )
-               ly.progress (_ ("Dissecting..."))
+               progress (_ ("Dissecting..."))
                chunks = find_toplevel_snippets (source, snippet_types)
 
                if global_options.format == LATEX:
@@ -1604,15 +1617,15 @@ def do_file (input_filename):
                                    re.search (r"\\begin *{document}", c.replacement_text())):
                                        modify_preamble (c)
                                        break
-               ly.progress ('\n')
+               progress ('\n')
 
                if global_options.filter_cmd:
                        write_if_updated (output_filename,
                                          [c.filter_text () for c in chunks])
                elif global_options.process_cmd:
                        do_process_cmd (chunks, input_fullname)
-                       ly.progress (_ ("Compiling %s...") % output_filename)
-                       ly.progress ('\n')
+                       progress (_ ("Compiling %s...") % output_filename)
+                       progress ('\n')
                        write_if_updated (output_filename,
                                          [s.replacement_text ()
                                           for s in chunks])
@@ -1620,8 +1633,8 @@ def do_file (input_filename):
                def process_include (snippet):
                        os.chdir (original_dir)
                        name = snippet.substring ('filename')
-                       ly.progress (_ ("Processing include: %s") % name)
-                       ly.progress ('\n')
+                       progress (_ ("Processing include: %s") % name)
+                       progress ('\n')
                        return do_file (name)
 
                include_chunks = map (process_include,
@@ -1634,8 +1647,8 @@ def do_file (input_filename):
                
        except Compile_error:
                os.chdir (original_dir)
-               ly.progress (_ ("Removing `%s'") % output_filename)
-               ly.progress ('\n')
+               progress (_ ("Removing `%s'") % output_filename)
+               progress ('\n')
                raise Compile_error
 
 def do_options ():
@@ -1645,7 +1658,6 @@ def do_options ():
        if options.format in ('texi-html', 'texi'):
                options.format = TEXINFO
        options.use_hash = True
-       options.pseudo_filter = False
 
        options.include_path =  map (os.path.abspath, options.include_path)
 
@@ -1657,7 +1669,7 @@ def main ():
        files = do_options ()
        if not files or len (files) > 1:
                ly.help ()
-               ly.exit (2)
+               exit (2)
 
        file = files[0]
 
@@ -1690,29 +1702,29 @@ def main ():
 
                        psfonts_file = basename + '.psfonts' 
                        if not global_options.verbose:
-                               ly.progress (_ ("Writing fonts to %s...") % psfonts_file)
+                               progress (_ ("Writing fonts to %s...") % psfonts_file)
                        fontextract.extract_fonts (psfonts_file,
                                                   [x.basename() + '.eps'
                                                    for x in snippet_chunks])
                        if not global_options.verbose:
-                               ly.progress ('\n')
+                               progress ('\n')
                        
        except Compile_error:
-               ly.exit (1)
+               exit (1)
 
        if global_options.format in (TEXINFO, LATEX):
                if not global_options.psfonts:
-                       ly.warning (_ ("option --psfonts not used"))
-                       ly.warning (_ ("processing with dvips will have no fonts"))
+                       warning (_ ("option --psfonts not used"))
+                       warning (_ ("processing with dvips will have no fonts"))
 
                psfonts_file = os.path.join (global_options.output_name, basename + '.psfonts')
                output = os.path.join (global_options.output_name, basename +  '.dvi' )
                
-               ly.progress ('\n')
-               ly.progress (_ ("DVIPS usage:"))
-               ly.progress ('\n')
-               ly.progress ("    dvips -h %(psfonts_file)s %(output)s" % vars ())
-               ly.progress ('\n')
+               progress ('\n')
+               progress (_ ("DVIPS usage:"))
+               progress ('\n')
+               progress ("    dvips -h %(psfonts_file)s %(output)s" % vars ())
+               progress ('\n')
 
        inputs = note_input_file ('')
        inputs.pop ()