Skip to content

Instantly share code, notes, and snippets.

@d
Last active July 11, 2016 18:14
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 d/f3f86a8949aaa0319b98fd0057a9a357 to your computer and use it in GitHub Desktop.
Save d/f3f86a8949aaa0319b98fd0057a9a357 to your computer and use it in GitHub Desktop.
Eliminate `.inl` files
#!/usr/bin/env python3
# Usage:
# git grep -l '#include ".*\.inl"' | pypy3 inline.py -I libgpos/libgpos/include
# OR
# git grep -l '#include ".*\.inl"' | pypy3 inline.py -I libgpopt/include -I libnaucrates/include -I libgpdbcost/include -I server/include
import sys
import re
import os.path
import argparse
class Inliner:
def __init__(self, include_dirs):
self._include_dirs = list(include_dirs)
def inline(self, filename):
with open(filename) as f:
processed = list(self.process_lines(f, filename))
with open(filename, "w") as f:
f.writelines(processed)
def process_lines(self, f, header_filename):
for line in f:
if is_inline(line):
try:
inl_filename = self.extract_inl_name(line)
except:
raise RuntimeError(line)
inl_path = self.resolve_inl_path(inl_filename, header_filename)
with open(inl_path) as f:
yield f.read()
os.unlink(inl_path)
else:
yield line
@staticmethod
def extract_inl_name(line):
m = re.match('^#include "(.*\.inl)"$', line)
return m.group(1)
def resolve_inl_path(self, inl_filename, header_filename):
for dir in [os.path.dirname(header_filename)] + self._include_dirs:
path = os.path.join(dir, inl_filename)
if os.path.exists(path):
return path
raise IndexError(inl_filename)
def is_inline(line):
if re.match('^#include ".*\.inl"$', line):
return True
else:
return False
class ArgumentParser(argparse.ArgumentParser):
def __init__(self):
super().__init__()
self.add_argument("-I", dest="include_dirs", action="append", default=[])
def parse(self, args):
return self.parse_args(args)
def main():
parser = ArgumentParser()
include_dirs = parser.parse(sys.argv[1:]).include_dirs
inliner = Inliner(include_dirs)
for line in sys.stdin:
header = line.strip()
inliner.inline(header)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment