Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
| 2 # | |
| 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | |
| 5 # found in the LICENSE file. | |
| 6 | |
| 7 import sys | |
| 8 | |
| 9 from idl_log import ErrOut, InfoOut, WarnOut | |
| 10 from idl_option import GetOption, Option, ParseOptions | |
| 11 | |
| 12 GeneratorList = [] | |
| 13 | |
| 14 Option('version', 'Which version to generate.', default='M14') | |
| 15 Option('range', 'Which version ranges in the form of MIN,MAX.', default='') | |
| 16 | |
| 17 | |
| 18 # | |
| 19 # Generator | |
| 20 # | |
| 21 # Base class for generators. This class provides a mechanism for | |
| 22 # adding new generator objects to the IDL driver. To use this class | |
| 23 # override the GenerateVersion and GenerateRange members, and | |
| 24 # instantiate one copy of the class in the same module which defines it to | |
| 25 # register the generator. After the AST is generated, call the static Run | |
| 26 # member which will check every registered generator to see which ones have | |
| 27 # been enabled through command-line options. To a enable generator use the | |
|
sehr (please use chromium)
2011/08/16 16:43:12
s/a enable/enable a/
noelallen1
2011/08/19 17:07:29
Done.
| |
| 28 # switches: | |
| 29 # --<sname> : To enable with defaults | |
| 30 # --<sname>_opt=<XXX,YYY=y> : To enable with generator specific options. | |
| 31 # | |
| 32 # NOTE: Generators still have access to global options | |
| 33 | |
| 34 class Generator(object): | |
| 35 def __init__(self, name, sname, desc): | |
| 36 self.name = name | |
| 37 self.run_switch = Option(sname, desc) | |
| 38 self.opt_switch = Option(sname + '_opt', 'Options for %s.' % sname, | |
| 39 default='') | |
| 40 GeneratorList.append(self) | |
| 41 self.errors = 0 | |
| 42 | |
| 43 def Error(self, msg): | |
| 44 ErrOut.Log('Error %s : %s' % (self.name, msg)) | |
| 45 self.errors += 1 | |
| 46 | |
| 47 def GetRunOptions(self): | |
| 48 options = {} | |
| 49 option_list = self.opt_switch.Get() | |
| 50 if option_list: | |
| 51 option_list = option_list.split(',') | |
| 52 for opt in option_list: | |
| 53 offs = opt.find('=') | |
| 54 if offs > 0: | |
| 55 options[opt[:offs]] = opt[offs+1:] | |
| 56 else: | |
| 57 options[opt] = True | |
| 58 return options | |
| 59 if self.run_switch.Get(): | |
| 60 return options | |
| 61 return None | |
| 62 | |
| 63 def Generate(self, ast, options): | |
| 64 self.errors = 0 | |
| 65 | |
| 66 rangestr = GetOption('range') | |
| 67 versionstr = GetOption('version') | |
| 68 | |
| 69 # Check for a range option which over-rides a version option | |
| 70 if rangestr: | |
| 71 range_list = rangestr.split(',') | |
| 72 if len(range_list) != 2: | |
| 73 self.Error('Failed to generate for %s, incorrect range: "%s"' % | |
| 74 (self.name, rangestr)) | |
| 75 else: | |
| 76 vmin = range_list[0] | |
| 77 vmax = range_list[1] | |
| 78 ret = self.GenerateRange(ast, vmin, vmax, options) | |
| 79 if not ret: | |
| 80 self.Error('Failed to generate range %s : %s.' %(vmin, vmax)) | |
| 81 # Otherwise this should be a single version generation | |
| 82 else: | |
| 83 if versionstr: | |
| 84 ret = self.GenerateVersion(ast, versionstr, options) | |
| 85 if not ret: | |
| 86 self.Error('Failed to generate version.') | |
| 87 else: | |
| 88 self.Error('No range or version specified for %s.' % versionstr) | |
| 89 return self.errors | |
| 90 | |
| 91 def GenerateVersion(self, ast, version, options): | |
| 92 __pychecker__ = 'unusednames=ast,version,options' | |
| 93 self.Error("Undefined version generator.") | |
| 94 | |
| 95 def GenerateRange(self, ast, vmin, vmax, options): | |
| 96 __pychecker__ = 'unusednames=ast,vmin,vmax,options' | |
| 97 self.Error("Undefined range generator.") | |
| 98 | |
| 99 @staticmethod | |
| 100 def Run(ast): | |
| 101 count = 0 | |
| 102 | |
| 103 # Check all registered generators if they should run. | |
| 104 for gen in GeneratorList: | |
| 105 options = gen.GetRunOptions() | |
| 106 if options is not None: | |
| 107 if not gen.Generate(ast, options): | |
| 108 count += 1 | |
| 109 return count | |
|
sehr (please use chromium)
2011/08/16 16:43:12
what is the significance of count? The number of
noelallen1
2011/08/19 17:07:29
Done.
| |
| 110 | |
| 111 | |
| 112 check_version = 0 | |
| 113 check_range = 0 | |
| 114 | |
| 115 class GeneratorVersionTest(Generator): | |
| 116 def GenerateVersion(self, ast, version, options = {}): | |
| 117 __pychecker__ = 'unusednames=ast,version,options' | |
| 118 global check_version | |
| 119 check_map = { | |
| 120 'so_long': True, | |
| 121 'MyOpt': 'XYZ', | |
| 122 'goodbye': True | |
| 123 } | |
| 124 check_version = 1 | |
| 125 for item in check_map: | |
| 126 check_item = check_map[item] | |
| 127 option_item = options.get(item, None) | |
| 128 if check_item != option_item: | |
| 129 print 'Option %s is %s, expecting %s' % (item, option_item, check_item) | |
| 130 check_version = 0 | |
| 131 | |
| 132 if version != 'M14': | |
| 133 check_version = 0 | |
| 134 return check_version == 1 | |
| 135 | |
| 136 def GenerateRange(self, ast, vmin, vmax, options = {}): | |
| 137 __pychecker__ = 'unusednames=ast,vmin,vmax,options' | |
| 138 global check_range | |
| 139 check_range = 1 | |
| 140 return True | |
| 141 | |
| 142 def Main(args): | |
| 143 __pychecker__ = 'unusednames=args' | |
| 144 global check_version | |
| 145 global check_range | |
| 146 | |
| 147 ParseOptions(['--testgen_opt=so_long,MyOpt=XYZ,goodbye']) | |
| 148 if Generator.Run('AST') != 1: | |
| 149 print 'Generate version: Failed.\n' | |
| 150 return -1 | |
| 151 | |
| 152 if check_version != 1 or check_range != 0: | |
| 153 print 'Gererate version: Failed to run.\n' | |
| 154 return -1 | |
| 155 | |
| 156 check_version = 0 | |
| 157 ParseOptions(['--testgen_opt="HELLO"', '--range=M14,M16']) | |
| 158 if Generator.Run('AST') != 1: | |
| 159 print 'Generate range: Failed.\n' | |
| 160 return -1 | |
| 161 | |
| 162 if check_version != 0 or check_range != 1: | |
| 163 print 'Gererate range: Failed to run.\n' | |
| 164 return -1 | |
| 165 | |
| 166 print 'Generator test: Pass' | |
| 167 return 0 | |
| 168 | |
| 169 if __name__ == '__main__': | |
| 170 GeneratorVersionTest('Test Gen', 'testgen', 'Generator Class Test.') | |
| 171 sys.exit(Main(sys.argv[1:])) | |
| 172 | |
| OLD | NEW |