blob: 89cfbeca141a56f6c4b63b9ecf0f06c9cfca0a6e [file] [log] [blame]
Simran Basibdb52712015-08-10 13:23:23 -07001# Copyright (C) 2015 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Simran Basibdb52712015-08-10 13:23:23 -070015import os
Dan Willemsen5ea32d12015-09-08 13:27:20 -070016import platform
17import re
David Pursehouse46496d82015-08-20 16:37:09 +090018import sys
Simran Basib9a1b732015-08-20 12:19:28 -070019import time
Simran Basibdb52712015-08-10 13:23:23 -070020
21import git_command
22import git_config
Simran Basi8ce50412015-08-28 14:25:44 -070023import wrapper
Simran Basibdb52712015-08-10 13:23:23 -070024
Xin Lif97e72e2016-06-24 12:17:14 -070025from error import ManifestParseError
26
Dan Willemsen39252ba2016-08-23 14:06:59 -070027NUM_BATCH_RETRIEVE_REVISIONID = 32
Simran Basibdb52712015-08-10 13:23:23 -070028
David Pursehouse819827a2020-02-12 15:20:19 +090029
Simran Basi8ce50412015-08-28 14:25:44 -070030def get_gitc_manifest_dir():
31 return wrapper.Wrapper().get_gitc_manifest_dir()
32
David Pursehouse819827a2020-02-12 15:20:19 +090033
Simran Basib9a1b732015-08-20 12:19:28 -070034def parse_clientdir(gitc_fs_path):
Dan Willemsen745b4ad2015-10-06 15:23:19 -070035 return wrapper.Wrapper().gitc_parse_clientdir(gitc_fs_path)
Simran Basib9a1b732015-08-20 12:19:28 -070036
David Pursehouse819827a2020-02-12 15:20:19 +090037
Simran Basibdb52712015-08-10 13:23:23 -070038def _set_project_revisions(projects):
39 """Sets the revisionExpr for a list of projects.
40
41 Because of the limit of open file descriptors allowed, length of projects
42 should not be overly large. Recommend calling this function multiple times
43 with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects.
44
Mike Frysinger0a849b62020-12-11 03:26:42 -050045 Args:
46 projects: List of project objects to set the revionExpr for.
Simran Basibdb52712015-08-10 13:23:23 -070047 """
48 # Retrieve the commit id for each project based off of it's current
49 # revisionExpr and it is not already a commit id.
50 project_gitcmds = [(
51 project, git_command.GitCommand(None,
52 ['ls-remote',
53 project.remote.url,
54 project.revisionExpr],
55 capture_stdout=True, cwd='/tmp'))
David Pursehouseabdf7502020-02-12 14:58:39 +090056 for project in projects if not git_config.IsId(project.revisionExpr)]
Simran Basibdb52712015-08-10 13:23:23 -070057 for proj, gitcmd in project_gitcmds:
58 if gitcmd.Wait():
David Pursehouse022a1d42015-08-20 16:41:04 +090059 print('FATAL: Failed to retrieve revisionExpr for %s' % proj)
Simran Basibdb52712015-08-10 13:23:23 -070060 sys.exit(1)
Xin Lif97e72e2016-06-24 12:17:14 -070061 revisionExpr = gitcmd.stdout.split('\t')[0]
62 if not revisionExpr:
Mike Frysinger31067c02019-06-13 02:13:23 -040063 raise ManifestParseError('Invalid SHA-1 revision project %s (%s)' %
64 (proj.remote.url, proj.revisionExpr))
Xin Lif97e72e2016-06-24 12:17:14 -070065 proj.revisionExpr = revisionExpr
Simran Basibdb52712015-08-10 13:23:23 -070066
David Pursehouse819827a2020-02-12 15:20:19 +090067
Dan Willemsen5ea32d12015-09-08 13:27:20 -070068def _manifest_groups(manifest):
69 """Returns the manifest group string that should be synced
70
71 This is the same logic used by Command.GetProjects(), which is used during
72 repo sync
73
Mike Frysinger0a849b62020-12-11 03:26:42 -050074 Args:
75 manifest: The XmlManifest object
Dan Willemsen5ea32d12015-09-08 13:27:20 -070076 """
77 mp = manifest.manifestProject
78 groups = mp.config.GetString('manifest.groups')
79 if not groups:
80 groups = 'default,platform-' + platform.system().lower()
81 return groups
82
David Pursehouse819827a2020-02-12 15:20:19 +090083
Dan Willemsen5ea32d12015-09-08 13:27:20 -070084def generate_gitc_manifest(gitc_manifest, manifest, paths=None):
Simran Basibdb52712015-08-10 13:23:23 -070085 """Generate a manifest for shafsd to use for this GITC client.
86
Mike Frysinger0a849b62020-12-11 03:26:42 -050087 Args:
88 gitc_manifest: Current gitc manifest, or None if there isn't one yet.
89 manifest: A GitcManifest object loaded with the current repo manifest.
90 paths: List of project paths we want to update.
Simran Basibdb52712015-08-10 13:23:23 -070091 """
Dan Willemsen5ea32d12015-09-08 13:27:20 -070092
Simran Basibdb52712015-08-10 13:23:23 -070093 print('Generating GITC Manifest by fetching revision SHAs for each '
94 'project.')
Dan Willemsen5ea32d12015-09-08 13:27:20 -070095 if paths is None:
Mike Frysinger31067c02019-06-13 02:13:23 -040096 paths = list(manifest.paths.keys())
Dan Willemsen5ea32d12015-09-08 13:27:20 -070097
98 groups = [x for x in re.split(r'[,\s]+', _manifest_groups(manifest)) if x]
99
100 # Convert the paths to projects, and filter them to the matched groups.
101 projects = [manifest.paths[p] for p in paths]
102 projects = [p for p in projects if p.MatchesGroups(groups)]
103
104 if gitc_manifest is not None:
Mike Frysinger31067c02019-06-13 02:13:23 -0400105 for path, proj in manifest.paths.items():
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700106 if not proj.MatchesGroups(groups):
107 continue
108
109 if not proj.upstream and not git_config.IsId(proj.revisionExpr):
110 proj.upstream = proj.revisionExpr
111
David Pursehouseeeff3532020-02-12 11:24:10 +0900112 if path not in gitc_manifest.paths:
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700113 # Any new projects need their first revision, even if we weren't asked
114 # for them.
115 projects.append(proj)
David Pursehouseeeff3532020-02-12 11:24:10 +0900116 elif path not in paths:
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700117 # And copy revisions from the previous manifest if we're not updating
118 # them now.
119 gitc_proj = gitc_manifest.paths[path]
120 if gitc_proj.old_revision:
121 proj.revisionExpr = None
122 proj.old_revision = gitc_proj.old_revision
123 else:
124 proj.revisionExpr = gitc_proj.revisionExpr
125
Simran Basibdb52712015-08-10 13:23:23 -0700126 index = 0
Simran Basib9a1b732015-08-20 12:19:28 -0700127 while index < len(projects):
Simran Basibdb52712015-08-10 13:23:23 -0700128 _set_project_revisions(
David Pursehouse54a4e602020-02-12 14:31:05 +0900129 projects[index:(index + NUM_BATCH_RETRIEVE_REVISIONID)])
Simran Basibdb52712015-08-10 13:23:23 -0700130 index += NUM_BATCH_RETRIEVE_REVISIONID
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700131
132 if gitc_manifest is not None:
Mike Frysinger31067c02019-06-13 02:13:23 -0400133 for path, proj in gitc_manifest.paths.items():
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700134 if proj.old_revision and path in paths:
135 # If we updated a project that has been started, keep the old-revision
136 # updated.
137 repo_proj = manifest.paths[path]
138 repo_proj.old_revision = repo_proj.revisionExpr
139 repo_proj.revisionExpr = None
140
141 # Convert URLs from relative to absolute.
Mike Frysinger31067c02019-06-13 02:13:23 -0400142 for _name, remote in manifest.remotes.items():
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700143 remote.fetchUrl = remote.resolvedFetchUrl
144
Simran Basibdb52712015-08-10 13:23:23 -0700145 # Save the manifest.
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700146 save_manifest(manifest)
Simran Basib9a1b732015-08-20 12:19:28 -0700147
David Pursehouse819827a2020-02-12 15:20:19 +0900148
Simran Basib9a1b732015-08-20 12:19:28 -0700149def save_manifest(manifest, client_dir=None):
150 """Save the manifest file in the client_dir.
151
Mike Frysinger0a849b62020-12-11 03:26:42 -0500152 Args:
153 manifest: Manifest object to save.
154 client_dir: Client directory to save the manifest in.
Simran Basib9a1b732015-08-20 12:19:28 -0700155 """
156 if not client_dir:
Mike Frysingerdc60e542020-12-11 04:02:19 -0500157 manifest_file = manifest.manifestFile
158 else:
159 manifest_file = os.path.join(client_dir, '.manifest')
160 with open(manifest_file, 'w') as f:
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700161 manifest.Save(f, groups=_manifest_groups(manifest))
Simran Basib9a1b732015-08-20 12:19:28 -0700162 # TODO(sbasi/jorg): Come up with a solution to remove the sleep below.
163 # Give the GITC filesystem time to register the manifest changes.
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700164 time.sleep(3)