diff options
Diffstat (limited to 'subcmds/gitc_init.py')
| -rw-r--r-- | subcmds/gitc_init.py | 73 |
1 files changed, 17 insertions, 56 deletions
diff --git a/subcmds/gitc_init.py b/subcmds/gitc_init.py index 9b9cefda..c243bb3b 100644 --- a/subcmds/gitc_init.py +++ b/subcmds/gitc_init.py | |||
| @@ -15,19 +15,15 @@ | |||
| 15 | 15 | ||
| 16 | from __future__ import print_function | 16 | from __future__ import print_function |
| 17 | import os | 17 | import os |
| 18 | import shutil | ||
| 19 | import sys | 18 | import sys |
| 20 | 19 | ||
| 21 | import git_command | 20 | import gitc_utils |
| 21 | from command import RequiresGitcCommand | ||
| 22 | from manifest_xml import GitcManifest | ||
| 22 | from subcmds import init | 23 | from subcmds import init |
| 23 | 24 | ||
| 24 | 25 | ||
| 25 | GITC_MANIFEST_DIR = '/usr/local/google/gitc' | 26 | class GitcInit(init.Init, RequiresGitcCommand): |
| 26 | GITC_FS_ROOT_DIR = '/gitc/sha/rw' | ||
| 27 | NUM_BATCH_RETRIEVE_REVISIONID = 300 | ||
| 28 | |||
| 29 | |||
| 30 | class GitcInit(init.Init): | ||
| 31 | common = True | 27 | common = True |
| 32 | helpSummary = "Initialize a GITC Client." | 28 | helpSummary = "Initialize a GITC Client." |
| 33 | helpUsage = """ | 29 | helpUsage = """ |
| @@ -39,7 +35,7 @@ with the GITC file system. | |||
| 39 | 35 | ||
| 40 | This command will setup the client directory, initialize repo, just | 36 | This command will setup the client directory, initialize repo, just |
| 41 | like repo init does, and then downloads the manifest collection | 37 | like repo init does, and then downloads the manifest collection |
| 42 | and installs in in the .repo/directory of the GITC client. | 38 | and installs it in the .repo/directory of the GITC client. |
| 43 | 39 | ||
| 44 | Once this is done, a GITC manifest is generated by pulling the HEAD | 40 | 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 | 41 | SHA for each project and generates the properly formatted XML file |
| @@ -65,59 +61,24 @@ use for this GITC client. | |||
| 65 | if not opt.gitc_client: | 61 | if not opt.gitc_client: |
| 66 | print('fatal: gitc client (-c) is required', file=sys.stderr) | 62 | print('fatal: gitc client (-c) is required', file=sys.stderr) |
| 67 | sys.exit(1) | 63 | sys.exit(1) |
| 68 | self.client_dir = os.path.join(GITC_MANIFEST_DIR, opt.gitc_client) | 64 | self.client_dir = os.path.join(gitc_utils.get_gitc_manifest_dir(), |
| 69 | if not os.path.exists(GITC_MANIFEST_DIR): | 65 | opt.gitc_client) |
| 70 | os.makedirs(GITC_MANIFEST_DIR) | 66 | if not os.path.exists(gitc_utils.get_gitc_manifest_dir()): |
| 67 | os.makedirs(gitc_utils.get_gitc_manifest_dir()) | ||
| 71 | if not os.path.exists(self.client_dir): | 68 | if not os.path.exists(self.client_dir): |
| 72 | os.mkdir(self.client_dir) | 69 | os.mkdir(self.client_dir) |
| 73 | super(GitcInit, self).Execute(opt, args) | 70 | super(GitcInit, self).Execute(opt, args) |
| 71 | |||
| 72 | manifest_file = self.manifest.manifestFile | ||
| 74 | if opt.manifest_file: | 73 | if opt.manifest_file: |
| 75 | if not os.path.exists(opt.manifest_file): | 74 | if not os.path.exists(opt.manifest_file): |
| 76 | print('fatal: Specified manifest file %s does not exist.' % | 75 | print('fatal: Specified manifest file %s does not exist.' % |
| 77 | opt.manifest_file) | 76 | opt.manifest_file) |
| 78 | sys.exit(1) | 77 | sys.exit(1) |
| 79 | shutil.copyfile(opt.manifest_file, | 78 | manifest_file = 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 | 79 | ||
| 89 | Because of the limit of open file descriptors allowed, length of projects | 80 | manifest = GitcManifest(self.repodir, opt.gitc_client) |
| 90 | should not be overly large. Recommend calling this function multiple times | 81 | manifest.Override(manifest_file) |
| 91 | with each call not exceeding NUM_BATCH_RETRIEVE_REVISIONID projects. | 82 | gitc_utils.generate_gitc_manifest(None, manifest) |
| 92 | 83 | print('Please run `cd %s` to view your GITC client.' % | |
| 93 | @param projects: List of project objects to set the revionExpr for. | 84 | os.path.join(gitc_utils.GITC_FS_ROOT_DIR, opt.gitc_client)) |
| 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) | ||
