init.py 7.2 KB

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