/run_sed

https://code.google.com/p/latex-makefile/ · #! · 42 lines · 31 code · 11 blank · 0 comment · 0 complexity · 3e93c7b68164425210907273c5e2a019 MD5 · raw file

  1. #!/usr/bin/env python2.6
  2. """Run sed from a script, but do it as though it is being used in a Makefile.
  3. The difference between this and its use in the makefile itself is that here we
  4. don't escape $ to $$.
  5. """
  6. from __future__ import print_function, division
  7. import os
  8. import sys
  9. from optparse import OptionParser
  10. from util import template
  11. def main(opts, args):
  12. ns = template.Template()
  13. sed_args = ["sed"]
  14. text = ns.expand_file(args[0],
  15. transform_after=ns.xfunc_sed_lines_to_args())
  16. sed_args.extend(text.split('\n'))
  17. sed_args.extend(args[1:])
  18. if opts.show_cmd:
  19. import pprint
  20. pprint.pprint(sed_args, stream=sys.stderr)
  21. # Ensure that sed still works with international characters (and doesn't just
  22. # hang)
  23. os.putenv("LANG", "C")
  24. with os.popen(" ".join(sed_args)) as f:
  25. print(f.read())
  26. if __name__ == '__main__':
  27. op = OptionParser()
  28. op.add_option("-c", "--show_cmd", dest="show_cmd", default=False,
  29. action="store_true", help="show sed script")
  30. opts, args = op.parse_args()
  31. main(opts, args)