Пример #1
0
def main(argv):
  if len(argv) > 1:
    _Usage(sys.stderr)
    return 1
  if not argv:
    path = git.FindRepoCheckoutRoot(os.getcwd())
    if path is None:
      _Usage(sys.stderr)
      print >> sys.stderr, "We're not in a repo checkout."
      return 1
  else:
    if argv[0] in ('-h', '--help', '--usage'):
      _Usage(sys.stderr)
      return 0
    path = argv[0]
    if not repository.IsARepoRoot(path):
      _Usage(sys.stderr)
      print >> sys.stderr, ("Path %s doesn't point to the root of a repository."
                           % (argv[0],))
      return 1

  print "Cleaning stale git configuration from %s" % (path,)
  configure_repo.FixBrokenExistingRepos(path)
  print "\nAdding git push configuration to %s" % (path,)
  configure_repo.FixExternalRepoPushUrls(path)
Пример #2
0
    def Initialize(cls,
                   root,
                   manifest_url,
                   repo_url=None,
                   repo_branch=None,
                   groups=None):
        """Initialize the checkout if necessary. Otherwise a no-op.

    Args:
      root: The repo root.
      manifest_url: Manifest repository URL.
      repo_url: Repo repository URL. Uses default googlesource repo if None.
      repo_branch: Repo repository branch.
      groups: Repo groups to sync.
    """
        osutils.SafeMakedirs(root)
        if git.FindRepoCheckoutRoot(root) is None:
            logging.notice('Will initialize checkout %s for this run.', root)
            repo_util.Repository.Initialize(root,
                                            manifest_url,
                                            repo_url=repo_url,
                                            repo_branch=repo_branch,
                                            groups=groups)
        else:
            logging.notice('Will use existing checkout %s for this run.', root)
        return cls(root,
                   manifest_url=manifest_url,
                   repo_url=repo_url,
                   groups=groups)
Пример #3
0
    def _Run(self, repo_cmd, cwd=None, capture_output=False):
        """Wrapper for `repo`.

    Args:
      repo_cmd: List of arguments to pass to `repo`.
      cwd: The path to run the command in. Defaults to Repository root.
        Must be within the root.
      capture_output: Whether to capture the output, making it available in the
        CommandResult object, or print it to stdout/err. Defaults to False.

    Returns:
      A CommandResult object.

    Raises:
      NotInRepoError: if cwd is not within the Repository root.
      RunCommandError: if the command failed.
    """
        # Use the checkout's copy of repo so that it doesn't have to be in PATH.
        cmd = [os.path.join(self._repo_dir, 'repo', 'repo')] + repo_cmd
        if cwd is None:
            cwd = self.root
        elif git.FindRepoCheckoutRoot(cwd) != self.root:
            raise NotInRepoError(
                'cannot run `repo` outside of Repository root '
                '(cwd=%r root=%r)' % (cwd, self.root))
        return cros_build_lib.run(cmd,
                                  cwd=cwd,
                                  capture_output=capture_output,
                                  debug_level=logging.DEBUG,
                                  encoding='utf-8')
Пример #4
0
    def Initialize(cls,
                   root,
                   manifest_url,
                   manifest_branch=None,
                   manifest_name=None,
                   mirror=False,
                   reference=None,
                   depth=None,
                   groups=None,
                   repo_url=None,
                   repo_branch=None):
        """Initialize and return a new Repository with `repo init`.

    Args:
      root: Path to the new repo root. Must not be within an existing
        repo repository.
      manifest_url: Manifest repository URL.
      manifest_branch: Manifest branch or revision.
      manifest_name: Initial manifest file from manifest repository.
      mirror: If True, create a repo mirror instead of a checkout.
      reference: Location of a mirror directory to reference.
      depth: Create shallow git clones with the given depth.
      groups: Restrict manifest projects to the given groups.
      repo_url: Repo command repository URL.
      repo_branch: Repo command repository branch or revision.

    Raises:
      Error: root already contained a .repo subdir.
      RunCommandError: `repo init` failed.
    """
        existing_root = git.FindRepoCheckoutRoot(root)
        if existing_root is not None:
            raise Error('cannot init in existing repo %r.' % existing_root)

        # TODO(lannm): Use 'chromite/bootstrap/repo'?
        cmd = ['repo', 'init', '--manifest-url', manifest_url]
        if manifest_branch is not None:
            cmd += ['--manifest-branch', manifest_branch]
        if manifest_name is not None:
            cmd += ['--manifest-name', manifest_name]
        if mirror:
            cmd += ['--mirror']
        if reference is not None:
            if isinstance(reference, Repository):
                reference = reference.root
            cmd += ['--reference', reference]
        if depth is not None:
            cmd += ['--depth', str(depth)]
        if groups is not None:
            cmd += ['--groups', groups]
        if repo_url is not None:
            cmd += ['--repo-url', repo_url]
        if repo_branch is not None:
            cmd += ['--repo-branch', repo_branch]

        repo_dir = os.path.join(root, '.repo')
        warning_msg = 'Removing %r due to `repo init` failures.' % repo_dir
        with _RmDirOnError(repo_dir, msg=warning_msg):
            cros_build_lib.run(cmd, cwd=root)
        return cls(root)
