Mercurial > public > mercurial-scm > hg
comparison contrib/byteify-strings.py @ 38385:a2976c27dac4
byteify-strings: add basic command interface
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Thu, 31 May 2018 22:23:30 +0900 |
parents | 1d9c97db465f |
children | 9f42e4a83676 |
comparison
equal
deleted
inserted
replaced
38384:1d9c97db465f | 38385:a2976c27dac4 |
---|---|
1 #!/usr/bin/env python3 | |
2 # | |
1 # byteify-strings.py - transform string literals to be Python 3 safe | 3 # byteify-strings.py - transform string literals to be Python 3 safe |
2 # | 4 # |
3 # Copyright 2015 Gregory Szorc <gregory.szorc@gmail.com> | 5 # Copyright 2015 Gregory Szorc <gregory.szorc@gmail.com> |
4 # | 6 # |
5 # This software may be used and distributed according to the terms of the | 7 # This software may be used and distributed according to the terms of the |
6 # GNU General Public License version 2 or any later version. | 8 # GNU General Public License version 2 or any later version. |
7 | 9 |
8 from __future__ import absolute_import | 10 from __future__ import absolute_import |
9 | 11 |
12 import argparse | |
10 import io | 13 import io |
14 import sys | |
11 import token | 15 import token |
12 import tokenize | 16 import tokenize |
13 | 17 |
14 if True: | 18 if True: |
15 def replacetokens(tokens, fullname): | 19 def replacetokens(tokens, fullname): |
150 yield t._replace(string=fn[4:]) | 154 yield t._replace(string=fn[4:]) |
151 continue | 155 continue |
152 | 156 |
153 # Emit unmodified token. | 157 # Emit unmodified token. |
154 yield t | 158 yield t |
159 | |
160 def process(fin, fout): | |
161 tokens = tokenize.tokenize(fin.readline) | |
162 tokens = replacetokens(list(tokens), fullname='<dummy>') | |
163 fout.write(tokenize.untokenize(tokens)) | |
164 | |
165 def main(): | |
166 ap = argparse.ArgumentParser() | |
167 ap.add_argument('files', metavar='FILE', nargs='+', help='source file') | |
168 args = ap.parse_args() | |
169 for fname in args.files: | |
170 with open(fname, 'rb') as fin: | |
171 fout = sys.stdout.buffer | |
172 process(fin, fout) | |
173 | |
174 if __name__ == '__main__': | |
175 main() |