OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python2.4 |
| 2 # Copyright (c) 2006-2008 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 '''Command processor for GRIT. This is the script you invoke to run the various |
| 7 GRIT tools. |
| 8 ''' |
| 9 |
| 10 import os |
| 11 import sys |
| 12 if __name__ == '__main__': |
| 13 sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '..')) |
| 14 |
| 15 import getopt |
| 16 |
| 17 from grit import util |
| 18 |
| 19 import grit.exception |
| 20 |
| 21 import grit.tool.build |
| 22 import grit.tool.count |
| 23 import grit.tool.diff_structures |
| 24 import grit.tool.menu_from_parts |
| 25 import grit.tool.newgrd |
| 26 import grit.tool.resize |
| 27 import grit.tool.rc2grd |
| 28 import grit.tool.test |
| 29 import grit.tool.transl2tc |
| 30 import grit.tool.unit |
| 31 |
| 32 |
| 33 # Copyright notice |
| 34 _COPYRIGHT = '''\ |
| 35 GRIT - the Google Resource and Internationalization Tool |
| 36 Copyright (c) Google Inc. %d |
| 37 ''' % util.GetCurrentYear() |
| 38 |
| 39 # Keys for the following map |
| 40 _CLASS = 1 |
| 41 _REQUIRES_INPUT = 2 |
| 42 _HIDDEN = 3 # optional key - presence indicates tool is hidden |
| 43 |
| 44 |
| 45 # Maps tool names to the tool's module. Done as a list of (key, value) tuples |
| 46 # instead of a map to preserve ordering. |
| 47 _TOOLS = [ |
| 48 ['build', { _CLASS : grit.tool.build.RcBuilder, _REQUIRES_INPUT : True }], |
| 49 ['newgrd', { _CLASS : grit.tool.newgrd.NewGrd, _REQUIRES_INPUT : False }], |
| 50 ['rc2grd', { _CLASS : grit.tool.rc2grd.Rc2Grd, _REQUIRES_INPUT : False }], |
| 51 ['transl2tc', { _CLASS : grit.tool.transl2tc.TranslationToTc, |
| 52 _REQUIRES_INPUT : False }], |
| 53 ['sdiff', { _CLASS : grit.tool.diff_structures.DiffStructures, |
| 54 _REQUIRES_INPUT : False }], |
| 55 ['resize', { |
| 56 _CLASS : grit.tool.resize.ResizeDialog, _REQUIRES_INPUT : True }], |
| 57 ['unit', { _CLASS : grit.tool.unit.UnitTestTool, _REQUIRES_INPUT : False }], |
| 58 ['count', { _CLASS : grit.tool.count.CountMessage, _REQUIRES_INPUT : True }], |
| 59 ['test', { |
| 60 _CLASS: grit.tool.test.TestTool, _REQUIRES_INPUT : True, |
| 61 _HIDDEN : True }], |
| 62 ['menufromparts', { |
| 63 _CLASS: grit.tool.menu_from_parts.MenuTranslationsFromParts, |
| 64 _REQUIRES_INPUT : True, _HIDDEN : True }], |
| 65 ] |
| 66 |
| 67 |
| 68 def PrintUsage(): |
| 69 print _COPYRIGHT |
| 70 |
| 71 tool_list = '' |
| 72 for (tool, info) in _TOOLS: |
| 73 if not _HIDDEN in info.keys(): |
| 74 tool_list += ' %-12s %s\n' % (tool, info[_CLASS]().ShortDescription()) |
| 75 |
| 76 # TODO(joi) Put these back into the usage when appropriate: |
| 77 # |
| 78 # -d Work disconnected. This causes GRIT not to attempt connections with |
| 79 # e.g. Perforce. |
| 80 # |
| 81 # -c Use the specified Perforce CLIENT when talking to Perforce. |
| 82 print '''Usage: grit [GLOBALOPTIONS] TOOL [args to tool] |
| 83 |
| 84 Global options: |
| 85 |
| 86 -i INPUT Specifies the INPUT file to use (a .grd file). If this is not |
| 87 specified, GRIT will look for the environment variable GRIT_INPUT. |
| 88 If it is not present either, GRIT will try to find an input file |
| 89 named 'resource.grd' in the current working directory. |
| 90 |
| 91 -v Print more verbose runtime information. |
| 92 |
| 93 -x Print extremely verbose runtime information. Implies -v |
| 94 |
| 95 -p FNAME Specifies that GRIT should profile its execution and output the |
| 96 results to the file FNAME. |
| 97 |
| 98 Tools: |
| 99 |
| 100 TOOL can be one of the following: |
| 101 %s |
| 102 For more information on how to use a particular tool, and the specific |
| 103 arguments you can send to that tool, execute 'grit help TOOL' |
| 104 ''' % (tool_list) |
| 105 |
| 106 |
| 107 class Options(object): |
| 108 '''Option storage and parsing.''' |
| 109 |
| 110 def __init__(self): |
| 111 self.disconnected = False |
| 112 self.client = '' |
| 113 self.input = None |
| 114 self.verbose = False |
| 115 self.extra_verbose = False |
| 116 self.output_stream = sys.stdout |
| 117 self.profile_dest = None |
| 118 |
| 119 def ReadOptions(self, args): |
| 120 '''Reads options from the start of args and returns the remainder.''' |
| 121 (opts, args) = getopt.getopt(args, 'g:dvxc:i:p:') |
| 122 for (key, val) in opts: |
| 123 if key == '-d': self.disconnected = True |
| 124 elif key == '-c': self.client = val |
| 125 elif key == '-i': self.input = val |
| 126 elif key == '-v': |
| 127 self.verbose = True |
| 128 util.verbose = True |
| 129 elif key == '-x': |
| 130 self.verbose = True |
| 131 util.verbose = True |
| 132 self.extra_verbose = True |
| 133 util.extra_verbose = True |
| 134 elif key == '-p': self.profile_dest = val |
| 135 |
| 136 if not self.input: |
| 137 if 'GRIT_INPUT' in os.environ: |
| 138 self.input = os.environ['GRIT_INPUT'] |
| 139 else: |
| 140 self.input = 'resource.grd' |
| 141 |
| 142 return args |
| 143 |
| 144 def __repr__(self): |
| 145 return '(disconnected: %d, verbose: %d, client: %s, input: %s)' % ( |
| 146 self.disconnected, self.verbose, self.client, self.input) |
| 147 |
| 148 |
| 149 def _GetToolInfo(tool): |
| 150 '''Returns the info map for the tool named 'tool' or None if there is no |
| 151 such tool.''' |
| 152 matches = filter(lambda t: t[0] == tool, _TOOLS) |
| 153 if not len(matches): |
| 154 return None |
| 155 else: |
| 156 return matches[0][1] |
| 157 |
| 158 |
| 159 def Main(args): |
| 160 '''Parses arguments and does the appropriate thing.''' |
| 161 util.ChangeStdoutEncoding() |
| 162 |
| 163 if not len(args) or len(args) == 1 and args[0] == 'help': |
| 164 PrintUsage() |
| 165 return 0 |
| 166 elif len(args) == 2 and args[0] == 'help': |
| 167 tool = args[1].lower() |
| 168 if not _GetToolInfo(tool): |
| 169 print "No such tool. Try running 'grit help' for a list of tools." |
| 170 return 2 |
| 171 |
| 172 print ("Help for 'grit %s' (for general help, run 'grit help'):\n" |
| 173 % (tool)) |
| 174 print _GetToolInfo(tool)[_CLASS].__doc__ |
| 175 return 0 |
| 176 else: |
| 177 options = Options() |
| 178 args = options.ReadOptions(args) # args may be shorter after this |
| 179 if not args: |
| 180 print "No tool provided. Try running 'grit help' for a list of tools." |
| 181 return 2 |
| 182 tool = args[0] |
| 183 if not _GetToolInfo(tool): |
| 184 print "No such tool. Try running 'grit help' for a list of tools." |
| 185 return 2 |
| 186 |
| 187 try: |
| 188 if _GetToolInfo(tool)[_REQUIRES_INPUT]: |
| 189 os.stat(options.input) |
| 190 except OSError: |
| 191 print ('Input file %s not found.\n' |
| 192 'To specify a different input file:\n' |
| 193 ' 1. Use the GRIT_INPUT environment variable.\n' |
| 194 ' 2. Use the -i command-line option. This overrides ' |
| 195 'GRIT_INPUT.\n' |
| 196 ' 3. Specify neither GRIT_INPUT or -i and GRIT will try to load ' |
| 197 "'resource.grd'\n" |
| 198 ' from the current directory.' % options.input) |
| 199 return 2 |
| 200 |
| 201 toolobject = _GetToolInfo(tool)[_CLASS]() |
| 202 if options.profile_dest: |
| 203 import hotshot |
| 204 prof = hotshot.Profile(options.profile_dest) |
| 205 prof.runcall(toolobject.Run, options, args[1:]) |
| 206 else: |
| 207 toolobject.Run(options, args[1:]) |
| 208 |
| 209 |
| 210 if __name__ == '__main__': |
| 211 sys.exit(Main(sys.argv[1:])) |
| 212 |
OLD | NEW |