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