- Read initialization file(s)
- """
- varTable = [
- # name set method
- # ---- ----------
- ( 'DEBUG', this.setDebug ),
- ( 'DEPENDENCIES', this.setDependencies ),
- ( 'KEEPLILYPOND', this.setKeeplilypond ),
- ( 'KEEPLY2DVI', this.setKeeply2dvi ),
- ( 'LANGUAGE', this.setLanguage ),
- ( 'LATEXHF', this.setHeader ),
- ( 'LILYINCLUDE', this.setInclude ),
- ( 'LILYPONDPREFIX', this.setRoot ),
- ( 'NONUMBER', this.setNonumber ),
- ( 'ORIENTATION', this.setOrientation ),
- ( 'OUTPUTDIR', this.setOutput ),
- ( 'PAPERSIZE', this.setPaperZize ),
- ( 'PHEIGHT', this.setTextHeight ),
- ( 'POSTSCRIPT', this.setPostscript ),
- ( 'PWIDTH', this.setLineWidth ),
- ( 'SEPARATE', this.setSeparate ),
- ( 'TMP', this.setTmp ),
- ]
-
- if ( os.name == 'posix' ):
- dotFilename='.lilyrc'
- else: # Windows apps like edit choke on .lilyrc
- dotFilename='_lilyrc'
-
- for d in [os.path.join(this.get('root'),'ly'), \
- os.environ['HOME'], os.curdir ]:
- file=os.path.join(d,dotFilename)
- try:
- fd = open( file, 'r' )
- except:
- continue
-
- for line in fd.readlines():
- if re.match('#.*',line):
- continue
- m=re.search('([\w]+)=(.*)',line)
+
+ if verbose_p:
+ progress (_ ("Invoking `%s\'") % cmd)
+
+ st = os.system (cmd)
+ if st:
+ name = re.match ('[ \t]*([^ \t]*)', cmd).group (1)
+ msg = name + ': ' + _ ("command exited with value %d") % st
+ if ignore_error:
+ if not quiet:
+ warning (msg + ' ' + _ ("(ignored)") + ' ')
+ else:
+ error (msg)
+
+ return st
+
+
+def cleanup_temp ():
+ if not keep_temp_dir_p:
+ if verbose_p:
+ progress (_ ("Cleaning %s...") % temp_dir)
+ shutil.rmtree (temp_dir)
+
+
+def strip_extension (f, ext):
+ (p, e) = os.path.splitext (f)
+ if e == ext:
+ e = ''
+ 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)
+
+
+# Python < 1.5.2 compatibility
+#
+# 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:
+ def abspath (path):
+ return os.path.normpath (os.path.join (os.getcwd (), path))
+
+if os.__dict__.has_key ('makedirs'):
+ makedirs = os.makedirs
+else:
+ def makedirs (dir, mode=0777):
+ system ('mkdir -p %s' % dir)
+
+
+def mkdir_p (dir, mode=0777):
+ if not os.path.isdir (dir):
+ makedirs (dir, mode)
+
+
+# if set, LILYPONDPREFIX must take prevalence
+# if datadir is not set, we're doing a build and LILYPONDPREFIX
+datadir = '@local_lilypond_datadir@'
+
+if os.environ.has_key ('LILYPONDPREFIX') :
+ datadir = os.environ['LILYPONDPREFIX']
+else:
+ datadir = '@local_lilypond_datadir@'
+
+
+while datadir[-1] == os.sep:
+ datadir= datadir[:-1]
+
+sys.path.insert (0, os.path.join (datadir, 'python'))
+
+################################################################
+# END Library
+
+
+program_name = 'ly2dvi'
+
+original_dir = os.getcwd ()
+temp_dir = os.path.join (original_dir, '%s.dir' % program_name)
+errorport = sys.stderr
+keep_temp_dir_p = 0
+verbose_p = 0
+preview_p = 0
+preview_resolution = 90
+pseudo_filter_p = 0
+
+help_summary = _ ("Generate .dvi with LaTeX for LilyPond")
+
+option_definitions = [
+ ('', 'd', 'dependencies', _ ("write Makefile dependencies for every input file")),
+ ('', 'h', 'help', _ ("this help")),
+ (_ ("DIR"), 'I', 'include', _ ("add DIR to LilyPond's search path")),
+ ('', 'k', 'keep', _ ("keep all output, and name the directory %s.dir") % program_name),
+ ('', '', 'no-lily', _ ("don't run LilyPond")),
+ ('', 'm', 'no-paper', _ ("produce MIDI output only")),
+ (_ ("FILE"), 'o', 'output', _ ("write ouput to FILE")),
+ (_ ("FILE"), 'f', 'find-pfa', _ ("find pfa fonts used in FILE")),
+ # why capital P?
+ ('', '', 'preview', _("Make a picture of the first system.")),
+ (_ ('RES'), '', 'preview-resolution', _("Set the resolution of the preview to RES.")),
+ ('', 'P', 'postscript', _ ("generate PostScript output")),
+ ('', 'p', 'pdf', _ ("generate PDF output")),
+ (_ ("KEY=VAL"), 's', 'set', _ ("change global setting KEY to VAL")),
+ ('', 'V', 'verbose', _ ("verbose")),
+ ('', 'v', 'version', _ ("print version number")),
+ ('', 'w', 'warranty', _ ("show warranty and copyright")),
+ ]
+
+layout_fields = ['dedication', 'title', 'subtitle', 'subsubtitle',
+ 'footer', 'head', 'composer', 'arranger', 'instrument',
+ 'opus', 'piece', 'metre', 'meter', 'poet', 'texttranslator']
+
+
+# init to empty; values here take precedence over values in the file
+
+## TODO: change name.
+extra_init = {
+ 'language' : [],
+ 'latexheaders' : [],
+ 'latexpackages' : ['geometry'],
+ 'latexoptions' : [],
+ 'papersize' : [],
+ 'pagenumber' : [1],
+ 'textheight' : [],
+ 'linewidth' : [],
+ 'orientation' : [],
+ 'unit' : ['pt'],
+}
+
+extra_fields = extra_init.keys ()
+fields = layout_fields + extra_fields
+
+include_path = ['.']
+lily_p = 1
+paper_p = 1
+
+output_name = ''
+
+# List of output formats that ly2dvi should create
+# Most advanced format first.
+targets = ['DVI', 'LATEX', 'MIDI', 'TEX']
+
+track_dependencies_p = 0
+dependency_files = []
+
+
+
+kpse = os.popen ('kpsexpand \$TEXMF').read()
+kpse = re.sub('[ \t\n]+$','', kpse)
+type1_paths = os.popen ('kpsewhich -expand-path=\$T1FONTS').read ()
+
+environment = {
+ # TODO: * prevent multiple addition.
+ # * clean TEXINPUTS, MFINPUTS, TFMFONTS,
+ # as these take prevalence over $TEXMF
+ # and thus may break tex run?
+ 'TEXMF' : "{%s,%s}" % (datadir, kpse) ,
+ 'GS_FONTPATH' : type1_paths,
+ 'GS_LIB' : datadir + '/ps',
+}
+
+# tex needs lots of memory, more than it gets by default on Debian
+non_path_environment = {
+ 'extra_mem_top' : '1000000',
+ 'extra_mem_bottom' : '1000000',
+ 'pool_size' : '250000',
+}
+
+def setup_environment ():
+ # $TEXMF is special, previous value is already taken care of
+ if os.environ.has_key ('TEXMF'):
+ del os.environ['TEXMF']
+
+ for key in environment.keys ():
+ val = environment[key]
+ if os.environ.has_key (key):
+ val = os.environ[key] + os.pathsep + val
+ os.environ[key] = val
+
+ for key in non_path_environment.keys ():
+ val = non_path_environment[key]
+ os.environ[key] = val
+
+#what a name.
+def set_setting (dict, key, val):
+ try:
+ val = string.atoi (val)
+ except ValueError:
+ #warning (_ ("invalid value: %s") % `val`)
+ pass
+
+ if type(val) == type ('hoi'):
+ try:
+ val = string.atof (val)
+ except ValueError:
+ #warning (_ ("invalid value: %s") % `val`)
+ pass
+
+ try:
+ dict[key].append (val)
+ except KeyError:
+ warning (_ ("no such setting: %s") % `key`)
+ dict[key] = [val]
+
+
+def print_environment ():
+ for (k,v) in os.environ.items ():
+ sys.stderr.write ("%s=\"%s\"\n" % (k,v))
+
+def quiet_system (cmd, name, ignore_error = 0):
+ if not verbose_p:
+ progress ( _("Running %s...") % name)
+ cmd = cmd + ' 1> /dev/null 2> /dev/null'
+ elif pseudo_filter_p:
+ cmd = cmd + ' 1> /dev/null'
+
+ return system (cmd, ignore_error, quiet = 1)
+
+
+def run_lilypond (files, dep_prefix):
+
+ opts = ''
+ opts = opts + ' ' + string.join (map (lambda x : '-I ' + x,
+ include_path))
+ if pseudo_filter_p:
+ opts = opts + ' --output=lelie'
+ if paper_p:
+ opts = opts + ' ' + string.join (map (lambda x : '-H ' + x,
+ fields))
+ else:
+ opts = opts + ' --no-paper'
+
+ if track_dependencies_p:
+ opts = opts + " --dependencies"
+ if dep_prefix:
+ opts = opts + ' --dep-prefix=%s' % dep_prefix
+
+ fs = string.join (files)
+
+ if not verbose_p:
+ # cmd = cmd + ' 1> /dev/null 2> /dev/null'
+ progress ( _("Running %s...") % 'LilyPond')
+ else:
+ opts = opts + ' --verbose'
+
+ # for better debugging!
+ print_environment ()
+
+ cmd = 'lilypond %s %s ' % (opts, fs)
+ if verbose_p:
+ progress ("Invoking `%s'"% cmd)
+ status = os.system (cmd)
+
+ signal = 0x0f & status
+ exit_status = status >> 8
+
+ # 2 == user interrupt.
+ if signal and signal <> 2:
+ error("\n\nLilyPond crashed (signal %d). Please submit a bugreport to bug-lilypond@gnu.org\n" % signal)
+
+ if status:
+ error ("\n\nLilyPond failed on the input file. (exit status %d)\n" % exit_status)
+
+
+def analyse_lilypond_output (filename, extra):
+
+ # urg
+ '''Grep FILENAME for interesting stuff, and
+ put relevant info into EXTRA.'''
+ filename = filename+'.tex'
+ progress (_ ("Analyzing %s...") % filename)
+ s = open (filename).read ()
+
+ # search only the first 10k
+ s = s[:10240]
+ for x in extra_fields:
+ m = re.search (r'\\def\\lilypondpaper%s{([^}]*)}'%x, s)