init.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #
  2. # Copyright (C) 2008 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. import os
  16. import sys
  17. from color import Coloring
  18. from command import InteractiveCommand, MirrorSafeCommand
  19. from error import ManifestParseError
  20. from remote import Remote
  21. from git_command import git, MIN_GIT_VERSION
  22. class Init(InteractiveCommand, MirrorSafeCommand):
  23. common = True
  24. helpSummary = "Initialize repo in the current directory"
  25. helpUsage = """
  26. %prog [options]
  27. """
  28. helpDescription = """
  29. The '%prog' command is run once to install and initialize repo.
  30. The latest repo source code and manifest collection is downloaded
  31. from the server and is installed in the .repo/ directory in the
  32. current working directory.
  33. The optional <manifest> argument can be used to specify an alternate
  34. manifest to be used. If no manifest is specified, the manifest
  35. default.xml will be used.
  36. """
  37. def _Options(self, p):
  38. # Logging
  39. g = p.add_option_group('Logging options')
  40. g.add_option('-q', '--quiet',
  41. dest="quiet", action="store_true", default=False,
  42. help="be quiet")
  43. # Manifest
  44. g = p.add_option_group('Manifest options')
  45. g.add_option('-u', '--manifest-url',
  46. dest='manifest_url',
  47. help='manifest repository location', metavar='URL')
  48. g.add_option('-b', '--manifest-branch',
  49. dest='manifest_branch',
  50. help='manifest branch or revision', metavar='REVISION')
  51. g.add_option('-m', '--manifest-name',
  52. dest='manifest_name', default='default.xml',
  53. help='initial manifest file', metavar='NAME.xml')
  54. g.add_option('--mirror',
  55. dest='mirror', action='store_true',
  56. help='mirror the forrest')
  57. # Tool
  58. g = p.add_option_group('Version options')
  59. g.add_option('--repo-url',
  60. dest='repo_url',
  61. help='repo repository location', metavar='URL')
  62. g.add_option('--repo-branch',
  63. dest='repo_branch',
  64. help='repo branch or revision', metavar='REVISION')
  65. g.add_option('--no-repo-verify',
  66. dest='no_repo_verify', action='store_true',
  67. help='do not verify repo source code')
  68. def _CheckGitVersion(self):
  69. ver_str = git.version()
  70. if not ver_str.startswith('git version '):
  71. print >>sys.stderr, 'error: "%s" unsupported' % ver_str
  72. sys.exit(1)
  73. ver_str = ver_str[len('git version '):].strip()
  74. ver_act = tuple(map(lambda x: int(x), ver_str.split('.')[0:3]))
  75. if ver_act < MIN_GIT_VERSION:
  76. need = '.'.join(map(lambda x: str(x), MIN_GIT_VERSION))
  77. print >>sys.stderr, 'fatal: git %s or later required' % need
  78. sys.exit(1)
  79. def _SyncManifest(self, opt):
  80. m = self.manifest.manifestProject
  81. is_new = not m.Exists
  82. if is_new:
  83. if not opt.manifest_url:
  84. print >>sys.stderr, 'fatal: manifest url (-u) is required.'
  85. sys.exit(1)
  86. if not opt.quiet:
  87. print >>sys.stderr, 'Getting manifest ...'
  88. print >>sys.stderr, ' from %s' % opt.manifest_url
  89. m._InitGitDir()
  90. if opt.manifest_branch:
  91. m.revision = opt.manifest_branch
  92. else:
  93. m.revision = 'refs/heads/master'
  94. else:
  95. if opt.manifest_branch:
  96. m.revision = opt.manifest_branch
  97. else:
  98. m.PreSync()
  99. if opt.manifest_url:
  100. r = m.GetRemote(m.remote.name)
  101. r.url = opt.manifest_url
  102. r.ResetFetch()
  103. r.Save()
  104. if opt.mirror:
  105. if is_new:
  106. m.config.SetString('repo.mirror', 'true')
  107. else:
  108. print >>sys.stderr, 'fatal: --mirror not supported on existing client'
  109. sys.exit(1)
  110. m.Sync_NetworkHalf()
  111. m.Sync_LocalHalf()
  112. m.StartBranch('default')
  113. def _LinkManifest(self, name):
  114. if not name:
  115. print >>sys.stderr, 'fatal: manifest name (-m) is required.'
  116. sys.exit(1)
  117. try:
  118. self.manifest.Link(name)
  119. except ManifestParseError, e:
  120. print >>sys.stderr, "fatal: manifest '%s' not available" % name
  121. print >>sys.stderr, 'fatal: %s' % str(e)
  122. sys.exit(1)
  123. def _PromptKey(self, prompt, key, value):
  124. mp = self.manifest.manifestProject
  125. sys.stdout.write('%-10s [%s]: ' % (prompt, value))
  126. a = sys.stdin.readline().strip()
  127. if a != '' and a != value:
  128. mp.config.SetString(key, a)
  129. def _ConfigureUser(self):
  130. mp = self.manifest.manifestProject
  131. print ''
  132. self._PromptKey('Your Name', 'user.name', mp.UserName)
  133. self._PromptKey('Your Email', 'user.email', mp.UserEmail)
  134. def _HasColorSet(self, gc):
  135. for n in ['ui', 'diff', 'status']:
  136. if gc.Has('color.%s' % n):
  137. return True
  138. return False
  139. def _ConfigureColor(self):
  140. gc = self.manifest.globalConfig
  141. if self._HasColorSet(gc):
  142. return
  143. class _Test(Coloring):
  144. def __init__(self):
  145. Coloring.__init__(self, gc, 'test color display')
  146. self._on = True
  147. out = _Test()
  148. print ''
  149. print "Testing colorized output (for 'repo diff', 'repo status'):"
  150. for c in ['black','red','green','yellow','blue','magenta','cyan']:
  151. out.write(' ')
  152. out.printer(fg=c)(' %-6s ', c)
  153. out.write(' ')
  154. out.printer(fg='white', bg='black')(' %s ' % 'white')
  155. out.nl()
  156. for c in ['bold','dim','ul','reverse']:
  157. out.write(' ')
  158. out.printer(fg='black', attr=c)(' %-6s ', c)
  159. out.nl()
  160. sys.stdout.write('Enable color display in this user account (y/n)? ')
  161. a = sys.stdin.readline().strip().lower()
  162. if a in ('y', 'yes', 't', 'true', 'on'):
  163. gc.SetString('color.ui', 'auto')
  164. def Execute(self, opt, args):
  165. self._CheckGitVersion()
  166. self._SyncManifest(opt)
  167. self._LinkManifest(opt.manifest_name)
  168. if os.isatty(0) and os.isatty(1) and not opt.mirror:
  169. self._ConfigureUser()
  170. self._ConfigureColor()
  171. if opt.mirror:
  172. type = 'mirror '
  173. else:
  174. type = ''
  175. print ''
  176. print 'repo %sinitialized in %s' % (type, self.manifest.topdir)