]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/bib2texi.py
Removes mutopia-index.py processing
[lilypond.git] / scripts / build / bib2texi.py
1 #!@PYTHON@
2 import os
3 import sys
4 import getopt
5 import tempfile
6
7 # usage:
8 def usage ():
9     print 'usage: %s [-s style] [-o <outfile>] [-q] BIBFILES...'
10     print '-q suppresses most output'
11
12 (options, files) = getopt.getopt (sys.argv[1:], 's:o:hq', [])
13
14 output = 'bib.itexi'
15 style = 'long'
16 show_output = True
17
18 for (o,a) in options:
19     if o == '-h' or o == '--help':
20         usage ()
21         sys.exit (0)
22     elif o == '-s' or o == '--style':
23         style = a
24     elif o == '-o' or o == '--output':
25         output = a
26     elif o == '-q':
27         show_output = False
28     else:
29         raise Exception ('unknown option: %s' % o)
30
31 if not files:
32    usage ()
33    sys.exit (2)
34
35
36 def strip_extension (f, ext):
37     (p, e) = os.path.splitext (f)
38     if e == ext:
39         e = ''
40     return p + e
41
42 nf = []
43 for f in files:
44     nf.append (strip_extension (f, '.bib'))
45
46 files = ','.join (nf)
47
48 tmpfile = tempfile.mkstemp ('bib2texi')[1]
49
50 #This writes a .aux file to the temporary directory.
51 #The .aux file contains the commands for bibtex
52 #PEH changed the bibstyle to allow a single template file in the parent directory
53 #The template filename is texi-*.bst, where * defaults to 'long' but can be a parameter
54 open (tmpfile + '.aux', 'w').write (r'''
55 \relax
56 \citation{*}
57 \bibstyle{%(style)s}
58 \bibdata{%(files)s}''' % vars ())
59
60 tmpdir = tempfile.gettempdir ()
61
62 if (show_output):
63     quiet_flag = ''
64 else:
65     quiet_flag = ' -terse '
66
67 #The command line to invoke bibtex
68 cmd = "TEXMFOUTPUT=%s bibtex %s %s" % (tmpdir, quiet_flag, tmpfile)
69
70 if (show_output):
71     sys.stdout.write ("Running bibtex on %s\n" % files)
72 #And invoke it
73 stat = os.system (cmd)
74 if stat <> 0:
75     sys.exit(1)
76
77 #TODO: do tex -> itexi on output
78 # Following 2 lines copy tmpfile.bbl to the desired output file
79 bbl = open (tmpfile + '.bbl').read ()
80
81 open (output, 'w').write  (bbl)
82
83 def cleanup (tmpfile):
84     for a in ['aux','bbl', 'blg']:
85         os.unlink (tmpfile + '.' + a)
86
87
88 cleanup (tmpfile)
89 #Following line added by PEH - script was leaving a dangling temporary file with no extension
90 os.unlink (tmpfile)
91