Skip to content

Instantly share code, notes, and snippets.

@haberman
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haberman/51de05fe7dc1c8e5b6e9 to your computer and use it in GitHub Desktop.
Save haberman/51de05fe7dc1c8e5b6e9 to your computer and use it in GitHub Desktop.
For building a upb amalgamator.
#!/usr/bin/python
import sys
import re
INCLUDE_RE = re.compile('^#include "([^"]*)"$')
def parse_include(line):
match = INCLUDE_RE.match(line)
return match.groups()[0] if match else None
class Amalgamator:
def __init__(self):
self.included = set()
self.output_h = open("upb.h", "w")
self.output_c = open("upb.c", "w")
self.output_c.write("// Amalgamated source file\n")
self.output_c.write('#include "upb.h"\n')
self.output_h.write("// Amalgamated source file\n")
def _process_file(self, infile_name, outfile):
for line in open(infile_name):
include = parse_include(line)
if include is not None and include.startswith("upb"):
if include not in self.included:
self.included.add(include)
self._add_header(include)
else:
outfile.write(line)
def _add_header(self, filename):
self._process_file(filename, self.output_h)
def add_src(self, filename):
self._process_file(filename, self.output_c)
amalgamator = Amalgamator()
for filename in sys.argv[1:]:
amalgamator.add_src(filename.strip())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment