]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/bib2texi.py
Add '-dcrop' option to ps and svg backends
[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: bib2texi.py [-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 marker = """@c This file was autogenerated
36 @c     from: %s
37 @c     by:   %s
38
39 """ % (", ".join(files), sys.argv[0])
40
41 def strip_extension (f, ext):
42     (p, e) = os.path.splitext (f)
43     if e == ext:
44         e = ''
45     return p + e
46
47 nf = []
48 for f in files:
49     nf.append (strip_extension (f, '.bib'))
50
51 files = ','.join (nf)
52
53 tmpfile = tempfile.mkstemp ('bib2texi')[1]
54
55 #This writes a .aux file to the temporary directory.
56 #The .aux file contains the commands for bibtex
57 #PEH changed the bibstyle to allow a single template file in the parent directory
58 #The template filename is texi-*.bst, where * defaults to 'long' but can be a parameter
59 open (tmpfile + '.aux', 'w').write (r'''
60 \relax
61 \citation{*}
62 \bibstyle{%(style)s}
63 \bibdata{%(files)s}''' % vars ())
64
65 tmpdir = tempfile.gettempdir ()
66
67 if (show_output):
68     quiet_flag = ''
69 else:
70     quiet_flag = ' -terse '
71
72 #The command line to invoke bibtex
73 cmd = "TEXMFOUTPUT=%s bibtex %s %s" % (tmpdir, quiet_flag, tmpfile)
74
75 if (show_output):
76     sys.stdout.write ("Running bibtex on %s\n" % files)
77     sys.stdout.write (cmd)
78 #And invoke it
79 stat = os.system (cmd)
80 if stat <> 0:
81     sys.stderr.write ("Bibtex exited with nonzero exit status!")
82     sys.exit (1)
83
84 #TODO: do tex -> itexi on output
85 # Following lines copy tmpfile.bbl to the desired output file
86 bbl = open (tmpfile + '.bbl').read ()
87
88 if bbl.strip () == '':
89     sys.stderr.write ("Bibtex generated an empty file!")
90     sys.exit (1)
91
92 fout = open (output, 'w')
93 fout.write (marker)
94 fout.write (bbl)
95 fout.close ()
96
97 def cleanup (tmpfile):
98     for a in ['aux','bbl', 'blg']:
99         os.unlink (tmpfile + '.' + a)
100
101
102 cleanup (tmpfile)
103 #Following line added by PEH - script was leaving a dangling temporary file with no extension
104 os.unlink (tmpfile)
105