diff options
| author | Simran Basi <sbasi@google.com> | 2015-08-05 15:04:22 -0700 |
|---|---|---|
| committer | Simran Basi <sbasi@google.com> | 2015-08-12 16:22:14 -0700 |
| commit | 1efc2b4a0157b5c23317e5e7a51643016133cff5 (patch) | |
| tree | e1d51a6b93948da37f888adf89206c658193f2d5 /subcmds/gitc_init.py | |
| parent | abaa7f312f1b6c8d11d7c757fe909900ce5788b5 (diff) | |
| download | git-repo-1efc2b4a0157b5c23317e5e7a51643016133cff5.tar.gz | |
GITC: Add gitc-init subcommand to repo.
Adds the new gitc-init command to set up a GITC client. Gitc-init
sets up the client directory and calls repo init within it. Once
the repo is initialized, then generates a GITC manifest file
by using git ls-remote on each project and retrieving the HEAD SHA
to use as the revision attribute.
Gitc-init inherits from and has all the options as repo init.
Change-Id: Icd7e47e90eab752a77de7c80ebc98cfe16bf6de3
Diffstat (limited to 'subcmds/gitc_init.py')
| -rw-r--r-- | subcmds/gitc_init.py | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/subcmds/gitc_init.py b/subcmds/gitc_init.py new file mode 100644 index 00000000..9b9cefda --- /dev/null +++ b/subcmds/gitc_init.py | |||
| @@ -0,0 +1,123 @@ | |||
| 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 shutil | ||
| 19 | import sys | ||
| 20 | |||
| 21 | import git_command | ||
| 22 | from subcmds import init | ||
| 23 | |||
| 24 | |||
| 25 | GITC_MANIFEST_DIR = '/usr/local/google/gitc' | ||
| 26 | GITC_FS_ROOT_DIR = '/gitc/sha/rw' | ||
| 27 | NUM_BATCH_RETRIEVE_REVISIONID = 300 | ||
| 28 | |||
| 29 | |||
| 30 | class GitcInit(init.Init): | ||
| 31 | common = True | ||
| 32 | helpSummary = "Initialize a GITC Client." | ||
| 33 | helpUsage = """ | ||
| 34 | %prog [options] [client name] | ||
| 35 | """ | ||
| 36 | helpDescription = """ | ||
| 37 | The '%prog' command is ran to initialize a new GITC client for use | ||
| 38 | with the GITC file system. | ||
| 39 | |||
| 40 | This command will setup the client directory, initialize repo, just | ||
| 41 | like repo init does, and then downloads the manifest collection | ||
| 42 | and installs in in the .repo/directory of the GITC client. | ||
| 43 | |||
| 44 | Once this is done, a GITC manifest is generated by pulling the HEAD | ||
| 45 | SHA for each project and generates the properly formatted XML file | ||
| 46 | and installs it as .manifest in the GITC client directory. | ||
| 47 | |||
| 48 | The -c argument is required to specify the GITC client name. | ||
| 49 | |||
| 50 | The optional -f argument can be used to specify the manifest file to | ||
| 51 | use 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) | ||
