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 Converts a given ASCII proto into a binary resource. | |
| 8 | |
| 9 """ | |
| 10 | |
| 11 import abc | |
| 12 import optparse | |
| 13 import os | |
| 14 import re | |
| 15 import subprocess | |
| 16 import sys | |
| 17 import traceback | |
| 18 | |
| 19 class BinaryProtoGenerator: | |
| 20 | |
| 21 # If the script is run in a virtualenv | |
| 22 # (https://virtualenv.pypa.io/en/stable/), then no google.protobuf library | |
| 23 # should be brought in from site-packages. Passing -S into the interpreter in | |
| 24 # a virtualenv actually destroys the ability to import standard library | |
| 25 # functions like optparse, so this script should not be wrapped if we're in a | |
| 26 # virtualenv. | |
| 27 def _IsInVirtualEnv(self): | |
| 28 # This is the way used by pip and other software to detect virtualenv. | |
| 29 return hasattr(sys, 'real_prefix') | |
| 30 | |
| 31 def _ImportProtoModules(self, paths): | |
| 32 """Import the protobuf modules we need. |paths| is list of import paths""" | |
| 33 print paths | |
|
Nathan Parker
2016/12/10 00:56:50
I think you'll want to remove any "print"s since t
meacer
2016/12/13 02:30:16
Eeek, of course. Debugging artifact :)
| |
| 34 for path in paths: | |
| 35 # Put the path to our proto libraries in front, so that we don't use | |
| 36 # system protobuf. | |
| 37 sys.path.insert(1, path) | |
| 38 | |
| 39 import google.protobuf.text_format as text_format | |
| 40 globals()['text_format'] = text_format | |
| 41 self.ImportProtoModule() | |
|
Nathan Parker
2016/12/10 00:56:50
What does this do? Is this so a child class can ov
meacer
2016/12/13 02:30:16
Yes, it's an abstract method to be implemented by
| |
| 42 | |
| 43 def _GenerateBinaryProtos(self, opts): | |
| 44 """ Read the ASCII proto and generate one or more binary protos. """ | |
| 45 # Read the ASCII | |
| 46 ifile = open(opts.infile, 'r') | |
| 47 ascii_pb_str = ifile.read() | |
| 48 ifile.close() | |
| 49 | |
| 50 # Parse it into a structured PB | |
| 51 full_pb = self.EmptyProtoInstance() | |
| 52 text_format.Merge(ascii_pb_str, full_pb) | |
| 53 | |
| 54 self.ValidatePb(opts, full_pb); | |
| 55 self.ProcessPb(opts, full_pb) | |
| 56 | |
| 57 @abc.abstractmethod | |
|
Nathan Parker
2016/12/10 00:56:50
I'm not familiar with @abc.. is this to ensure it
meacer
2016/12/13 02:30:16
abc stands for abstract base class, and yes the an
| |
| 58 def ImportProtoModule(self): | |
| 59 """ Import the proto module to be used by the generator. """ | |
| 60 pass | |
| 61 | |
| 62 @abc.abstractmethod | |
| 63 def EmptyProtoInstance(self): | |
| 64 """ Returns an empty proto instance to be filled by the generator.""" | |
| 65 pass | |
| 66 | |
| 67 @abc.abstractmethod | |
| 68 def ValidatePb(self, opts, pb): | |
| 69 """ Validate the basic values of the protobuf. The | |
| 70 file_type_policies_unittest.cc will also validate it by platform, | |
| 71 but this will catch errors earlier. | |
| 72 """ | |
| 73 pass | |
| 74 | |
| 75 @abc.abstractmethod | |
| 76 def ProcessPb(self, opts, pb): | |
| 77 """ Process the parsed prototobuf. """ | |
| 78 pass | |
| 79 | |
| 80 def AddCommandLineOptions(self, parser): | |
| 81 """ Allows subclasses to add any options the command line parser. """ | |
| 82 pass | |
| 83 | |
| 84 def AddExtraCommandLineArgsForVirtualEnvRun(self, opts, command): | |
| 85 """ Allows subclasses to add any extra command line arguments when running | |
| 86 this under a virtualenv.""" | |
| 87 pass | |
| 88 | |
| 89 def VerifyArgs(self, opts): | |
| 90 """ Allows subclasses to check command line parameters before running. """ | |
| 91 return True | |
| 92 | |
| 93 def Run(self): | |
| 94 parser = optparse.OptionParser() | |
| 95 # TODO(nparker): Remove this once the bug is fixed. | |
|
Nathan Parker
2016/12/10 00:56:50
That bug is actually fixed, but we still need this
meacer
2016/12/13 02:30:16
Updated the reference.
| |
| 96 parser.add_option('-w', '--wrap', action="store_true", default=False, | |
| 97 help='Wrap this script in another python ' | |
| 98 'execution to disable site-packages. This is a ' | |
| 99 'fix for http://crbug.com/605592') | |
| 100 | |
| 101 parser.add_option('-i', '--infile', | |
| 102 help='The ASCII DownloadFileType-proto file to read.') | |
|
Nathan Parker
2016/12/10 00:56:50
remove reference to the DownloadFileType file name
meacer
2016/12/13 02:30:16
Done.
| |
| 103 parser.add_option('-d', '--outdir', | |
| 104 help='Directory underwhich binary file(s) will be ' + | |
| 105 'written') | |
| 106 parser.add_option('-o', '--outbasename', | |
| 107 help='Basename of the binary file to write to.') | |
| 108 parser.add_option('-p', '--path', action="append", | |
| 109 help='Repeat this as needed. Directory(s) containing ' + | |
| 110 'the known_certs_pb2.py and ' + | |
|
Nathan Parker
2016/12/10 00:56:50
Maybe generalize the known_certs_pb2.py to "your_p
meacer
2016/12/13 02:30:16
Done.
| |
| 111 'google.protobuf.text_format modules') | |
| 112 self.AddCommandLineOptions(parser) | |
| 113 | |
| 114 (opts, args) = parser.parse_args() | |
| 115 if opts.infile is None or opts.outdir is None or opts.outbasename is None: | |
| 116 parser.print_help() | |
| 117 return 1 | |
| 118 | |
| 119 if opts.wrap and not self._IsInVirtualEnv(): | |
| 120 # Run this script again with different args to the interpreter to suppress | |
| 121 # the inclusion of libraries, like google.protobuf, from site-packages, | |
| 122 # which is checked before sys.path when resolving imports. We want to | |
| 123 # specifically import the libraries injected into the sys.path in | |
| 124 # ImportProtoModules(). | |
| 125 command = [sys.executable, '-S', '-s', sys.argv[0]] | |
| 126 command += ['-i', opts.infile] | |
| 127 command += ['-d', opts.outdir] | |
| 128 command += ['-o', opts.outbasename] | |
| 129 for path in opts.path: | |
| 130 command += ['-p', path] | |
| 131 | |
| 132 self.AddExtraCommandLineArgsForVirtualEnvRun(opts, command); | |
| 133 sys.exit(subprocess.call(command)) | |
| 134 | |
| 135 self._ImportProtoModules(opts.path) | |
| 136 | |
| 137 if not self.VerifyArgs(opts): | |
| 138 print "Wrong arguments" | |
| 139 return 1 | |
| 140 | |
| 141 try: | |
| 142 self._GenerateBinaryProtos(opts) | |
| 143 except Exception as e: | |
| 144 print "ERROR: Failed to render binary version of %s:\n %s\n%s" % ( | |
| 145 opts.infile, str(e), traceback.format_exc()) | |
| 146 return 1 | |
| OLD | NEW |