diff options
Diffstat (limited to 'gitc_utils.py')
-rw-r--r-- | gitc_utils.py | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/gitc_utils.py b/gitc_utils.py new file mode 100644 index 00000000..0f3e1818 --- /dev/null +++ b/gitc_utils.py | |||
@@ -0,0 +1,148 @@ | |||
1 | # | ||
2 | # Copyright (C) 2015 The Android Open Source Project | ||
3 | # | ||
4 | # Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | # you may not use this file except in compliance with the License. | ||
6 | # You may obtain a copy of the License at | ||
7 | # | ||
8 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | # | ||
10 | # Unless required by applicable law or agreed to in writing, software | ||
11 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | # See the License for the specific language governing permissions and | ||
14 | # limitations under the License. | ||
15 | |||
16 | from __future__ import print_function | ||
17 | import os | ||
18 | import platform | ||
19 | import re | ||
20 | import sys | ||
21 | import time | ||
22 | |||
23 | import git_command | ||
24 | import git_config | ||
25 | import wrapper | ||
26 | |||
27 | NUM_BATCH_RETRIEVE_REVISIONID = 300 | ||
28 | |||
29 | def get_gitc_manifest_dir(): | ||
30 | return wrapper.Wrapper().get_gitc_manifest_dir() | ||
31 | |||
32 | def parse_clientdir(gitc_fs_path): | ||
33 | return wrapper.Wrapper().gitc_parse_clientdir(gitc_fs_path) | ||
34 | |||
35 | def _set_project_revisions(projects): | ||
36 | """Sets the revisionExpr for a list of projects. | ||
37 | |||
38 | Because of the limit of open file descriptors allowed, length of projects | ||
39 | should not be overly large. Recommend calling this function multiple times | ||
40 | with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects. | ||
41 | |||
42 | @param projects: List of project objects to set the revionExpr for. | ||
43 | """ | ||
44 | # Retrieve the commit id for each project based off of it's current | ||
45 | # revisionExpr and it is not already a commit id. | ||
46 | project_gitcmds = [( | ||
47 | project, git_command.GitCommand(None, | ||
48 | ['ls-remote', | ||
49 | project.remote.url, | ||
50 | project.revisionExpr], | ||
51 | capture_stdout=True, cwd='/tmp')) | ||
52 | for project in projects if not git_config.IsId(project.revisionExpr)] | ||
53 | for proj, gitcmd in project_gitcmds: | ||
54 | if gitcmd.Wait(): | ||
55 | print('FATAL: Failed to retrieve revisionExpr for %s' % proj) | ||
56 | sys.exit(1) | ||
57 | proj.revisionExpr = gitcmd.stdout.split('\t')[0] | ||
58 | |||
59 | def _manifest_groups(manifest): | ||
60 | """Returns the manifest group string that should be synced | ||
61 | |||
62 | This is the same logic used by Command.GetProjects(), which is used during | ||
63 | repo sync | ||
64 | |||
65 | @param manifest: The XmlManifest object | ||
66 | """ | ||
67 | mp = manifest.manifestProject | ||
68 | groups = mp.config.GetString('manifest.groups') | ||
69 | if not groups: | ||
70 | groups = 'default,platform-' + platform.system().lower() | ||
71 | return groups | ||
72 | |||
73 | def generate_gitc_manifest(gitc_manifest, manifest, paths=None): | ||
74 | """Generate a manifest for shafsd to use for this GITC client. | ||
75 | |||
76 | @param gitc_manifest: Current gitc manifest, or None if there isn't one yet. | ||
77 | @param manifest: A GitcManifest object loaded with the current repo manifest. | ||
78 | @param paths: List of project paths we want to update. | ||
79 | """ | ||
80 | |||
81 | print('Generating GITC Manifest by fetching revision SHAs for each ' | ||
82 | 'project.') | ||
83 | if paths is None: | ||
84 | paths = manifest.paths.keys() | ||
85 | |||
86 | groups = [x for x in re.split(r'[,\s]+', _manifest_groups(manifest)) if x] | ||
87 | |||
88 | # Convert the paths to projects, and filter them to the matched groups. | ||
89 | projects = [manifest.paths[p] for p in paths] | ||
90 | projects = [p for p in projects if p.MatchesGroups(groups)] | ||
91 | |||
92 | if gitc_manifest is not None: | ||
93 | for path, proj in manifest.paths.iteritems(): | ||
94 | if not proj.MatchesGroups(groups): | ||
95 | continue | ||
96 | |||
97 | if not proj.upstream and not git_config.IsId(proj.revisionExpr): | ||
98 | proj.upstream = proj.revisionExpr | ||
99 | |||
100 | if not path in gitc_manifest.paths: | ||
101 | # Any new projects need their first revision, even if we weren't asked | ||
102 | # for them. | ||
103 | projects.append(proj) | ||
104 | elif not path in paths: | ||
105 | # And copy revisions from the previous manifest if we're not updating | ||
106 | # them now. | ||
107 | gitc_proj = gitc_manifest.paths[path] | ||
108 | if gitc_proj.old_revision: | ||
109 | proj.revisionExpr = None | ||
110 | proj.old_revision = gitc_proj.old_revision | ||
111 | else: | ||
112 | proj.revisionExpr = gitc_proj.revisionExpr | ||
113 | |||
114 | index = 0 | ||
115 | while index < len(projects): | ||
116 | _set_project_revisions( | ||
117 | projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)]) | ||
118 | index += NUM_BATCH_RETRIEVE_REVISIONID | ||
119 | |||
120 | if gitc_manifest is not None: | ||
121 | for path, proj in gitc_manifest.paths.iteritems(): | ||
122 | if proj.old_revision and path in paths: | ||
123 | # If we updated a project that has been started, keep the old-revision | ||
124 | # updated. | ||
125 | repo_proj = manifest.paths[path] | ||
126 | repo_proj.old_revision = repo_proj.revisionExpr | ||
127 | repo_proj.revisionExpr = None | ||
128 | |||
129 | # Convert URLs from relative to absolute. | ||
130 | for name, remote in manifest.remotes.iteritems(): | ||
131 | remote.fetchUrl = remote.resolvedFetchUrl | ||
132 | |||
133 | # Save the manifest. | ||
134 | save_manifest(manifest) | ||
135 | |||
136 | def save_manifest(manifest, client_dir=None): | ||
137 | """Save the manifest file in the client_dir. | ||
138 | |||
139 | @param client_dir: Client directory to save the manifest in. | ||
140 | @param manifest: Manifest object to save. | ||
141 | """ | ||
142 | if not client_dir: | ||
143 | client_dir = manifest.gitc_client_dir | ||
144 | with open(os.path.join(client_dir, '.manifest'), 'w') as f: | ||
145 | manifest.Save(f, groups=_manifest_groups(manifest)) | ||
146 | # TODO(sbasi/jorg): Come up with a solution to remove the sleep below. | ||
147 | # Give the GITC filesystem time to register the manifest changes. | ||
148 | time.sleep(3) | ||