blob: b47e181cedcb3d81d3917bcc982a35d73a2c6862 [file] [log] [blame]
Mike Frysingerf6013762019-06-13 02:30:51 -04001# -*- coding:utf-8 -*-
Simran Basibdb52712015-08-10 13:23:23 -07002#
3# Copyright (C) 2015 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17from __future__ import print_function
18import os
Dan Willemsen5ea32d12015-09-08 13:27:20 -070019import platform
20import re
David Pursehouse46496d82015-08-20 16:37:09 +090021import sys
Simran Basib9a1b732015-08-20 12:19:28 -070022import time
Simran Basibdb52712015-08-10 13:23:23 -070023
24import git_command
25import git_config
Simran Basi8ce50412015-08-28 14:25:44 -070026import wrapper
Simran Basibdb52712015-08-10 13:23:23 -070027
Xin Lif97e72e2016-06-24 12:17:14 -070028from error import ManifestParseError
29
Dan Willemsen39252ba2016-08-23 14:06:59 -070030NUM_BATCH_RETRIEVE_REVISIONID = 32
Simran Basibdb52712015-08-10 13:23:23 -070031
Simran Basi8ce50412015-08-28 14:25:44 -070032def get_gitc_manifest_dir():
33 return wrapper.Wrapper().get_gitc_manifest_dir()
34
Simran Basib9a1b732015-08-20 12:19:28 -070035def parse_clientdir(gitc_fs_path):
Dan Willemsen745b4ad2015-10-06 15:23:19 -070036 return wrapper.Wrapper().gitc_parse_clientdir(gitc_fs_path)
Simran Basib9a1b732015-08-20 12:19:28 -070037
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
45 @param projects: List of project objects to set the revionExpr for.
46 """
47 # Retrieve the commit id for each project based off of it's current
48 # revisionExpr and it is not already a commit id.
49 project_gitcmds = [(
50 project, git_command.GitCommand(None,
51 ['ls-remote',
52 project.remote.url,
53 project.revisionExpr],
54 capture_stdout=True, cwd='/tmp'))
55 for project in projects if not git_config.IsId(project.revisionExpr)]
56 for proj, gitcmd in project_gitcmds:
57 if gitcmd.Wait():
David Pursehouse022a1d42015-08-20 16:41:04 +090058 print('FATAL: Failed to retrieve revisionExpr for %s' % proj)
Simran Basibdb52712015-08-10 13:23:23 -070059 sys.exit(1)
Xin Lif97e72e2016-06-24 12:17:14 -070060 revisionExpr = gitcmd.stdout.split('\t')[0]
61 if not revisionExpr:
Mike Frysinger31067c02019-06-13 02:13:23 -040062 raise ManifestParseError('Invalid SHA-1 revision project %s (%s)' %
63 (proj.remote.url, proj.revisionExpr))
Xin Lif97e72e2016-06-24 12:17:14 -070064 proj.revisionExpr = revisionExpr
Simran Basibdb52712015-08-10 13:23:23 -070065
Dan Willemsen5ea32d12015-09-08 13:27:20 -070066def _manifest_groups(manifest):
67 """Returns the manifest group string that should be synced
68
69 This is the same logic used by Command.GetProjects(), which is used during
70 repo sync
71
72 @param manifest: The XmlManifest object
73 """
74 mp = manifest.manifestProject
75 groups = mp.config.GetString('manifest.groups')
76 if not groups:
77 groups = 'default,platform-' + platform.system().lower()
78 return groups
79
80def generate_gitc_manifest(gitc_manifest, manifest, paths=None):
Simran Basibdb52712015-08-10 13:23:23 -070081 """Generate a manifest for shafsd to use for this GITC client.
82
Dan Willemsen5ea32d12015-09-08 13:27:20 -070083 @param gitc_manifest: Current gitc manifest, or None if there isn't one yet.
84 @param manifest: A GitcManifest object loaded with the current repo manifest.
85 @param paths: List of project paths we want to update.
Simran Basibdb52712015-08-10 13:23:23 -070086 """
Dan Willemsen5ea32d12015-09-08 13:27:20 -070087
Simran Basibdb52712015-08-10 13:23:23 -070088 print('Generating GITC Manifest by fetching revision SHAs for each '
89 'project.')
Dan Willemsen5ea32d12015-09-08 13:27:20 -070090 if paths is None:
Mike Frysinger31067c02019-06-13 02:13:23 -040091 paths = list(manifest.paths.keys())
Dan Willemsen5ea32d12015-09-08 13:27:20 -070092
93 groups = [x for x in re.split(r'[,\s]+', _manifest_groups(manifest)) if x]
94
95 # Convert the paths to projects, and filter them to the matched groups.
96 projects = [manifest.paths[p] for p in paths]
97 projects = [p for p in projects if p.MatchesGroups(groups)]
98
99 if gitc_manifest is not None:
Mike Frysinger31067c02019-06-13 02:13:23 -0400100 for path, proj in manifest.paths.items():
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700101 if not proj.MatchesGroups(groups):
102 continue
103
104 if not proj.upstream and not git_config.IsId(proj.revisionExpr):
105 proj.upstream = proj.revisionExpr
106
107 if not path in gitc_manifest.paths:
108 # Any new projects need their first revision, even if we weren't asked
109 # for them.
110 projects.append(proj)
111 elif not path in paths:
112 # And copy revisions from the previous manifest if we're not updating
113 # them now.
114 gitc_proj = gitc_manifest.paths[path]
115 if gitc_proj.old_revision:
116 proj.revisionExpr = None
117 proj.old_revision = gitc_proj.old_revision
118 else:
119 proj.revisionExpr = gitc_proj.revisionExpr
120
Simran Basibdb52712015-08-10 13:23:23 -0700121 index = 0
Simran Basib9a1b732015-08-20 12:19:28 -0700122 while index < len(projects):
Simran Basibdb52712015-08-10 13:23:23 -0700123 _set_project_revisions(
Simran Basib9a1b732015-08-20 12:19:28 -0700124 projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)])
Simran Basibdb52712015-08-10 13:23:23 -0700125 index += NUM_BATCH_RETRIEVE_REVISIONID
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700126
127 if gitc_manifest is not None:
Mike Frysinger31067c02019-06-13 02:13:23 -0400128 for path, proj in gitc_manifest.paths.items():
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700129 if proj.old_revision and path in paths:
130 # If we updated a project that has been started, keep the old-revision
131 # updated.
132 repo_proj = manifest.paths[path]
133 repo_proj.old_revision = repo_proj.revisionExpr
134 repo_proj.revisionExpr = None
135
136 # Convert URLs from relative to absolute.
Mike Frysinger31067c02019-06-13 02:13:23 -0400137 for _name, remote in manifest.remotes.items():
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700138 remote.fetchUrl = remote.resolvedFetchUrl
139
Simran Basibdb52712015-08-10 13:23:23 -0700140 # Save the manifest.
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700141 save_manifest(manifest)
Simran Basib9a1b732015-08-20 12:19:28 -0700142
143def save_manifest(manifest, client_dir=None):
144 """Save the manifest file in the client_dir.
145
146 @param client_dir: Client directory to save the manifest in.
147 @param manifest: Manifest object to save.
148 """
149 if not client_dir:
150 client_dir = manifest.gitc_client_dir
Simran Basibdb52712015-08-10 13:23:23 -0700151 with open(os.path.join(client_dir, '.manifest'), 'w') as f:
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700152 manifest.Save(f, groups=_manifest_groups(manifest))
Simran Basib9a1b732015-08-20 12:19:28 -0700153 # TODO(sbasi/jorg): Come up with a solution to remove the sleep below.
154 # Give the GITC filesystem time to register the manifest changes.
Dan Willemsen5ea32d12015-09-08 13:27:20 -0700155 time.sleep(3)