init.py 7.0 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 project import SyncBuffer
  21. from git_command import git_require, 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 -b argument can be used to select the manifest branch
  34. to checkout and use. If no branch is specified, master is assumed.
  35. The optional -m argument can be used to specify an alternate manifest
  36. to be used. If no manifest is specified, the manifest default.xml
  37. will be used.
  38. Switching Manifest Branches
  39. ---------------------------
  40. To switch to another manifest branch, `repo init -b otherbranch`
  41. may be used in an existing client. However, as this only updates the
  42. manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
  43. to update the working directory files.
  44. """
  45. def _Options(self, p):
  46. # Logging
  47. g = p.add_option_group('Logging options')
  48. g.add_option('-q', '--quiet',
  49. dest="quiet", action="store_true", default=False,
  50. help="be quiet")
  51. # Manifest
  52. g = p.add_option_group('Manifest options')
  53. g.add_option('-u', '--manifest-url',
  54. dest='manifest_url',
  55. help='manifest repository location', metavar='URL')
  56. g.add_option('-b', '--manifest-branch',
  57. dest='manifest_branch',
  58. help='manifest branch or revision', metavar='REVISION')
  59. g.add_option('-m', '--manifest-name',
  60. dest='manifest_name', default='default.xml',
  61. help='initial manifest file', metavar='NAME.xml')
  62. g.add_option('--mirror',
  63. dest='mirror', action='store_true',
  64. help='mirror the forrest')
  65. # Tool
  66. g = p.add_option_group('repo Version options')
  67. g.add_option('--repo-url',
  68. dest='repo_url',
  69. help='repo repository location', metavar='URL')
  70. g.add_option('--repo-branch',
  71. dest='repo_branch',
  72. help='repo branch or revision', metavar='REVISION')
  73. g.add_option('--no-repo-verify',
  74. dest='no_repo_verify', action='store_true',
  75. help='do not verify repo source code')
  76. def _SyncManifest(self, opt):
  77. m = self.manifest.manifestProject
  78. is_new = not m.Exists
  79. if is_new:
  80. if not opt.manifest_url:
  81. print >>sys.stderr, 'fatal: manifest url (-u) is required.'
  82. sys.exit(1)
  83. if not opt.quiet:
  84. print >>sys.stderr, 'Getting manifest ...'
  85. print >>sys.stderr, ' from %s' % opt.manifest_url
  86. m._InitGitDir()
  87. if opt.manifest_branch:
  88. m.revisionExpr = opt.manifest_branch
  89. else:
  90. m.revisionExpr = 'refs/heads/master'
  91. else:
  92. if opt.manifest_branch:
  93. m.revisionExpr = opt.manifest_branch
  94. else:
  95. m.PreSync()
  96. if opt.manifest_url:
  97. r = m.GetRemote(m.remote.name)
  98. r.url = opt.manifest_url
  99. r.ResetFetch()
  100. r.Save()
  101. if opt.mirror:
  102. if is_new:
  103. m.config.SetString('repo.mirror', 'true')
  104. else:
  105. print >>sys.stderr, 'fatal: --mirror not supported on existing client'
  106. sys.exit(1)
  107. if not m.Sync_NetworkHalf():
  108. r = m.GetRemote(m.remote.name)
  109. print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url
  110. sys.exit(1)
  111. syncbuf = SyncBuffer(m.config)
  112. m.Sync_LocalHalf(syncbuf)
  113. syncbuf.Finish()
  114. if is_new or m.CurrentBranch is None:
  115. if not m.StartBranch('default'):
  116. print >>sys.stderr, 'fatal: cannot create default in manifest'
  117. sys.exit(1)
  118. def _LinkManifest(self, name):
  119. if not name:
  120. print >>sys.stderr, 'fatal: manifest name (-m) is required.'
  121. sys.exit(1)
  122. try:
  123. self.manifest.Link(name)
  124. except ManifestParseError, e:
  125. print >>sys.stderr, "fatal: manifest '%s' not available" % name
  126. print >>sys.stderr, 'fatal: %s' % str(e)
  127. sys.exit(1)
  128. def _Prompt(self, prompt, value):
  129. mp = self.manifest.manifestProject
  130. sys.stdout.write('%-10s [%s]: ' % (prompt, value))
  131. a = sys.stdin.readline().strip()
  132. if a == '':
  133. return value
  134. return a
  135. def _ConfigureUser(self):
  136. mp = self.manifest.manifestProject
  137. while True:
  138. print ''
  139. name = self._Prompt('Your Name', mp.UserName)
  140. email = self._Prompt('Your Email', mp.UserEmail)
  141. print ''
  142. print 'Your identity is: %s <%s>' % (name, email)
  143. sys.stdout.write('is this correct [y/n]? ')
  144. a = sys.stdin.readline().strip()
  145. if a in ('yes', 'y', 't', 'true'):
  146. break
  147. if name != mp.UserName:
  148. mp.config.SetString('user.name', name)
  149. if email != mp.UserEmail:
  150. mp.config.SetString('user.email', email)
  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. git_require(MIN_GIT_VERSION, fail=True)
  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)