preprocess.py 889 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import sys
  2. import os
  3. def load_file(p):
  4. with open(p, 'r') as f:
  5. return f.read()
  6. def step(base, data, hh):
  7. def flt():
  8. for line in data.split('\n'):
  9. if line in hh:
  10. pp = os.path.join(base, hh[line])
  11. yield '\n\n' + load_file(pp) + '\n\n'
  12. os.unlink(pp)
  13. else:
  14. yield line
  15. return '\n'.join(flt())
  16. def subst_headers(path, headers):
  17. hh = dict()
  18. for h in headers:
  19. hh['# include "' + h + '"'] = h
  20. data = load_file(path)
  21. prev = data
  22. while True:
  23. ret = step(os.path.dirname(path), prev, hh)
  24. if ret == prev:
  25. break
  26. prev = ret
  27. if data != prev:
  28. with open(path, 'w') as f:
  29. f.write(prev)
  30. if __name__ == '__main__':
  31. subst_headers(sys.argv[1], ['stack.hh', 'position.hh', 'location.hh'])