9 print 'usage: bib2texi.py [-s style] [-o <outfile>] [-q] BIBFILES...'
10 print '-q suppresses most output'
12 (options, files) = getopt.getopt (sys.argv[1:], 's:o:hq', [])
19 if o == '-h' or o == '--help':
22 elif o == '-s' or o == '--style':
24 elif o == '-o' or o == '--output':
29 raise Exception ('unknown option: %s' % o)
35 marker = """@c This file was autogenerated
39 """ % (", ".join(files), sys.argv[0])
41 def strip_extension (f, ext):
42 (p, e) = os.path.splitext (f)
49 nf.append (strip_extension (f, '.bib'))
53 tmpfile = tempfile.mkstemp ('bib2texi')[1]
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'''
63 \bibdata{%(files)s}''' % vars ())
65 tmpdir = tempfile.gettempdir ()
70 quiet_flag = ' -terse '
72 #The command line to invoke bibtex
73 cmd = "TEXMFOUTPUT=%s bibtex %s %s" % (tmpdir, quiet_flag, tmpfile)
76 sys.stdout.write ("Running bibtex on %s\n" % files)
77 sys.stdout.write (cmd)
79 stat = os.system (cmd)
81 sys.stderr.write ("Bibtex exited with nonzero exit status!")
84 #TODO: do tex -> itexi on output
85 # Following lines copy tmpfile.bbl to the desired output file
86 bbl = open (tmpfile + '.bbl').read ()
88 if bbl.strip () == '':
89 sys.stderr.write ("Bibtex generated an empty file!")
92 fout = open (output, 'w')
97 def cleanup (tmpfile):
98 for a in ['aux','bbl', 'blg']:
99 os.unlink (tmpfile + '.' + a)
103 #Following line added by PEH - script was leaving a dangling temporary file with no extension