OLD | NEW |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright 2016 The Chromium Authors. All rights reserved. | 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """ | 6 """ |
7 Convert the ASCII download_file_types.asciipb proto into a binary resource. | 7 Convert the ASCII download_file_types.asciipb proto into a binary resource. |
8 """ | 8 """ |
9 | 9 |
10 import optparse | 10 import optparse |
11 import sys | 11 import sys |
12 | 12 |
13 | 13 |
14 def ConvertProto(opts): | 14 def ConvertProto(opts): |
15 # Find the proto code | 15 # Find the proto code |
16 for path in opts.path: | 16 for path in opts.path: |
17 # Put the path to our proto libraries in front, so that we don't use system | 17 # Put the path to our proto libraries in front, so that we don't use system |
18 # protobuf. | 18 # protobuf. |
19 sys.path.insert(1, path) | 19 sys.path.insert(1, path) |
20 | 20 |
21 import download_file_types_pb2 as config_pb2 | 21 import download_file_types_pb2 as config_pb2 |
22 import google.protobuf.text_format as text_format | 22 import google.protobuf.text_format as text_format |
23 | 23 |
24 # Read the ASCII | 24 # Read the ASCII |
25 ifile = open(opts.infile, 'r') | 25 ifile = open(opts.infile, 'rb') |
Jialiu Lin
2016/04/22 21:36:41
should it be 'r', since you're not reading a binar
Nathan Parker
2016/04/25 22:39:19
Done.
| |
26 ascii_pb_str = ifile.read() | 26 ascii_pb_str = ifile.read() |
27 ifile.close() | 27 ifile.close() |
28 | 28 |
29 # Parse it into a structure PB | 29 # Parse it into a structure PB |
30 pb = config_pb2.DownloadFileTypeConfig() | 30 pb = config_pb2.DownloadFileTypeConfig() |
31 text_format.Merge(ascii_pb_str, pb) | 31 text_format.Merge(ascii_pb_str, pb) |
32 | 32 |
33 # Serialize it | 33 # Serialize it |
34 binary_pb_str = pb.SerializeToString() | 34 binary_pb_str = pb.SerializeToString() |
35 | 35 |
36 # Write it to disk | 36 # Write it to disk |
37 open(opts.outfile, 'w').write(binary_pb_str) | 37 open(opts.outfile, 'wb').write(binary_pb_str) |
38 | 38 |
39 | 39 |
40 def main(): | 40 def main(): |
41 parser = optparse.OptionParser() | 41 parser = optparse.OptionParser() |
42 parser.add_option('-i', '--infile', | 42 parser.add_option('-i', '--infile', |
43 help='The ASCII DownloadFileType-proto file to read.') | 43 help='The ASCII DownloadFileType-proto file to read.') |
44 parser.add_option('-o', '--outfile', | 44 parser.add_option('-o', '--outfile', |
45 help='The binary file to write.') | 45 help='The binary file to write.') |
46 parser.add_option('-p', '--path', action="append", | 46 parser.add_option('-p', '--path', action="append", |
47 help='Repeat this as needed. Directory(s) containing ' + | 47 help='Repeat this as needed. Directory(s) containing ' + |
48 'the download_file_types_pb2.py and ' + | 48 'the download_file_types_pb2.py and ' + |
49 'google.protobuf.text_format modules') | 49 'google.protobuf.text_format modules') |
50 (opts, args) = parser.parse_args() | 50 (opts, args) = parser.parse_args() |
51 if opts.infile is None or opts.outfile is None: | 51 if opts.infile is None or opts.outfile is None: |
52 parser.print_help() | 52 parser.print_help() |
53 return 1 | 53 return 1 |
54 | 54 |
55 try: | 55 try: |
56 ConvertProto(opts) | 56 ConvertProto(opts) |
57 except Exception as e: | 57 except Exception as e: |
58 print "ERROR: %s failed to parse ASCII proto\n%s: %s\n" % ( | 58 print "ERROR: %s failed to parse ASCII proto\n%s: %s\n" % ( |
59 sys.argv, opts.infile, str(e)) | 59 sys.argv, opts.infile, str(e)) |
60 return 1 | 60 return 1 |
61 | 61 |
62 if __name__ == '__main__': | 62 if __name__ == '__main__': |
63 sys.exit(main()) | 63 sys.exit(main()) |
OLD | NEW |