untempl.py 669 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #! /usr/bin/env python2.7
  2. import sys, re
  3. def readFile (fileName):
  4. fh = open(fileName, 'rb')
  5. x = fh.read()
  6. fh.close()
  7. return x
  8. def writeFile (fileName, x):
  9. fh = open(fileName, 'wb')
  10. fh.write(x)
  11. fh.close()
  12. _p = re.compile('\%\%([^\%]+)\%\%')
  13. def untempl (x):
  14. while True:
  15. r = _p.search(x)
  16. if r:
  17. fileName = r.group(1)
  18. y = readFile(fileName)
  19. toRepl = "%%%%%s%%%%" % (fileName,)
  20. x = x.replace(toRepl, y)
  21. else:
  22. break
  23. return x
  24. def main ():
  25. if len(sys.argv) != 3:
  26. print "usage: %s oldfile newfile" % (sys.argv[0],)
  27. else:
  28. old = readFile(sys.argv[1])
  29. new = untempl(old)
  30. writeFile(sys.argv[2], new)
  31. if __name__ == '__main__':
  32. main()