2015-08-10 20:23:23 +00:00
|
|
|
# Copyright (C) 2015 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
|
2021-02-16 20:01:39 +00:00
|
|
|
import multiprocessing
|
2015-09-08 20:27:20 +00:00
|
|
|
import platform
|
|
|
|
import re
|
2015-08-20 07:37:09 +00:00
|
|
|
import sys
|
2015-08-20 19:19:28 +00:00
|
|
|
import time
|
2015-08-10 20:23:23 +00:00
|
|
|
|
|
|
|
import git_command
|
|
|
|
import git_config
|
2015-08-28 21:25:44 +00:00
|
|
|
import wrapper
|
2015-08-10 20:23:23 +00:00
|
|
|
|
2016-06-24 19:17:14 +00:00
|
|
|
from error import ManifestParseError
|
|
|
|
|
2016-08-23 21:06:59 +00:00
|
|
|
NUM_BATCH_RETRIEVE_REVISIONID = 32
|
2015-08-10 20:23:23 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2015-08-28 21:25:44 +00:00
|
|
|
def get_gitc_manifest_dir():
|
|
|
|
return wrapper.Wrapper().get_gitc_manifest_dir()
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2015-08-20 19:19:28 +00:00
|
|
|
def parse_clientdir(gitc_fs_path):
|
2015-10-06 22:23:19 +00:00
|
|
|
return wrapper.Wrapper().gitc_parse_clientdir(gitc_fs_path)
|
2015-08-20 19:19:28 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2021-02-16 20:01:39 +00:00
|
|
|
def _get_project_revision(args):
|
|
|
|
"""Worker for _set_project_revisions to lookup one project remote."""
|
|
|
|
(i, url, expr) = args
|
|
|
|
gitcmd = git_command.GitCommand(
|
|
|
|
None, ['ls-remote', url, expr], capture_stdout=True, cwd='/tmp')
|
|
|
|
rc = gitcmd.Wait()
|
|
|
|
return (i, rc, gitcmd.stdout.split('\t', 1)[0])
|
|
|
|
|
|
|
|
|
2015-08-10 20:23:23 +00:00
|
|
|
def _set_project_revisions(projects):
|
|
|
|
"""Sets the revisionExpr for a list of projects.
|
|
|
|
|
|
|
|
Because of the limit of open file descriptors allowed, length of projects
|
|
|
|
should not be overly large. Recommend calling this function multiple times
|
|
|
|
with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects.
|
|
|
|
|
2020-12-11 08:26:42 +00:00
|
|
|
Args:
|
|
|
|
projects: List of project objects to set the revionExpr for.
|
2015-08-10 20:23:23 +00:00
|
|
|
"""
|
|
|
|
# Retrieve the commit id for each project based off of it's current
|
|
|
|
# revisionExpr and it is not already a commit id.
|
2021-02-16 20:01:39 +00:00
|
|
|
with multiprocessing.Pool(NUM_BATCH_RETRIEVE_REVISIONID) as pool:
|
|
|
|
results_iter = pool.imap_unordered(
|
|
|
|
_get_project_revision,
|
|
|
|
((i, project.remote.url, project.revisionExpr)
|
|
|
|
for i, project in enumerate(projects)
|
|
|
|
if not git_config.IsId(project.revisionExpr)),
|
|
|
|
chunksize=8)
|
|
|
|
for (i, rc, revisionExpr) in results_iter:
|
|
|
|
project = projects[i]
|
|
|
|
if rc:
|
|
|
|
print('FATAL: Failed to retrieve revisionExpr for %s' % project.name)
|
|
|
|
pool.terminate()
|
|
|
|
sys.exit(1)
|
|
|
|
if not revisionExpr:
|
|
|
|
pool.terminate()
|
|
|
|
raise ManifestParseError('Invalid SHA-1 revision project %s (%s)' %
|
|
|
|
(project.remote.url, project.revisionExpr))
|
|
|
|
project.revisionExpr = revisionExpr
|
2015-08-10 20:23:23 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2015-09-08 20:27:20 +00:00
|
|
|
def _manifest_groups(manifest):
|
|
|
|
"""Returns the manifest group string that should be synced
|
|
|
|
|
|
|
|
This is the same logic used by Command.GetProjects(), which is used during
|
|
|
|
repo sync
|
|
|
|
|
2020-12-11 08:26:42 +00:00
|
|
|
Args:
|
|
|
|
manifest: The XmlManifest object
|
2015-09-08 20:27:20 +00:00
|
|
|
"""
|
|
|
|
mp = manifest.manifestProject
|
|
|
|
groups = mp.config.GetString('manifest.groups')
|
|
|
|
if not groups:
|
|
|
|
groups = 'default,platform-' + platform.system().lower()
|
|
|
|
return groups
|
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2015-09-08 20:27:20 +00:00
|
|
|
def generate_gitc_manifest(gitc_manifest, manifest, paths=None):
|
2015-08-10 20:23:23 +00:00
|
|
|
"""Generate a manifest for shafsd to use for this GITC client.
|
|
|
|
|
2020-12-11 08:26:42 +00:00
|
|
|
Args:
|
|
|
|
gitc_manifest: Current gitc manifest, or None if there isn't one yet.
|
|
|
|
manifest: A GitcManifest object loaded with the current repo manifest.
|
|
|
|
paths: List of project paths we want to update.
|
2015-08-10 20:23:23 +00:00
|
|
|
"""
|
2015-09-08 20:27:20 +00:00
|
|
|
|
2015-08-10 20:23:23 +00:00
|
|
|
print('Generating GITC Manifest by fetching revision SHAs for each '
|
|
|
|
'project.')
|
2015-09-08 20:27:20 +00:00
|
|
|
if paths is None:
|
2019-06-13 06:13:23 +00:00
|
|
|
paths = list(manifest.paths.keys())
|
2015-09-08 20:27:20 +00:00
|
|
|
|
|
|
|
groups = [x for x in re.split(r'[,\s]+', _manifest_groups(manifest)) if x]
|
|
|
|
|
|
|
|
# Convert the paths to projects, and filter them to the matched groups.
|
|
|
|
projects = [manifest.paths[p] for p in paths]
|
|
|
|
projects = [p for p in projects if p.MatchesGroups(groups)]
|
|
|
|
|
|
|
|
if gitc_manifest is not None:
|
2019-06-13 06:13:23 +00:00
|
|
|
for path, proj in manifest.paths.items():
|
2015-09-08 20:27:20 +00:00
|
|
|
if not proj.MatchesGroups(groups):
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not proj.upstream and not git_config.IsId(proj.revisionExpr):
|
|
|
|
proj.upstream = proj.revisionExpr
|
|
|
|
|
2020-02-12 02:24:10 +00:00
|
|
|
if path not in gitc_manifest.paths:
|
2015-09-08 20:27:20 +00:00
|
|
|
# Any new projects need their first revision, even if we weren't asked
|
|
|
|
# for them.
|
|
|
|
projects.append(proj)
|
2020-02-12 02:24:10 +00:00
|
|
|
elif path not in paths:
|
2015-09-08 20:27:20 +00:00
|
|
|
# And copy revisions from the previous manifest if we're not updating
|
|
|
|
# them now.
|
|
|
|
gitc_proj = gitc_manifest.paths[path]
|
|
|
|
if gitc_proj.old_revision:
|
|
|
|
proj.revisionExpr = None
|
|
|
|
proj.old_revision = gitc_proj.old_revision
|
|
|
|
else:
|
|
|
|
proj.revisionExpr = gitc_proj.revisionExpr
|
|
|
|
|
2021-02-16 20:01:39 +00:00
|
|
|
_set_project_revisions(projects)
|
2015-09-08 20:27:20 +00:00
|
|
|
|
|
|
|
if gitc_manifest is not None:
|
2019-06-13 06:13:23 +00:00
|
|
|
for path, proj in gitc_manifest.paths.items():
|
2015-09-08 20:27:20 +00:00
|
|
|
if proj.old_revision and path in paths:
|
|
|
|
# If we updated a project that has been started, keep the old-revision
|
|
|
|
# updated.
|
|
|
|
repo_proj = manifest.paths[path]
|
|
|
|
repo_proj.old_revision = repo_proj.revisionExpr
|
|
|
|
repo_proj.revisionExpr = None
|
|
|
|
|
|
|
|
# Convert URLs from relative to absolute.
|
2019-06-13 06:13:23 +00:00
|
|
|
for _name, remote in manifest.remotes.items():
|
2015-09-08 20:27:20 +00:00
|
|
|
remote.fetchUrl = remote.resolvedFetchUrl
|
|
|
|
|
2015-08-10 20:23:23 +00:00
|
|
|
# Save the manifest.
|
2015-09-08 20:27:20 +00:00
|
|
|
save_manifest(manifest)
|
2015-08-20 19:19:28 +00:00
|
|
|
|
2020-02-12 06:20:19 +00:00
|
|
|
|
2015-08-20 19:19:28 +00:00
|
|
|
def save_manifest(manifest, client_dir=None):
|
|
|
|
"""Save the manifest file in the client_dir.
|
|
|
|
|
2020-12-11 08:26:42 +00:00
|
|
|
Args:
|
|
|
|
manifest: Manifest object to save.
|
|
|
|
client_dir: Client directory to save the manifest in.
|
2015-08-20 19:19:28 +00:00
|
|
|
"""
|
|
|
|
if not client_dir:
|
2020-12-11 09:02:19 +00:00
|
|
|
manifest_file = manifest.manifestFile
|
|
|
|
else:
|
|
|
|
manifest_file = os.path.join(client_dir, '.manifest')
|
|
|
|
with open(manifest_file, 'w') as f:
|
2015-09-08 20:27:20 +00:00
|
|
|
manifest.Save(f, groups=_manifest_groups(manifest))
|
2015-08-20 19:19:28 +00:00
|
|
|
# TODO(sbasi/jorg): Come up with a solution to remove the sleep below.
|
|
|
|
# Give the GITC filesystem time to register the manifest changes.
|
2015-09-08 20:27:20 +00:00
|
|
|
time.sleep(3)
|