gitc_init.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. from __future__ import print_function
  16. import os
  17. import sys
  18. import gitc_utils
  19. from command import GitcAvailableCommand
  20. from manifest_xml import GitcManifest
  21. from subcmds import init
  22. class GitcInit(init.Init, GitcAvailableCommand):
  23. common = True
  24. helpSummary = "Initialize a GITC Client."
  25. helpUsage = """
  26. %prog [options] [client name]
  27. """
  28. helpDescription = """
  29. The '%prog' command is ran to initialize a new GITC client for use
  30. with the GITC file system.
  31. This command will setup the client directory, initialize repo, just
  32. like repo init does, and then downloads the manifest collection
  33. and installs it in the .repo/directory of the GITC client.
  34. Once this is done, a GITC manifest is generated by pulling the HEAD
  35. SHA for each project and generates the properly formatted XML file
  36. and installs it as .manifest in the GITC client directory.
  37. The -c argument is required to specify the GITC client name.
  38. The optional -f argument can be used to specify the manifest file to
  39. use for this GITC client.
  40. """
  41. def _Options(self, p):
  42. super(GitcInit, self)._Options(p)
  43. g = p.add_option_group('GITC options')
  44. g.add_option('-f', '--manifest-file',
  45. dest='manifest_file',
  46. help='Optional manifest file to use for this GITC client.')
  47. g.add_option('-c', '--gitc-client',
  48. dest='gitc_client',
  49. help='The name for the new gitc_client instance.')
  50. def Execute(self, opt, args):
  51. if not opt.gitc_client:
  52. print('fatal: gitc client (-c) is required', file=sys.stderr)
  53. sys.exit(1)
  54. self.client_dir = os.path.join(gitc_utils.get_gitc_manifest_dir(),
  55. opt.gitc_client)
  56. if not os.path.exists(gitc_utils.get_gitc_manifest_dir()):
  57. os.makedirs(gitc_utils.get_gitc_manifest_dir())
  58. if not os.path.exists(self.client_dir):
  59. os.mkdir(self.client_dir)
  60. super(GitcInit, self).Execute(opt, args)
  61. manifest_file = self.manifest.manifestFile
  62. if opt.manifest_file:
  63. if not os.path.exists(opt.manifest_file):
  64. print('fatal: Specified manifest file %s does not exist.' %
  65. opt.manifest_file)
  66. sys.exit(1)
  67. manifest_file = opt.manifest_file
  68. manifest = GitcManifest(self.repodir, opt.gitc_client)
  69. manifest.Override(manifest_file)
  70. gitc_utils.generate_gitc_manifest(None, manifest)
  71. print('Please run `cd %s` to view your GITC client.' %
  72. os.path.join(gitc_utils.GITC_FS_ROOT_DIR, opt.gitc_client))