Пример #5
0
def _GetProjectPath(path):
    """Find the absolute path of the git checkout that contains |path|."""
    if git.FindRepoCheckoutRoot(path):
        manifest = git.ManifestCheckout.Cached(path)
        return manifest.FindCheckoutFromPath(path).GetPath(absolute=True)
    else:
        # Maybe they're running on a file outside of a checkout.
        # e.g. cros lint ~/foo.py /tmp/test.py
        return os.path.dirname(path)
Пример #6
0
    def Find(cls, path):
        """Searches for a repo directory and returns a Repository if found.

    Args:
      path: The path where the search starts.
    """
        repo_root = git.FindRepoCheckoutRoot(path)
        if repo_root is None:
            return None
        return cls(repo_root)
Пример #7
0
  def FindCacheDir(cls, _parser, _opts):
    debug_msg = 'Cache dir lookup: looking for %s checkout root.'
    logging.debug(debug_msg, 'repo')
    path = git.FindRepoCheckoutRoot(os.getcwd())
    path = os.path.join(path, cls.REPO_CACHE_DIR) if path else path
    if path is None:
      logging.debug(debug_msg, 'gclient')
      path = gclient.FindGclientCheckoutRoot(os.getcwd())
      path = os.path.join(path, cls.CHROME_CACHE_DIR) if path else path
    if path is None:
      logging.debug(debug_msg, 'git submodule')
      chrome_url = '%s/%s.git' % (constants.GIT_HTTP_URL,
                                  constants.CHROMIUM_SRC_PROJECT)
      path = git.FindGitSubmoduleCheckoutRoot(os.getcwd(), 'origin', chrome_url)
      path = os.path.join(path, cls.CHROME_CACHE_DIR) if path else path
    if path is None:
      path = os.path.join(tempfile.gettempdir(), 'chromeos-cache')

    return path
Пример #8
0
    def _GenerateTestArtifactsInMoblabDisk(self, vms):
        """Generates test artifacts into devserver cache directory in moblab's disk.

    Args:
      vms: A moblab_vm.MoblabVm instance that has been Createed but not Started.

    Returns:
      The absolute path inside moblab VM where the image cache is located.
    """
        with vms.MountedMoblabDiskContext() as disk_dir:
            # If by any chance this path exists, the permission bits are surely
            # nonsense, since 'moblab' user doesn't exist on the host system.
            osutils.RmDir(os.path.join(disk_dir, _MOBLAB_PAYLOAD_CACHE_DIR),
                          ignore_missing=True,
                          sudo=True)
            payloads_dir = os.path.join(disk_dir, _MOBLAB_PAYLOAD_CACHE_DIR,
                                        self._SubDutTargetImage())
            # moblab VM will chown this folder upon boot, so once again permission
            # bits from the host don't matter.
            osutils.SafeMakedirsNonRoot(payloads_dir)
            source_dir = git.FindRepoCheckoutRoot(__file__)
            commands.GeneratePayloads(
                build_root=source_dir,
                target_image_path=os.path.join(
                    self.GetImageDirSymlink(),
                    constants.TEST_IMAGE_BIN,
                ),
                archive_dir=payloads_dir,
                full=True,
                delta=False,
                stateful=True,
            )
            cwd = os.path.abspath(
                os.path.join(self._build_root, 'chroot', 'build',
                             self._current_board,
                             constants.AUTOTEST_BUILD_PATH, '..'))
            commands.BuildAutotestTarballsForHWTest(self._build_root, cwd,
                                                    payloads_dir)
        return os.path.join(_MOBLAB_STATIC_MOUNT_PATH,
                            _MOBLAB_PAYLOAD_CACHE_DIR)
Пример #9
0
def DetermineCheckout(cwd):
    """Gather information on the checkout we are in.

  Returns:
    A CheckoutInfo object with these attributes:
      type: The type of checkout.  Valid values are CHECKOUT_TYPE_*.
      root: The root of the checkout.
      chrome_src_dir: If the checkout is a Chrome checkout, the path to the
        Chrome src/ directory.
  """
    debug_msg = 'Looking for %s checkout root.'
    logging.debug(debug_msg, 'repo')

    # Determine checkout checkout_type and root.
    checkout_type = CHECKOUT_TYPE_UNKNOWN
    root = git.FindRepoCheckoutRoot(cwd)
    checkout_type = CHECKOUT_TYPE_REPO if root else checkout_type
    if root is None:
        logging.debug(debug_msg, 'gclient')
        root = gclient.FindGclientCheckoutRoot(cwd)
        checkout_type = CHECKOUT_TYPE_GCLIENT if root else checkout_type
    if root is None:
        logging.debug(debug_msg, 'git submodule')
        chrome_url = '%s/%s.git' % (constants.PUBLIC_GOB_URL,
                                    constants.CHROMIUM_SRC_PROJECT)
        root = git.FindGitSubmoduleCheckoutRoot(os.getcwd(), 'origin',
                                                chrome_url)
        checkout_type = CHECKOUT_TYPE_SUBMODULE if root else checkout_type
    if root is None:
        checkout_type = CHECKOUT_TYPE_UNKNOWN

    # Determine the chrome src directory.
    chrome_src_dir = None
    if checkout_type == CHECKOUT_TYPE_GCLIENT:
        chrome_src_dir = os.path.join(root, 'src')
    elif checkout_type == CHECKOUT_TYPE_SUBMODULE:
        chrome_src_dir = root

    return CheckoutInfo(checkout_type, root, chrome_src_dir)
Пример #10
0
    def Copy(self, dest_root):
        """Efficiently `cp` the .repo directory, using hardlinks if possible.

    Args:
      dest_root: Path to copy the .repo directory into. Must exist and must
        not already contain a .repo directory.

    Returns:
      A Repository pointing at dest_root.

    Raises:
      Error: if dest_root already contained a .repo subdir.
      RunCommandError: if `cp` failed.
    """
        existing_root = git.FindRepoCheckoutRoot(dest_root)
        if existing_root is not None:
            raise Error('cannot copy into existing repo %r' % existing_root)

        dest_path = os.path.abspath(dest_root)

        with _RmDirOnError(os.path.join(dest_root, '.repo')):
            # First, try to hard link project objects to dest_dir; this may fail if
            # e.g. the src and dest are on different mounts.
            for project in self.List():
                objects_dir = PROJECT_OBJECTS_PATH_FORMAT % project.name
                try:
                    cros_build_lib.run([
                        'cp', '--archive', '--link', '--parents', objects_dir,
                        dest_path
                    ],
                                       debug_level=logging.DEBUG,
                                       capture_output=True,
                                       encoding='utf-8',
                                       extra_env={'LC_MESSAGES': 'C'},
                                       cwd=self.root)
                except cros_build_lib.RunCommandError as e:
                    if 'Invalid cross-device link' in e.result.error:
                        logging.warning(
                            "Can't hard link across devices; aborting linking."
                        )
                        break
                    logging.warning('Copy linking failed: %s', e.result.error)

            # Copy everything that wasn't created by the hard linking above.
            try:
                cros_build_lib.run(
                    ['cp', '--archive', '--no-clobber', '.repo', dest_path],
                    debug_level=logging.DEBUG,
                    capture_output=True,
                    encoding='utf-8',
                    extra_env={'LC_MESSAGES': 'C'},
                    cwd=self.root)
            except cros_build_lib.RunCommandError as e:
                # Despite the --no-clobber, `cp` still complains when trying to copy a
                # file to its existing hard link. Filter these errors from the output
                # to see if there were any real failures.
                errors = e.result.error.splitlines()
                real_errors = [
                    x for x in errors if 'are the same file' not in x
                ]
                if real_errors:
                    e.result.error = '\n'.join(real_errors)
                    raise e
            return Repository(dest_root)
Пример #11
0
def main(argv):

    usage = 'usage: %prog [-d <DEPS.git file>] [command]'
    parser = optparse.OptionParser(usage=usage)

    # TODO(rcui): have -d accept a URL.
    parser.add_option('-d',
                      '--deps',
                      default=None,
                      help=('DEPS file to use. Defaults to '
                            '<chrome_src_root>/.DEPS.git'))
    parser.add_option('--internal',
                      action='store_false',
                      dest='internal',
                      default=True,
                      help='Allow chrome-internal URLs')
    parser.add_option('--runhooks',
                      action='store_true',
                      dest='runhooks',
                      default=False,
                      help="Run hooks as well.")
    parser.add_option('-v',
                      '--verbose',
                      default=False,
                      action='store_true',
                      help='Run with debug output.')
    (options, _inputs) = parser.parse_args(argv)

    # Set cros_build_lib debug level to hide RunCommand spew.
    if options.verbose:
        cros_build_lib.logger.setLevel(logging.DEBUG)
    else:
        cros_build_lib.logger.setLevel(logging.WARNING)

    if cros_build_lib.IsInsideChroot():
        ssh_path = '/usr/bin/ssh_no_update'
        if os.path.isfile(ssh_path):
            os.environ['GIT_SSH'] = ssh_path
        else:
            cros_build_lib.Warning(
                "Can't find %s.  Run build_packages or setup_board to update "
                "your choot." % ssh_path)

    repo_root = git.FindRepoCheckoutRoot(os.getcwd())
    chromium_src_root = os.path.join(repo_root, _CHROMIUM_SRC_ROOT)
    if not os.path.isdir(os.path.join(chromium_src_root, '.git')):
        error_msg = 'chromium checkout not found at %s.\n' % chromium_src_root

        manifest = os.path.join(repo_root, '.repo', 'manifest.xml')
        if os.path.basename(os.path.realpath(manifest)) == 'gerrit-source.xml':
            error_msg += ("Please run repo sync and try again.")
        else:
            link = 'http://www.chromium.org/chromium-os/new-chrome-developer-workflow'
            error_msg += (
                'Detected you are not using gerrit_source.xml.  Follow '
                'instructions at %s to use repo checkout of chrome.' % link)

        cros_build_lib.Die(error_msg)

    # Add DEPS files to parse.
    deps_files_to_parse = []
    if options.deps:
        deps_files_to_parse.append(options.deps)
    else:
        deps_files_to_parse.append(os.path.join(chromium_src_root,
                                                '.DEPS.git'))

    internal_deps = os.path.join(repo_root, _CHROMIUM_SRC_INTERNAL,
                                 '.DEPS.git')
    if os.path.exists(internal_deps):
        deps_files_to_parse.append(internal_deps)

    # Prepare source tree for resetting.
    chromium_root = os.path.join(repo_root, _CHROMIUM_ROOT)
    _CreateCrosSymlink(repo_root)

    # Parse DEPS files and store hooks.
    hook_dicts = []
    for deps_file in deps_files_to_parse:
        deps, merged_deps = GetParsedDeps(deps_file)
        _ResetGitCheckout(repo_root, merged_deps)
        hook_dicts.append(deps.get('hooks', {}))

    # Run hooks after checkout has been reset properly.
    if options.runhooks:
        for hooks in hook_dicts:
            _RunHooks(chromium_root, hooks)
Пример #12
0
def main(argv=None):
    if argv is None:
        argv = sys.argv[1:]

    usage = 'usage: %prog'
    parser = commandline.OptionParser(usage=usage)

    parser.add_option('-r',
                      '--testroot',
                      action='store',
                      type='path',
                      help=('Directory where test checkout is stored.'))
    parser.add_option('-f',
                      '--force',
                      default=True,
                      action='store_false',
                      dest='dryrun',
                      help='If enabled, allow committing.')
    parser.add_option(
        '--internal-manifest-url',
        default='%s/%s' %
        (constants.GERRIT_INT_SSH_URL, _INTERNAL_MANIFEST_PROJECT),
        help='Url to fetch the internal manifest from.')
    parser.add_option('--external-manifest-url',
                      default='%s/%s' %
                      (constants.GERRIT_SSH_URL, _EXTERNAL_MANIFEST_PROJECT),
                      help='Url to fetch the external manifest from.')
    parser.add_option('-v',
                      '--verbose',
                      default=False,
                      action='store_true',
                      help='Run with debug output.')
    parser.add_option('--reference',
                      default=None,
                      help="Repository to reference")
    (options, _inputs) = parser.parse_args(argv)

    if not options.testroot:
        parser.error('Please specify a test root!')

    # Set cros_build_lib debug level to hide RunCommand spew.
    logging.getLogger().setLevel(
        logging.DEBUG if options.verbose else logging.WARN)

    if options.reference is None:
        options.reference = git.FindRepoCheckoutRoot(os.getcwd())

    def process_target(internal, manifest_url, reference):
        subdir = 'internal' if internal else 'external'
        root = os.path.join(options.testroot, subdir)
        repo_dir = os.path.join(root, 'repo')
        osutils.SafeMakedirs(repo_dir)
        manifest_dir = os.path.join(root, 'manifest')

        if os.path.exists(manifest_dir):
            git.CleanAndCheckoutUpstream(manifest_dir)
            git.RunGit(manifest_dir,
                       ['checkout', '-B', 'master', '-t', 'origin/master'])
        else:
            repository.CloneGitRepo(manifest_dir, manifest_url)
        m = Manifest(repo_dir,
                     manifest_dir,
                     internal,
                     reference=reference,
                     dryrun=options.dryrun)
        m.PerformUpdate()

        if options.dryrun:
            print "%s manifest is now:" % subdir
            print osutils.ReadFile(os.path.join(manifest_dir, MANIFEST_FILE))

    # Process internal first so that any references to the passed in repo
    # are utilized in full, w/ external then slaving from the new internal.
    process_target(True, options.internal_manifest_url, options.reference)
    process_target(False, options.external_manifest_url,
                   os.path.join(options.testroot, 'internal', 'repo'))