# HG changeset patch # User Yuya Nishihara # Date 1527773010 -32400 # Node ID a2976c27dac4c08a82ec102ad8fe14a606f12f1c # Parent 1d9c97db465fc82baf3e9c380fb069dd17f4ddd2 byteify-strings: add basic command interface diff -r 1d9c97db465f -r a2976c27dac4 contrib/byteify-strings.py --- a/contrib/byteify-strings.py Thu May 31 22:07:04 2018 +0900 +++ b/contrib/byteify-strings.py Thu May 31 22:23:30 2018 +0900 @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 +# # byteify-strings.py - transform string literals to be Python 3 safe # # Copyright 2015 Gregory Szorc @@ -7,7 +9,9 @@ from __future__ import absolute_import +import argparse import io +import sys import token import tokenize @@ -152,3 +156,20 @@ # Emit unmodified token. yield t + +def process(fin, fout): + tokens = tokenize.tokenize(fin.readline) + tokens = replacetokens(list(tokens), fullname='') + fout.write(tokenize.untokenize(tokens)) + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument('files', metavar='FILE', nargs='+', help='source file') + args = ap.parse_args() + for fname in args.files: + with open(fname, 'rb') as fin: + fout = sys.stdout.buffer + process(fin, fout) + +if __name__ == '__main__': + main()