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