blob: 9b9cefdade93fa87abfb6c01fcf35e8ac5044412 [file] [log] [blame]
Simran Basi1efc2b42015-08-05 15:04:22 -07001#
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
16from __future__ import print_function
17import os
18import shutil
19import sys
20
21import git_command
22from subcmds import init
23
24
25GITC_MANIFEST_DIR = '/usr/local/google/gitc'
26GITC_FS_ROOT_DIR = '/gitc/sha/rw'
27NUM_BATCH_RETRIEVE_REVISIONID = 300
28
29
30class GitcInit(init.Init):
31 common = True
32 helpSummary = "Initialize a GITC Client."
33 helpUsage = """
34%prog [options] [client name]
35"""
36 helpDescription = """
37The '%prog' command is ran to initialize a new GITC client for use
38with the GITC file system.
39
40This command will setup the client directory, initialize repo, just
41like repo init does, and then downloads the manifest collection
42and installs in in the .repo/directory of the GITC client.
43
44Once this is done, a GITC manifest is generated by pulling the HEAD
45SHA for each project and generates the properly formatted XML file
46and installs it as .manifest in the GITC client directory.
47
48The -c argument is required to specify the GITC client name.
49
50The optional -f argument can be used to specify the manifest file to
51use for this GITC client.
52"""
53
54 def _Options(self, p):
55 super(GitcInit, self)._Options(p)
56 g = p.add_option_group('GITC options')
57 g.add_option('-f', '--manifest-file',
58 dest='manifest_file',
59 help='Optional manifest file to use for this GITC client.')
60 g.add_option('-c', '--gitc-client',
61 dest='gitc_client',
62 help='The name for the new gitc_client instance.')
63
64 def Execute(self, opt, args):
65 if not opt.gitc_client:
66 print('fatal: gitc client (-c) is required', file=sys.stderr)
67 sys.exit(1)
68 self.client_dir = os.path.join(GITC_MANIFEST_DIR, opt.gitc_client)
69 if not os.path.exists(GITC_MANIFEST_DIR):
70 os.makedirs(GITC_MANIFEST_DIR)
71 if not os.path.exists(self.client_dir):
72 os.mkdir(self.client_dir)
73 super(GitcInit, self).Execute(opt, args)
74 if opt.manifest_file:
75 if not os.path.exists(opt.manifest_file):
76 print('fatal: Specified manifest file %s does not exist.' %
77 opt.manifest_file)
78 sys.exit(1)
79 shutil.copyfile(opt.manifest_file,
80 os.path.join(self.client_dir, '.manifest'))
81 else:
82 self._GenerateGITCManifest()
83 print('Please run `cd %s` to view your GITC client.' %
84 os.path.join(GITC_FS_ROOT_DIR, opt.gitc_client))
85
86 def _SetProjectRevisions(self, projects, branch):
87 """Sets the revisionExpr for a list of projects.
88
89 Because of the limit of open file descriptors allowed, length of projects
90 should not be overly large. Recommend calling this function multiple times
91 with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects.
92
93 @param projects: List of project objects to set the revionExpr for.
94 @param branch: The remote branch to retrieve the SHA from. If branch is
95 None, 'HEAD' is used.
96 """
97 project_gitcmds = [(
98 project, git_command.GitCommand(None,
99 ['ls-remote',
100 project.remote.url,
101 branch], capture_stdout=True))
102 for project in projects]
103 for proj, gitcmd in project_gitcmds:
104 if gitcmd.Wait():
105 print('FATAL: Failed to retrieve revisionID for %s' % project)
106 sys.exit(1)
107 proj.revisionExpr = gitcmd.stdout.split('\t')[0]
108
109 def _GenerateGITCManifest(self):
110 """Generate a manifest for shafsd to use for this GITC client."""
111 print('Generating GITC Manifest by fetching revision SHAs for each '
112 'project.')
113 manifest = self.manifest
114 project_gitcmd_dict = {}
115 index = 0
116 while index < len(manifest.projects):
117 self._SetProjectRevisions(
118 manifest.projects[index:(index+NUM_BATCH_RETRIEVE_REVISIONID)],
119 manifest.default.revisionExpr)
120 index += NUM_BATCH_RETRIEVE_REVISIONID
121 # Save the manifest.
122 with open(os.path.join(self.client_dir, '.manifest'), 'w') as f:
123 manifest.Save(f)