2008-10-21 14:00:00 +00:00
|
|
|
#
|
|
|
|
# Copyright (C) 2008 The Android Open Source Project
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import optparse
|
2012-04-16 18:02:21 +00:00
|
|
|
import platform
|
2012-03-29 03:15:45 +00:00
|
|
|
import re
|
2008-10-21 14:00:00 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from error import NoSuchProjectError
|
2012-03-29 03:15:45 +00:00
|
|
|
from error import InvalidProjectGroupsError
|
2008-10-21 14:00:00 +00:00
|
|
|
|
2012-11-16 00:33:39 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
class Command(object):
|
|
|
|
"""Base class for any command line action in repo.
|
|
|
|
"""
|
|
|
|
|
|
|
|
common = False
|
|
|
|
manifest = None
|
|
|
|
_optparse = None
|
|
|
|
|
2009-04-18 20:49:13 +00:00
|
|
|
def WantPager(self, opt):
|
|
|
|
return False
|
|
|
|
|
2012-11-16 00:33:39 +00:00
|
|
|
def ReadEnvironmentOptions(self, opts):
|
|
|
|
""" Set options from environment variables. """
|
|
|
|
|
|
|
|
env_options = self._RegisteredEnvironmentOptions()
|
|
|
|
|
|
|
|
for env_key, opt_key in env_options.items():
|
|
|
|
# Get the user-set option value if any
|
|
|
|
opt_value = getattr(opts, opt_key)
|
|
|
|
|
|
|
|
# If the value is set, it means the user has passed it as a command
|
|
|
|
# line option, and we should use that. Otherwise we can try to set it
|
|
|
|
# with the value from the corresponding environment variable.
|
|
|
|
if opt_value is not None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
env_value = os.environ.get(env_key)
|
|
|
|
if env_value is not None:
|
|
|
|
setattr(opts, opt_key, env_value)
|
|
|
|
|
|
|
|
return opts
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
@property
|
|
|
|
def OptionParser(self):
|
|
|
|
if self._optparse is None:
|
|
|
|
try:
|
|
|
|
me = 'repo %s' % self.NAME
|
|
|
|
usage = self.helpUsage.strip().replace('%prog', me)
|
|
|
|
except AttributeError:
|
|
|
|
usage = 'repo %s' % self.NAME
|
|
|
|
self._optparse = optparse.OptionParser(usage = usage)
|
|
|
|
self._Options(self._optparse)
|
|
|
|
return self._optparse
|
|
|
|
|
|
|
|
def _Options(self, p):
|
|
|
|
"""Initialize the option parser.
|
|
|
|
"""
|
|
|
|
|
2012-11-16 00:33:39 +00:00
|
|
|
def _RegisteredEnvironmentOptions(self):
|
|
|
|
"""Get options that can be set from environment variables.
|
|
|
|
|
|
|
|
Return a dictionary mapping environment variable name
|
|
|
|
to option key name that it can override.
|
|
|
|
|
|
|
|
Example: {'REPO_MY_OPTION': 'my_option'}
|
|
|
|
|
|
|
|
Will allow the option with key value 'my_option' to be set
|
|
|
|
from the value in the environment variable named 'REPO_MY_OPTION'.
|
|
|
|
|
|
|
|
Note: This does not work properly for options that are explicitly
|
|
|
|
set to None by the user, or options that are defined with a
|
|
|
|
default value other than None.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return {}
|
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
def Usage(self):
|
|
|
|
"""Display usage and terminate.
|
|
|
|
"""
|
|
|
|
self.OptionParser.print_usage()
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def Execute(self, opt, args):
|
|
|
|
"""Perform the action, after option parsing is complete.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
2012-04-16 17:36:08 +00:00
|
|
|
|
Represent git-submodule as nested projects, take 2
(Previous submission of this change broke Android buildbot due to
incorrect regular expression for parsing git-config output. During
investigation, we also found that Android, which pulls Chromium, has a
workaround for Chromium's submodules; its manifest includes Chromium's
submodules. This new change, in addition to fixing the regex, also
take this type of workarounds into consideration; it adds a new
attribute that makes repo not fetch submodules unless submodules have a
project element defined in the manifest, or this attribute is
overridden by a parent project element or by the default element.)
We need a representation of git-submodule in repo; otherwise repo will
not sync submodules, and leave workspace in a broken state. Of course
this will not be a problem if all projects are owned by the owner of the
manifest file, who may simply choose not to use git-submodule in all
projects. However, this is not possible in practice because manifest
file owner is unlikely to own all upstream projects.
As git submodules are simply git repositories, it is natural to treat
them as plain repo projects that live inside a repo project. That is,
we could use recursively declared projects to denote the is-submodule
relation of git repositories.
The behavior of repo remains the same to projects that do not have a
sub-project within. As for parent projects, repo fetches them and their
sub-projects as normal projects, and then checks out subprojects at the
commit specified in parent's commit object. The sub-project is fetched
at a path relative to parent project's working directory; so the path
specified in manifest file should match that of .gitmodules file.
If a submodule is not registered in repo manifest, repo will derive its
properties from itself and its parent project, which might not always be
correct. In such cases, the subproject is called a derived subproject.
To a user, a sub-project is merely a git-submodule; so all tips of
working with a git-submodule apply here, too. For example, you should
not run `repo sync` in a parent repository if its submodule is dirty.
Change-Id: I4b8344c1b9ccad2f58ad304573133e5d52e1faef
2012-01-11 03:28:42 +00:00
|
|
|
def _ResetPathToProjectMap(self, projects):
|
|
|
|
self._by_path = dict((p.worktree, p) for p in projects)
|
|
|
|
|
|
|
|
def _UpdatePathToProjectMap(self, project):
|
|
|
|
self._by_path[project.worktree] = project
|
|
|
|
|
|
|
|
def _GetProjectByPath(self, path):
|
|
|
|
project = None
|
|
|
|
if os.path.exists(path):
|
|
|
|
oldpath = None
|
|
|
|
while path \
|
|
|
|
and path != oldpath \
|
|
|
|
and path != self.manifest.topdir:
|
|
|
|
try:
|
|
|
|
project = self._by_path[path]
|
|
|
|
break
|
|
|
|
except KeyError:
|
|
|
|
oldpath = path
|
|
|
|
path = os.path.dirname(path)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
project = self._by_path[path]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
return project
|
|
|
|
|
|
|
|
def GetProjects(self, args, missing_ok=False, submodules_ok=False):
|
2008-10-21 14:00:00 +00:00
|
|
|
"""A list of projects that match the arguments.
|
|
|
|
"""
|
2012-09-24 03:15:13 +00:00
|
|
|
all_projects = self.manifest.projects
|
2008-10-21 14:00:00 +00:00
|
|
|
result = []
|
|
|
|
|
2012-03-29 03:15:45 +00:00
|
|
|
mp = self.manifest.manifestProject
|
|
|
|
|
|
|
|
groups = mp.config.GetString('manifest.groups')
|
2012-04-23 20:41:58 +00:00
|
|
|
if not groups:
|
2012-08-13 20:11:18 +00:00
|
|
|
groups = 'all,-notdefault,platform-' + platform.system().lower()
|
2012-10-25 03:23:11 +00:00
|
|
|
groups = [x for x in re.split(r'[,\s]+', groups) if x]
|
2012-03-29 03:15:45 +00:00
|
|
|
|
2008-10-21 14:00:00 +00:00
|
|
|
if not args:
|
Represent git-submodule as nested projects, take 2
(Previous submission of this change broke Android buildbot due to
incorrect regular expression for parsing git-config output. During
investigation, we also found that Android, which pulls Chromium, has a
workaround for Chromium's submodules; its manifest includes Chromium's
submodules. This new change, in addition to fixing the regex, also
take this type of workarounds into consideration; it adds a new
attribute that makes repo not fetch submodules unless submodules have a
project element defined in the manifest, or this attribute is
overridden by a parent project element or by the default element.)
We need a representation of git-submodule in repo; otherwise repo will
not sync submodules, and leave workspace in a broken state. Of course
this will not be a problem if all projects are owned by the owner of the
manifest file, who may simply choose not to use git-submodule in all
projects. However, this is not possible in practice because manifest
file owner is unlikely to own all upstream projects.
As git submodules are simply git repositories, it is natural to treat
them as plain repo projects that live inside a repo project. That is,
we could use recursively declared projects to denote the is-submodule
relation of git repositories.
The behavior of repo remains the same to projects that do not have a
sub-project within. As for parent projects, repo fetches them and their
sub-projects as normal projects, and then checks out subprojects at the
commit specified in parent's commit object. The sub-project is fetched
at a path relative to parent project's working directory; so the path
specified in manifest file should match that of .gitmodules file.
If a submodule is not registered in repo manifest, repo will derive its
properties from itself and its parent project, which might not always be
correct. In such cases, the subproject is called a derived subproject.
To a user, a sub-project is merely a git-submodule; so all tips of
working with a git-submodule apply here, too. For example, you should
not run `repo sync` in a parent repository if its submodule is dirty.
Change-Id: I4b8344c1b9ccad2f58ad304573133e5d52e1faef
2012-01-11 03:28:42 +00:00
|
|
|
all_projects_list = all_projects.values()
|
|
|
|
derived_projects = {}
|
|
|
|
for project in all_projects_list:
|
|
|
|
if submodules_ok or project.sync_s:
|
|
|
|
derived_projects.update((p.name, p)
|
|
|
|
for p in project.GetDerivedSubprojects())
|
|
|
|
all_projects_list.extend(derived_projects.values())
|
|
|
|
for project in all_projects_list:
|
2012-03-29 03:15:45 +00:00
|
|
|
if ((missing_ok or project.Exists) and
|
|
|
|
project.MatchesGroups(groups)):
|
2008-10-21 14:00:00 +00:00
|
|
|
result.append(project)
|
|
|
|
else:
|
Represent git-submodule as nested projects, take 2
(Previous submission of this change broke Android buildbot due to
incorrect regular expression for parsing git-config output. During
investigation, we also found that Android, which pulls Chromium, has a
workaround for Chromium's submodules; its manifest includes Chromium's
submodules. This new change, in addition to fixing the regex, also
take this type of workarounds into consideration; it adds a new
attribute that makes repo not fetch submodules unless submodules have a
project element defined in the manifest, or this attribute is
overridden by a parent project element or by the default element.)
We need a representation of git-submodule in repo; otherwise repo will
not sync submodules, and leave workspace in a broken state. Of course
this will not be a problem if all projects are owned by the owner of the
manifest file, who may simply choose not to use git-submodule in all
projects. However, this is not possible in practice because manifest
file owner is unlikely to own all upstream projects.
As git submodules are simply git repositories, it is natural to treat
them as plain repo projects that live inside a repo project. That is,
we could use recursively declared projects to denote the is-submodule
relation of git repositories.
The behavior of repo remains the same to projects that do not have a
sub-project within. As for parent projects, repo fetches them and their
sub-projects as normal projects, and then checks out subprojects at the
commit specified in parent's commit object. The sub-project is fetched
at a path relative to parent project's working directory; so the path
specified in manifest file should match that of .gitmodules file.
If a submodule is not registered in repo manifest, repo will derive its
properties from itself and its parent project, which might not always be
correct. In such cases, the subproject is called a derived subproject.
To a user, a sub-project is merely a git-submodule; so all tips of
working with a git-submodule apply here, too. For example, you should
not run `repo sync` in a parent repository if its submodule is dirty.
Change-Id: I4b8344c1b9ccad2f58ad304573133e5d52e1faef
2012-01-11 03:28:42 +00:00
|
|
|
self._ResetPathToProjectMap(all_projects.values())
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
for arg in args:
|
2012-09-24 03:15:13 +00:00
|
|
|
project = all_projects.get(arg)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
if not project:
|
2011-01-10 01:31:57 +00:00
|
|
|
path = os.path.abspath(arg).replace('\\', '/')
|
Represent git-submodule as nested projects, take 2
(Previous submission of this change broke Android buildbot due to
incorrect regular expression for parsing git-config output. During
investigation, we also found that Android, which pulls Chromium, has a
workaround for Chromium's submodules; its manifest includes Chromium's
submodules. This new change, in addition to fixing the regex, also
take this type of workarounds into consideration; it adds a new
attribute that makes repo not fetch submodules unless submodules have a
project element defined in the manifest, or this attribute is
overridden by a parent project element or by the default element.)
We need a representation of git-submodule in repo; otherwise repo will
not sync submodules, and leave workspace in a broken state. Of course
this will not be a problem if all projects are owned by the owner of the
manifest file, who may simply choose not to use git-submodule in all
projects. However, this is not possible in practice because manifest
file owner is unlikely to own all upstream projects.
As git submodules are simply git repositories, it is natural to treat
them as plain repo projects that live inside a repo project. That is,
we could use recursively declared projects to denote the is-submodule
relation of git repositories.
The behavior of repo remains the same to projects that do not have a
sub-project within. As for parent projects, repo fetches them and their
sub-projects as normal projects, and then checks out subprojects at the
commit specified in parent's commit object. The sub-project is fetched
at a path relative to parent project's working directory; so the path
specified in manifest file should match that of .gitmodules file.
If a submodule is not registered in repo manifest, repo will derive its
properties from itself and its parent project, which might not always be
correct. In such cases, the subproject is called a derived subproject.
To a user, a sub-project is merely a git-submodule; so all tips of
working with a git-submodule apply here, too. For example, you should
not run `repo sync` in a parent repository if its submodule is dirty.
Change-Id: I4b8344c1b9ccad2f58ad304573133e5d52e1faef
2012-01-11 03:28:42 +00:00
|
|
|
project = self._GetProjectByPath(path)
|
|
|
|
|
|
|
|
# If it's not a derived project, update path->project mapping and
|
|
|
|
# search again, as arg might actually point to a derived subproject.
|
|
|
|
if (project and not project.Derived and
|
|
|
|
(submodules_ok or project.sync_s)):
|
|
|
|
search_again = False
|
|
|
|
for subproject in project.GetDerivedSubprojects():
|
|
|
|
self._UpdatePathToProjectMap(subproject)
|
|
|
|
search_again = True
|
|
|
|
if search_again:
|
|
|
|
project = self._GetProjectByPath(path) or project
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
if not project:
|
|
|
|
raise NoSuchProjectError(arg)
|
|
|
|
if not missing_ok and not project.Exists:
|
|
|
|
raise NoSuchProjectError(arg)
|
2012-03-29 03:15:45 +00:00
|
|
|
if not project.MatchesGroups(groups):
|
|
|
|
raise InvalidProjectGroupsError(arg)
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
result.append(project)
|
|
|
|
|
|
|
|
def _getpath(x):
|
|
|
|
return x.relpath
|
|
|
|
result.sort(key=_getpath)
|
|
|
|
return result
|
|
|
|
|
2012-10-22 03:50:15 +00:00
|
|
|
# pylint: disable=W0223
|
2012-10-11 07:44:48 +00:00
|
|
|
# Pylint warns that the `InteractiveCommand` and `PagedCommand` classes do not
|
|
|
|
# override method `Execute` which is abstract in `Command`. Since that method
|
|
|
|
# is always implemented in classes derived from `InteractiveCommand` and
|
|
|
|
# `PagedCommand`, this warning can be suppressed.
|
2008-10-21 14:00:00 +00:00
|
|
|
class InteractiveCommand(Command):
|
|
|
|
"""Command which requires user interaction on the tty and
|
|
|
|
must not run within a pager, even if the user asks to.
|
|
|
|
"""
|
2009-04-18 20:49:13 +00:00
|
|
|
def WantPager(self, opt):
|
|
|
|
return False
|
2008-10-21 14:00:00 +00:00
|
|
|
|
|
|
|
class PagedCommand(Command):
|
|
|
|
"""Command which defaults to output in a pager, as its
|
|
|
|
display tends to be larger than one screen full.
|
|
|
|
"""
|
2009-04-18 20:49:13 +00:00
|
|
|
def WantPager(self, opt):
|
|
|
|
return True
|
2009-03-04 01:47:06 +00:00
|
|
|
|
2012-10-22 03:50:15 +00:00
|
|
|
# pylint: enable=W0223
|
2012-10-11 07:44:48 +00:00
|
|
|
|
2009-03-04 01:47:06 +00:00
|
|
|
class MirrorSafeCommand(object):
|
|
|
|
"""Command permits itself to run within a mirror,
|
|
|
|
and does not require a working directory.
|
|
|
|
"""
|