]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/bib2texi.py
Merge branch 'lilypond/translation'
[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
28 if style not in ['alpha','index','long','longp','long-pario','short','short-pario','split']:
29     sys.stderr.write ("Unknown style \`%s'\n" % style)
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 open (tmpfile + '.aux', 'w').write (r'''
51 \relax
52 \citation{*}
53 \bibstyle{texi-%(style)s}
54 \bibdata{%(files)s}''' % vars ())
55
56 tmpdir = tempfile.gettempdir ()
57
58 cmd = "TEXMFOUTPUT=%s bibtex %s" % (tmpdir, tmpfile)
59
60 sys.stdout.write ("Invoking `%s'\n" % cmd)
61 stat = os.system (cmd)
62 if stat <> 0:
63     sys.exit(1)
64
65
66 #TODO: do tex -> itexi on output
67
68 bbl = open (tmpfile + '.bbl').read ()
69
70 open (output, 'w').write  (bbl)
71
72
73 def cleanup (tmpfile):
74     for a in ['aux','bbl', 'blg']:
75         os.unlink (tmpfile + '.' + a)
76
77 cleanup (tmpfile)
78