]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/bib2texi.py
Doc-fr: update for 2.16.1 (first part)
[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     sys.stdout.write (cmd)
73 #And invoke it
74 stat = os.system (cmd)
75 if stat <> 0:
76     sys.stderr.write ("Bibtex exited with nonzero exit status!")
77     sys.exit (1)
78
79 #TODO: do tex -> itexi on output
80 # Following lines copy tmpfile.bbl to the desired output file
81 bbl = open (tmpfile + '.bbl').read ()
82
83 if bbl.strip () == '':
84     sys.stderr.write ("Bibtex generated an empty file!")
85     sys.exit (1)
86
87 open (output, 'w').write  (bbl)
88
89 def cleanup (tmpfile):
90     for a in ['aux','bbl', 'blg']:
91         os.unlink (tmpfile + '.' + a)
92
93
94 cleanup (tmpfile)
95 #Following line added by PEH - script was leaving a dangling temporary file with no extension
96 os.unlink (tmpfile)
97