Example #1
0
    def _Config(self, argv):
        name = None
        glob = []

        for i in range(len(argv)):
            if not argv[i].startswith('-'):
                name = argv[i]
                if i > 0:
                    glob = argv[:i]
                argv = argv[i + 1:]
                break
        if not name:
            glob = argv
            name = 'help'
            argv = []

        gopts, _gargs = global_options.parse_args(glob)

        if gopts.trace:
            SetTrace()
        if gopts.show_version:
            if name == 'help':
                name = 'version'
            else:
                print('fatal: invalid usage of --version', file=sys.stderr)
                return 1

        try:
            cmd = self.commands[name]
        except KeyError:
            print("repo: '%s' is not a repo command.  See 'repo help'." % name,
                  file=sys.stderr)
            return 1

        cmd.repodir = self.repodir
        cmd.manifest = XmlManifest(cmd.repodir)
        Editor.globalConfig = cmd.manifest.globalConfig

        if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
            print("fatal: '%s' requires a working directory" % name,
                  file=sys.stderr)
            return 1

        copts, cargs = cmd.OptionParser.parse_args(argv)
        copts = cmd.ReadEnvironmentOptions(copts)

        self.config = name, cmd, gopts, _gargs, copts, cargs, argv
        return 0
Example #2
0
    def _Run(self, name, gopts, argv):
        """Execute the requested subcommand."""
        result = 0

        if gopts.trace:
            SetTrace()
        if gopts.show_version:
            if name == 'help':
                name = 'version'
            else:
                print('fatal: invalid usage of --version', file=sys.stderr)
                return 1

        SetDefaultColoring(gopts.color)

        try:
            cmd = self.commands[name]
        except KeyError:
            print("repo: '%s' is not a repo command.  See 'repo help'." % name,
                  file=sys.stderr)
            return 1

        cmd.repodir = self.repodir
        cmd.manifest = XmlManifest(cmd.repodir)
        cmd.gitc_manifest = None
        gitc_client_name = gitc_utils.parse_clientdir(os.getcwd())
        if gitc_client_name:
            cmd.gitc_manifest = GitcManifest(cmd.repodir, gitc_client_name)
            cmd.manifest.isGitcClient = True

        Editor.globalConfig = cmd.manifest.globalConfig

        if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
            print("fatal: '%s' requires a working directory" % name,
                  file=sys.stderr)
            return 1

        if isinstance(cmd, GitcAvailableCommand
                      ) and not gitc_utils.get_gitc_manifest_dir():
            print("fatal: '%s' requires GITC to be available" % name,
                  file=sys.stderr)
            return 1

        if isinstance(cmd, GitcClientCommand) and not gitc_client_name:
            print("fatal: '%s' requires a GITC client" % name, file=sys.stderr)
            return 1

        try:
            copts, cargs = cmd.OptionParser.parse_args(argv)
            copts = cmd.ReadEnvironmentOptions(copts)
        except NoManifestException as e:
            print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)),
                  file=sys.stderr)
            print('error: manifest missing or unreadable -- please run init',
                  file=sys.stderr)
            return 1

        if gopts.pager is not False and not isinstance(cmd,
                                                       InteractiveCommand):
            config = cmd.manifest.globalConfig
            if gopts.pager:
                use_pager = True
            else:
                use_pager = config.GetBoolean('pager.%s' % name)
                if use_pager is None:
                    use_pager = cmd.WantPager(copts)
            if use_pager:
                RunPager(config)

        start = time.time()
        cmd_event = cmd.event_log.Add(name, event_log.TASK_COMMAND, start)
        cmd.event_log.SetParent(cmd_event)
        try:
            cmd.ValidateOptions(copts, cargs)
            result = cmd.Execute(copts, cargs)
        except (DownloadError, ManifestInvalidRevisionError,
                NoManifestException) as e:
            print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)),
                  file=sys.stderr)
            if isinstance(e, NoManifestException):
                print(
                    'error: manifest missing or unreadable -- please run init',
                    file=sys.stderr)
            result = 1
        except NoSuchProjectError as e:
            if e.name:
                print('error: project %s not found' % e.name, file=sys.stderr)
            else:
                print('error: no project in current directory',
                      file=sys.stderr)
            result = 1
        except InvalidProjectGroupsError as e:
            if e.name:
                print('error: project group must be enabled for project %s' %
                      e.name,
                      file=sys.stderr)
            else:
                print(
                    'error: project group must be enabled for the project in the current directory',
                    file=sys.stderr)
            result = 1
        except SystemExit as e:
            if e.code:
                result = e.code
            raise
        finally:
            finish = time.time()
            elapsed = finish - start
            hours, remainder = divmod(elapsed, 3600)
            minutes, seconds = divmod(remainder, 60)
            if gopts.time:
                if hours == 0:
                    print('real\t%dm%.3fs' % (minutes, seconds),
                          file=sys.stderr)
                else:
                    print('real\t%dh%dm%.3fs' % (hours, minutes, seconds),
                          file=sys.stderr)

            cmd.event_log.FinishEvent(cmd_event, finish, result is None
                                      or result == 0)
            if gopts.event_log:
                cmd.event_log.Write(
                    os.path.abspath(os.path.expanduser(gopts.event_log)))

        return result
