11#
22# Copyright (c) 2009, 2010 Testrepository Contributors
3- #
3+ #
44# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
55# license at the users choice. A copy of both licenses are available in the
66# project source as Apache-2.0 and BSD. You may not use this file except in
77# compliance with one of these two licences.
8- #
8+ #
99# Unless required by applicable law or agreed to in writing, software
1010# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
1111# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1919
2020Actual commands can be found in testrepository.commands.$commandname.
2121
22- For example, testrepository.commands.init is the init command name, and
22+ For example, testrepository.commands.init is the init command name, and
2323testrepository.command.show_stats would be the show-stats command (if one
2424existed). The Command discovery logic looks for a class in the module with
2525the same name - e.g. tesrepository.commands.init.init would be the class.
4141
4242from testrepository .repository import file
4343
44+
4445def _find_command (cmd_name ):
4546 orig_cmd_name = cmd_name
46- cmd_name = cmd_name .replace ('-' , '_' )
47+ cmd_name = cmd_name .replace ("-" , "_" )
4748 classname = "%s" % cmd_name
4849 modname = "testrepository.commands.%s" % cmd_name
4950 try :
@@ -54,8 +55,9 @@ def _find_command(cmd_name):
5455 if result is None :
5556 raise KeyError (
5657 "Malformed command module - no command class %s found in module %s."
57- % (classname , modname ))
58- if getattr (result , 'name' , None ) is None :
58+ % (classname , modname )
59+ )
60+ if getattr (result , "name" , None ) is None :
5961 # Store the name for the common case of name == lookup path.
6062 result .name = orig_cmd_name
6163 return result
@@ -69,13 +71,13 @@ def iter_commands():
6971 # For now, only support regular installs. TODO: support zip, eggs.
7072 for filename in os .listdir (path ):
7173 base = os .path .basename (filename )
72- if base .startswith ('.' ):
74+ if base .startswith ("." ):
7375 continue
74- name = base .split ('.' , 1 )[0 ]
75- name = name .replace ('_' , '-' )
76+ name = base .split ("." , 1 )[0 ]
77+ name = name .replace ("_" , "-" )
7678 names .add (name )
77- names .discard (' --init--' )
78- names .discard (' --pycache--' )
79+ names .discard (" --init--" )
80+ names .discard (" --pycache--" )
7981 names = sorted (names )
8082 for name in names :
8183 yield _find_command (name )
@@ -91,7 +93,7 @@ class Command(object):
9193 :ivar ui: a UI object which is responsible for brokering the command
9294 arguments, input and output. There is no default ui, it must be
9395 passed to the constructor.
94-
96+
9597 :ivar repository_factory: a repository factory which is used to create or
9698 open repositories. The default repository factory is suitable for
9799 use in the command line tool.
@@ -129,7 +131,7 @@ def execute(self):
129131 This interrogates the UI to ensure that arguments and options are
130132 supplied, performs any validation for the same that the command needs
131133 and finally calls run() to perform the command. Most commands should
132- not need to override this method, and any user wanting to run a
134+ not need to override this method, and any user wanting to run a
133135 command should call this method.
134136
135137 This is a synchronous method, and basically just a helper. GUI's or
@@ -150,7 +152,7 @@ def execute(self):
150152
151153 @classmethod
152154 def get_summary (klass ):
153- docs = klass .__doc__ .split (' \n ' )
155+ docs = klass .__doc__ .split (" \n " )
154156 return docs [0 ]
155157
156158 def _init (self ):
@@ -173,15 +175,16 @@ def run_argv(argv, stdin, stdout, stderr):
173175 cmd_name = None
174176 cmd_args = argv [1 :]
175177 for arg in argv [1 :]:
176- if not arg .startswith ('-' ):
178+ if not arg .startswith ("-" ):
177179 cmd_name = arg
178180 break
179181 if cmd_name is None :
180- cmd_name = ' help'
181- cmd_args = [' help' ]
182+ cmd_name = " help"
183+ cmd_args = [" help" ]
182184 cmd_args .remove (cmd_name )
183185 cmdclass = _find_command (cmd_name )
184186 from testrepository .ui import cli
187+
185188 ui = cli .UI (cmd_args , stdin , stdout , stderr )
186189 cmd = cmdclass (ui )
187190 result = cmd .execute ()
@@ -203,11 +206,11 @@ def get_command_parser(cmd):
203206 parser = OptionParser ()
204207 for option in cmd .options :
205208 parser .add_option (option )
206- usage = ' %%prog %(cmd)s [options] %(args)s\n \n %(help)s' % {
207- ' args' : ' ' .join (map (lambda x :x .summary (), cmd .args )),
208- ' cmd' : getattr (cmd , ' name' , cmd ),
209- ' help' : getdoc (cmd ),
210- }
209+ usage = " %%prog %(cmd)s [options] %(args)s\n \n %(help)s" % {
210+ " args" : " " .join (map (lambda x : x .summary (), cmd .args )),
211+ " cmd" : getattr (cmd , " name" , cmd ),
212+ " help" : getdoc (cmd ),
213+ }
211214 parser .set_usage (usage )
212215 return parser
213216
0 commit comments