Chromium Code Reviews| 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 subprocess | |
| 12 import sys | |
| 13 | |
| 14 | |
| 15 def ConvertProto(opts): | |
| 16 # Find the proto code | |
| 17 for path in opts.path: | |
| 18 # Put the path to our proto libraries in front, so that we don't use system | |
| 19 # protobuf. | |
| 20 sys.path.insert(1, path) | |
| 21 | |
| 22 | |
| 23 import download_file_types_pb2 as config_pb2 | |
| 24 import google.protobuf.text_format as text_format | |
| 25 | |
| 26 # Read the ASCII | |
| 27 ifile = open(opts.infile, 'r') | |
| 28 ascii_pb_str = ifile.read() | |
| 29 ifile.close() | |
| 30 | |
| 31 # Parse it into a structure PB | |
| 32 pb = config_pb2.DownloadFileTypeConfig() | |
| 33 text_format.Merge(ascii_pb_str, pb) | |
| 34 | |
| 35 # Serialize it | |
| 36 binary_pb_str = pb.SerializeToString() | |
| 37 | |
| 38 # Write it to disk | |
| 39 open(opts.outfile, 'w').write(binary_pb_str) | |
| 40 | |
| 41 | |
| 42 def main(): | |
| 43 parser = optparse.OptionParser() | |
| 44 # TODO(nparker): Remove this once the bug is fixed. | |
| 45 parser.add_option('-w', '--wrap', action="store_true", default=False, | |
| 46 help='Wrap this script in another python ' | |
| 47 'execution to disable site-packages. This is a ' | |
| 48 'fix for http://crbug.com/605592') | |
| 49 parser.add_option('-i', '--infile', | |
| 50 help='The ASCII DownloadFileType-proto file to read.') | |
| 51 parser.add_option('-o', '--outfile', | |
| 52 help='The binary file to write.') | |
| 53 parser.add_option('-p', '--path', action="append", | |
| 54 help='Repeat this as needed. Directory(s) containing ' + | |
| 55 'the download_file_types_pb2.py and ' + | |
| 56 'google.protobuf.text_format modules') | |
| 57 (opts, args) = parser.parse_args() | |
| 58 if opts.infile is None or opts.outfile is None: | |
| 59 parser.print_help() | |
| 60 return 1 | |
| 61 | |
| 62 if opts.wrap: | |
| 63 # Run this script again with different args to the interpreter. | |
|
xyzzyz
2016/04/25 23:02:21
Explain in comment why we do this.
| |
| 64 command = [sys.executable, '-S', '-s', sys.argv[0]] | |
| 65 command += ['-i', opts.infile] | |
| 66 command += ['-o', opts.outfile] | |
| 67 for path in opts.path: | |
| 68 command += ['-p', path] | |
| 69 sys.exit(subprocess.call(command)) | |
| 70 | |
| 71 try: | |
| 72 ConvertProto(opts) | |
| 73 except Exception as e: | |
| 74 print "ERROR: %s failed to parse ASCII proto\n%s: %s\n" % ( | |
| 75 sys.argv, opts.infile, str(e)) | |
| 76 return 1 | |
| 77 | |
| 78 if __name__ == '__main__': | |
| 79 sys.exit(main()) | |
| OLD | NEW |