Example #3
0
    def _Run(self, name, gopts, argv):
        """Execute the requested subcommand."""
        result = 0

        if gopts.trace:
            SetTrace()

        # Handle options that terminate quickly first.
        if gopts.help or gopts.help_all:
            self._PrintHelp(short=False, all_commands=gopts.help_all)
            return 0
        elif gopts.show_version:
            # Always allow global --version regardless of subcommand validity.
            name = 'version'
        elif gopts.show_toplevel:
            print(os.path.dirname(self.repodir))
            return 0
        elif not name:
            # No subcommand specified, so show the help/subcommand.
            self._PrintHelp(short=True)
            return 1

        SetDefaultColoring(gopts.color)

        git_trace2_event_log = EventLog()
        repo_client = RepoClient(self.repodir)
        gitc_manifest = None
        gitc_client_name = gitc_utils.parse_clientdir(os.getcwd())
        if gitc_client_name:
            gitc_manifest = GitcClient(self.repodir, gitc_client_name)
            repo_client.isGitcClient = True

        try:
            cmd = self.commands[name](repodir=self.repodir,
                                      client=repo_client,
                                      manifest=repo_client.manifest,
                                      gitc_manifest=gitc_manifest,
                                      git_event_log=git_trace2_event_log)
        except KeyError:
            print("repo: '%s' is not a repo command.  See 'repo help'." % name,
                  file=sys.stderr)
            return 1

        Editor.globalConfig = cmd.client.globalConfig

        if not isinstance(cmd, MirrorSafeCommand) and cmd.manifest.IsMirror:
            print("fatal: '%s' requires a working directory" % name,
                  file=sys.stderr)
            return 1

        if isinstance(cmd, GitcAvailableCommand
                      ) and not gitc_utils.get_gitc_manifest_dir():
            print("fatal: '%s' requires GITC to be available" % name,
                  file=sys.stderr)
            return 1

        if isinstance(cmd, GitcClientCommand) and not gitc_client_name:
            print("fatal: '%s' requires a GITC client" % name, file=sys.stderr)
            return 1

        try:
            copts, cargs = cmd.OptionParser.parse_args(argv)
            copts = cmd.ReadEnvironmentOptions(copts)
        except NoManifestException as e:
            print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)),
                  file=sys.stderr)
            print('error: manifest missing or unreadable -- please run init',
                  file=sys.stderr)
            return 1

        if gopts.pager is not False and not isinstance(cmd,
                                                       InteractiveCommand):
            config = cmd.client.globalConfig
            if gopts.pager:
                use_pager = True
            else:
                use_pager = config.GetBoolean('pager.%s' % name)
                if use_pager is None:
                    use_pager = cmd.WantPager(copts)
            if use_pager:
                RunPager(config)

        start = time.time()
        cmd_event = cmd.event_log.Add(name, event_log.TASK_COMMAND, start)
        cmd.event_log.SetParent(cmd_event)
        git_trace2_event_log.StartEvent()
        git_trace2_event_log.CommandEvent(name='repo', subcommands=[name])

        try:
            cmd.CommonValidateOptions(copts, cargs)
            cmd.ValidateOptions(copts, cargs)
            result = cmd.Execute(copts, cargs)
        except (DownloadError, ManifestInvalidRevisionError,
                NoManifestException) as e:
            print('error: in `%s`: %s' % (' '.join([name] + argv), str(e)),
                  file=sys.stderr)
            if isinstance(e, NoManifestException):
                print(
                    'error: manifest missing or unreadable -- please run init',
                    file=sys.stderr)
            result = 1
        except NoSuchProjectError as e:
            if e.name:
                print('error: project %s not found' % e.name, file=sys.stderr)
            else:
                print('error: no project in current directory',
                      file=sys.stderr)
            result = 1
        except InvalidProjectGroupsError as e:
            if e.name:
                print('error: project group must be enabled for project %s' %
                      e.name,
                      file=sys.stderr)
            else:
                print(
                    'error: project group must be enabled for the project in the current directory',
                    file=sys.stderr)
            result = 1
        except SystemExit as e:
            if e.code:
                result = e.code
            raise
        finally:
            finish = time.time()
            elapsed = finish - start
            hours, remainder = divmod(elapsed, 3600)
            minutes, seconds = divmod(remainder, 60)
            if gopts.time:
                if hours == 0:
                    print('real\t%dm%.3fs' % (minutes, seconds),
                          file=sys.stderr)
                else:
                    print('real\t%dh%dm%.3fs' % (hours, minutes, seconds),
                          file=sys.stderr)

            cmd.event_log.FinishEvent(cmd_event, finish, result is None
                                      or result == 0)
            git_trace2_event_log.DefParamRepoEvents(
                cmd.manifest.manifestProject.config.DumpConfigDict())
            git_trace2_event_log.ExitEvent(result)

            if gopts.event_log:
                cmd.event_log.Write(
                    os.path.abspath(os.path.expanduser(gopts.event_log)))

            git_trace2_event_log.Write(gopts.git_trace2_event_log)
        return result