blob: c243bb3bead2324f8dbe3ee747d8d0d7eb803c51 [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
Simran Basi1efc2b42015-08-05 15:04:22 -070018import sys
19
Simran Basibdb52712015-08-10 13:23:23 -070020import gitc_utils
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070021from command import RequiresGitcCommand
Dan Willemsen5ea32d12015-09-08 13:27:20 -070022from manifest_xml import GitcManifest
Simran Basi1efc2b42015-08-05 15:04:22 -070023from subcmds import init
24
25
Dan Willemsen9ff2ece2015-08-31 15:45:06 -070026class GitcInit(init.Init, RequiresGitcCommand):
Simran Basi1efc2b42015-08-05 15:04:22 -070027 common = True
28 helpSummary = "Initialize a GITC Client."
29 helpUsage = """
30%prog [options] [client name]
31"""
32 helpDescription = """
33The '%prog' command is ran to initialize a new GITC client for use
34with the GITC file system.
35
36This command will setup the client directory, initialize repo, just
37like repo init does, and then downloads the manifest collection
Alexander Bird3c035802015-09-11 13:48:10 -040038and installs it in the .repo/directory of the GITC client.
Simran Basi1efc2b42015-08-05 15:04:22 -070039
40Once this is done, a GITC manifest is generated by pulling the HEAD
41SHA for each project and generates the properly formatted XML file
42and installs it as .manifest in the GITC client directory.
43
44The -c argument is required to specify the GITC client name.
45
46The optional -f argument can be used to specify the manifest file to
47use for this GITC client.
48"""
49
50 def _Options(self, p):
51 super(GitcInit, self)._Options(p)
52 g = p.add_option_group('GITC options')
53 g.add_option('-f', '--manifest-file',
54 dest='manifest_file',
55 help='Optional manifest file to use for this GITC client.')
56 g.add_option('-c', '--gitc-client',
57 dest='gitc_client',
58 help='The name for the new gitc_client instance.')
59
60 def Execute(self, opt, args):
61 if not opt.gitc_client:
62 print('fatal: gitc client (-c) is required', file=sys.stderr)
63 sys.exit(1)
Simran Basi8ce50412015-08-28 14:25:44 -070064 self.client_dir = os.path.join(gitc_utils.get_gitc_manifest_dir(),
Simran Basibdb52712015-08-10 13:23:23 -070065 opt.gitc_client)
Simran Basi8ce50412015-08-28 14:25:44 -070066 if not os.path.exists(gitc_utils.get_gitc_manifest_dir()):
67 os.makedirs(gitc_utils.get_gitc_manifest_dir())
Simran Basi1efc2b42015-08-05 15:04:22 -070068 if not os.path.exists(self.client_dir):
69 os.mkdir(self.client_dir)
70 super(GitcInit, self).Execute(opt, args)
Simran Basif7a51892015-08-27 11:44:42 -070071
Dan Willemsen5ea32d12015-09-08 13:27:20 -070072 manifest_file = self.manifest.manifestFile
Simran Basi1efc2b42015-08-05 15:04:22 -070073 if opt.manifest_file:
74 if not os.path.exists(opt.manifest_file):
75 print('fatal: Specified manifest file %s does not exist.' %
76 opt.manifest_file)
77 sys.exit(1)
Dan Willemsen5ea32d12015-09-08 13:27:20 -070078 manifest_file = opt.manifest_file
79
80 manifest = GitcManifest(self.repodir, opt.gitc_client)
81 manifest.Override(manifest_file)
82 gitc_utils.generate_gitc_manifest(None, manifest)
Simran Basi1efc2b42015-08-05 15:04:22 -070083 print('Please run `cd %s` to view your GITC client.' %
David Pursehousef4599a22015-08-20 16:45:39 +090084 os.path.join(gitc_utils.GITC_FS_ROOT_DIR, opt.gitc_client))