diff options
Diffstat (limited to 'main.py')
| -rwxr-xr-x | main.py | 126 |
1 files changed, 73 insertions, 53 deletions
| @@ -39,7 +39,7 @@ from color import SetDefaultColoring | |||
| 39 | import event_log | 39 | import event_log |
| 40 | from repo_trace import SetTrace | 40 | from repo_trace import SetTrace |
| 41 | from git_command import user_agent | 41 | from git_command import user_agent |
| 42 | from git_config import init_ssh, close_ssh, RepoConfig | 42 | from git_config import RepoConfig |
| 43 | from git_trace2_event_log import EventLog | 43 | from git_trace2_event_log import EventLog |
| 44 | from command import InteractiveCommand | 44 | from command import InteractiveCommand |
| 45 | from command import MirrorSafeCommand | 45 | from command import MirrorSafeCommand |
| @@ -71,7 +71,7 @@ from subcmds import all_commands | |||
| 71 | # | 71 | # |
| 72 | # python-3.6 is in Ubuntu Bionic. | 72 | # python-3.6 is in Ubuntu Bionic. |
| 73 | MIN_PYTHON_VERSION_SOFT = (3, 6) | 73 | MIN_PYTHON_VERSION_SOFT = (3, 6) |
| 74 | MIN_PYTHON_VERSION_HARD = (3, 5) | 74 | MIN_PYTHON_VERSION_HARD = (3, 6) |
| 75 | 75 | ||
| 76 | if sys.version_info.major < 3: | 76 | if sys.version_info.major < 3: |
| 77 | print('repo: error: Python 2 is no longer supported; ' | 77 | print('repo: error: Python 2 is no longer supported; ' |
| @@ -95,6 +95,8 @@ global_options = optparse.OptionParser( | |||
| 95 | add_help_option=False) | 95 | add_help_option=False) |
| 96 | global_options.add_option('-h', '--help', action='store_true', | 96 | global_options.add_option('-h', '--help', action='store_true', |
| 97 | help='show this help message and exit') | 97 | help='show this help message and exit') |
| 98 | global_options.add_option('--help-all', action='store_true', | ||
| 99 | help='show this help message with all subcommands and exit') | ||
| 98 | global_options.add_option('-p', '--paginate', | 100 | global_options.add_option('-p', '--paginate', |
| 99 | dest='pager', action='store_true', | 101 | dest='pager', action='store_true', |
| 100 | help='display command output in the pager') | 102 | help='display command output in the pager') |
| @@ -116,6 +118,10 @@ global_options.add_option('--time', | |||
| 116 | global_options.add_option('--version', | 118 | global_options.add_option('--version', |
| 117 | dest='show_version', action='store_true', | 119 | dest='show_version', action='store_true', |
| 118 | help='display this version of repo') | 120 | help='display this version of repo') |
| 121 | global_options.add_option('--show-toplevel', | ||
| 122 | action='store_true', | ||
| 123 | help='display the path of the top-level directory of ' | ||
| 124 | 'the repo client checkout') | ||
| 119 | global_options.add_option('--event-log', | 125 | global_options.add_option('--event-log', |
| 120 | dest='event_log', action='store', | 126 | dest='event_log', action='store', |
| 121 | help='filename of event log to append timeline to') | 127 | help='filename of event log to append timeline to') |
| @@ -128,34 +134,40 @@ class _Repo(object): | |||
| 128 | self.repodir = repodir | 134 | self.repodir = repodir |
| 129 | self.commands = all_commands | 135 | self.commands = all_commands |
| 130 | 136 | ||
| 137 | def _PrintHelp(self, short: bool = False, all_commands: bool = False): | ||
| 138 | """Show --help screen.""" | ||
| 139 | global_options.print_help() | ||
| 140 | print() | ||
| 141 | if short: | ||
| 142 | commands = ' '.join(sorted(self.commands)) | ||
| 143 | wrapped_commands = textwrap.wrap(commands, width=77) | ||
| 144 | print('Available commands:\n %s' % ('\n '.join(wrapped_commands),)) | ||
| 145 | print('\nRun `repo help <command>` for command-specific details.') | ||
| 146 | print('Bug reports:', Wrapper().BUG_URL) | ||
| 147 | else: | ||
| 148 | cmd = self.commands['help']() | ||
| 149 | if all_commands: | ||
| 150 | cmd.PrintAllCommandsBody() | ||
| 151 | else: | ||
| 152 | cmd.PrintCommonCommandsBody() | ||
| 153 | |||
| 131 | def _ParseArgs(self, argv): | 154 | def _ParseArgs(self, argv): |
| 132 | """Parse the main `repo` command line options.""" | 155 | """Parse the main `repo` command line options.""" |
| 133 | name = None | 156 | for i, arg in enumerate(argv): |
| 134 | glob = [] | 157 | if not arg.startswith('-'): |
| 135 | 158 | name = arg | |
| 136 | for i in range(len(argv)): | 159 | glob = argv[:i] |
| 137 | if not argv[i].startswith('-'): | ||
| 138 | name = argv[i] | ||
| 139 | if i > 0: | ||
| 140 | glob = argv[:i] | ||
| 141 | argv = argv[i + 1:] | 160 | argv = argv[i + 1:] |
| 142 | break | 161 | break |
| 143 | if not name: | 162 | else: |
| 163 | name = None | ||
| 144 | glob = argv | 164 | glob = argv |
| 145 | name = 'help' | ||
| 146 | argv = [] | 165 | argv = [] |
| 147 | gopts, _gargs = global_options.parse_args(glob) | 166 | gopts, _gargs = global_options.parse_args(glob) |
| 148 | 167 | ||
| 149 | name, alias_args = self._ExpandAlias(name) | 168 | if name: |
| 150 | argv = alias_args + argv | 169 | name, alias_args = self._ExpandAlias(name) |
| 151 | 170 | argv = alias_args + argv | |
| 152 | if gopts.help: | ||
| 153 | global_options.print_help() | ||
| 154 | commands = ' '.join(sorted(self.commands)) | ||
| 155 | wrapped_commands = textwrap.wrap(commands, width=77) | ||
| 156 | print('\nAvailable commands:\n %s' % ('\n '.join(wrapped_commands),)) | ||
| 157 | print('\nRun `repo help <command>` for command-specific details.') | ||
| 158 | global_options.exit() | ||
| 159 | 171 | ||
| 160 | return (name, gopts, argv) | 172 | return (name, gopts, argv) |
| 161 | 173 | ||
| @@ -186,32 +198,44 @@ class _Repo(object): | |||
| 186 | 198 | ||
| 187 | if gopts.trace: | 199 | if gopts.trace: |
| 188 | SetTrace() | 200 | SetTrace() |
| 189 | if gopts.show_version: | 201 | |
| 190 | if name == 'help': | 202 | # Handle options that terminate quickly first. |
| 191 | name = 'version' | 203 | if gopts.help or gopts.help_all: |
| 192 | else: | 204 | self._PrintHelp(short=False, all_commands=gopts.help_all) |
| 193 | print('fatal: invalid usage of --version', file=sys.stderr) | 205 | return 0 |
| 194 | return 1 | 206 | elif gopts.show_version: |
| 207 | # Always allow global --version regardless of subcommand validity. | ||
| 208 | name = 'version' | ||
| 209 | elif gopts.show_toplevel: | ||
| 210 | print(os.path.dirname(self.repodir)) | ||
| 211 | return 0 | ||
| 212 | elif not name: | ||
| 213 | # No subcommand specified, so show the help/subcommand. | ||
| 214 | self._PrintHelp(short=True) | ||
| 215 | return 1 | ||
| 195 | 216 | ||
| 196 | SetDefaultColoring(gopts.color) | 217 | SetDefaultColoring(gopts.color) |
| 197 | 218 | ||
| 219 | git_trace2_event_log = EventLog() | ||
| 220 | repo_client = RepoClient(self.repodir) | ||
| 221 | gitc_manifest = None | ||
| 222 | gitc_client_name = gitc_utils.parse_clientdir(os.getcwd()) | ||
| 223 | if gitc_client_name: | ||
| 224 | gitc_manifest = GitcClient(self.repodir, gitc_client_name) | ||
| 225 | repo_client.isGitcClient = True | ||
| 226 | |||
| 198 | try: | 227 | try: |
| 199 | cmd = self.commands[name]() | 228 | cmd = self.commands[name]( |
| 229 | repodir=self.repodir, | ||
| 230 | client=repo_client, | ||
| 231 | manifest=repo_client.manifest, | ||
| 232 | gitc_manifest=gitc_manifest, | ||
| 233 | git_event_log=git_trace2_event_log) | ||
| 200 | except KeyError: | 234 | except KeyError: |
| 201 | print("repo: '%s' is not a repo command. See 'repo help'." % name, | 235 | print("repo: '%s' is not a repo command. See 'repo help'." % name, |
| 202 | file=sys.stderr) | 236 | file=sys.stderr) |
| 203 | return 1 | 237 | return 1 |
| 204 | 238 | ||
| 205 | git_trace2_event_log = EventLog() | ||
| 206 | cmd.repodir = self.repodir | ||
| 207 | cmd.client = RepoClient(cmd.repodir) | ||
| 208 | cmd.manifest = cmd.client.manifest | ||
| 209 | cmd.gitc_manifest = None | ||
| 210 | gitc_client_name = gitc_utils.parse_clientdir(os.getcwd()) | ||
| 211 | if gitc_client_name: | ||
| 212 | cmd.gitc_manifest = GitcClient(cmd.repodir, gitc_client_name) | ||
| 213 | cmd.client.isGitcClient = True | ||
| 214 | |||
| 215 | Editor.globalConfig = cmd.client.globalConfig | 239 | Editor.globalConfig = cmd.client.globalConfig |
| 216 | 240 | ||
| 217 | if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror: | 241 | if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror: |
| @@ -591,20 +615,16 @@ def _Main(argv): | |||
| 591 | 615 | ||
| 592 | repo = _Repo(opt.repodir) | 616 | repo = _Repo(opt.repodir) |
| 593 | try: | 617 | try: |
| 594 | try: | 618 | init_http() |
| 595 | init_ssh() | 619 | name, gopts, argv = repo._ParseArgs(argv) |
| 596 | init_http() | 620 | run = lambda: repo._Run(name, gopts, argv) or 0 |
| 597 | name, gopts, argv = repo._ParseArgs(argv) | 621 | if gopts.trace_python: |
| 598 | run = lambda: repo._Run(name, gopts, argv) or 0 | 622 | import trace |
| 599 | if gopts.trace_python: | 623 | tracer = trace.Trace(count=False, trace=True, timing=True, |
| 600 | import trace | 624 | ignoredirs=set(sys.path[1:])) |
| 601 | tracer = trace.Trace(count=False, trace=True, timing=True, | 625 | result = tracer.runfunc(run) |
| 602 | ignoredirs=set(sys.path[1:])) | 626 | else: |
| 603 | result = tracer.runfunc(run) | 627 | result = run() |
| 604 | else: | ||
| 605 | result = run() | ||
| 606 | finally: | ||
| 607 | close_ssh() | ||
| 608 | except KeyboardInterrupt: | 628 | except KeyboardInterrupt: |
| 609 | print('aborted by user', file=sys.stderr) | 629 | print('aborted by user', file=sys.stderr) |
| 610 | result = 1 | 630 | result = 1 |
