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,
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.
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)
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
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:
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, ':')
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))
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:
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 ('''
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):
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))
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]:
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)
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
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
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
# 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):
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!
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)
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)
'include',
'lilypond',
)
- ly.progress (_ ("Dissecting..."))
+ progress (_ ("Dissecting..."))
chunks = find_toplevel_snippets (source, snippet_types)
if global_options.format == LATEX:
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])
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,
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 ():
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)
files = do_options ()
if not files or len (files) > 1:
ly.help ()
- ly.exit (2)
+ exit (2)
file = files[0]
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 ()