#!/usr/bin/python # Merge in the bibliography for an xml2rfc input file # # Eric Rescorla # # Copyright (c) 2008, RTFM, INC. # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the RTFM, INC. nor the # names of its contributors may be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY RTFM, INC. ``AS IS'' AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL RTFM, INC. BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import sys import getopt import re import os from tempfile import mkstemp # Read the command line arguments getopt_out=getopt.getopt(sys.argv[1:],"v") options=getopt_out[0] arguments=getopt_out[1] if (len(arguments) < 1) or (len(arguments)>2): usage() # Figure out what files to work with infile = arguments[0] if len(arguments)==2: outfile = arguments[1] else: outfile = infile # Compile the regexes we'll need re_entitydecl = re.compile("ENTITY\s+bibxml2rfc-(n|inf)ormative") re_entityref = re.compile("&(bibxml2rfc-(informative|normative));") # Now process the file IN = open(infile,'r') # There does not seem to be a utility function for what we need, # namely a tempfile which persists past close [out_fd, tmpfile_name] = mkstemp(".xml",text=True) OUT = os.fdopen(out_fd, "w") for line in IN: if re_entitydecl.search(line): continue m = re_entityref.search(line) if m != None: rfile = infile + "-" + m.group(2) REF = open(rfile,"r") OUT.write("\n\n") for r in REF: OUT.write(r) OUT.write("\n\n") continue OUT.write(line) OUT.close() if infile == outfile: os.rename(infile, infile+".orig") os.rename(tmpfile_name, outfile) # Success sys.exit(0)