init.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. from manifest_xml import XmlManifest
  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. Switching Manifest Branches
  37. ---------------------------
  38. To switch to another manifest branch, `repo init -b otherbranch`
  39. may be used in an existing client. However, as this only updates the
  40. manifest, a subsequent `repo sync` (or `repo sync -d`) is necessary
  41. to update the working directory files.
  42. """
  43. def _Options(self, p):
  44. # Logging
  45. g = p.add_option_group('Logging options')
  46. g.add_option('-q', '--quiet',
  47. dest="quiet", action="store_true", default=False,
  48. help="be quiet")
  49. # Manifest
  50. g = p.add_option_group('Manifest options')
  51. g.add_option('-u', '--manifest-url',
  52. dest='manifest_url',
  53. help='manifest repository location', metavar='URL')
  54. g.add_option('-b', '--manifest-branch',
  55. dest='manifest_branch',
  56. help='manifest branch or revision', metavar='REVISION')
  57. g.add_option('-o', '--origin',
  58. dest='manifest_origin',
  59. help="use REMOTE instead of 'origin' to track upstream",
  60. metavar='REMOTE')
  61. if isinstance(self.manifest, XmlManifest) \
  62. or not self.manifest.manifestProject.Exists:
  63. g.add_option('-m', '--manifest-name',
  64. dest='manifest_name', default='default.xml',
  65. help='initial manifest file', metavar='NAME.xml')
  66. g.add_option('--mirror',
  67. dest='mirror', action='store_true',
  68. help='mirror the forrest')
  69. # Tool
  70. g = p.add_option_group('repo Version options')
  71. g.add_option('--repo-url',
  72. dest='repo_url',
  73. help='repo repository location', metavar='URL')
  74. g.add_option('--repo-branch',
  75. dest='repo_branch',
  76. help='repo branch or revision', metavar='REVISION')
  77. g.add_option('--no-repo-verify',
  78. dest='no_repo_verify', action='store_true',
  79. help='do not verify repo source code')
  80. def _ApplyOptions(self, opt, is_new):
  81. m = self.manifest.manifestProject
  82. if is_new:
  83. if opt.manifest_origin:
  84. m.remote.name = opt.manifest_origin
  85. if opt.manifest_branch:
  86. m.revisionExpr = opt.manifest_branch
  87. else:
  88. m.revisionExpr = 'refs/heads/master'
  89. else:
  90. if opt.manifest_origin:
  91. print >>sys.stderr, 'fatal: cannot change origin name'
  92. sys.exit(1)
  93. if opt.manifest_branch:
  94. m.revisionExpr = opt.manifest_branch
  95. else:
  96. m.PreSync()
  97. def _SyncManifest(self, opt):
  98. m = self.manifest.manifestProject
  99. is_new = not m.Exists
  100. if is_new:
  101. if not opt.manifest_url:
  102. print >>sys.stderr, 'fatal: manifest url (-u) is required.'
  103. sys.exit(1)
  104. if not opt.quiet:
  105. print >>sys.stderr, 'Getting manifest ...'
  106. print >>sys.stderr, ' from %s' % opt.manifest_url
  107. m._InitGitDir()
  108. self._ApplyOptions(opt, is_new)
  109. if opt.manifest_url:
  110. r = m.GetRemote(m.remote.name)
  111. r.url = opt.manifest_url
  112. r.ResetFetch()
  113. r.Save()
  114. if opt.mirror:
  115. if is_new:
  116. m.config.SetString('repo.mirror', 'true')
  117. m.config.ClearCache()
  118. else:
  119. print >>sys.stderr, 'fatal: --mirror not supported on existing client'
  120. sys.exit(1)
  121. if not m.Sync_NetworkHalf():
  122. r = m.GetRemote(m.remote.name)
  123. print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url
  124. sys.exit(1)
  125. syncbuf = SyncBuffer(m.config)
  126. m.Sync_LocalHalf(syncbuf)
  127. syncbuf.Finish()
  128. if not self.manifest.InitBranch():
  129. print >>sys.stderr, 'fatal: cannot create branch in manifest'
  130. sys.exit(1)
  131. def _LinkManifest(self, name):
  132. if not name:
  133. print >>sys.stderr, 'fatal: manifest name (-m) is required.'
  134. sys.exit(1)
  135. try:
  136. self.manifest.Link(name)
  137. except ManifestParseError, e:
  138. print >>sys.stderr, "fatal: manifest '%s' not available" % name
  139. print >>sys.stderr, 'fatal: %s' % str(e)
  140. sys.exit(1)
  141. def _Prompt(self, prompt, value):
  142. mp = self.manifest.manifestProject
  143. sys.stdout.write('%-10s [%s]: ' % (prompt, value))
  144. a = sys.stdin.readline().strip()
  145. if a == '':
  146. return value
  147. return a
  148. def _ConfigureUser(self):
  149. mp = self.manifest.manifestProject
  150. while True:
  151. print ''
  152. name = self._Prompt('Your Name', mp.UserName)
  153. email = self._Prompt('Your Email', mp.UserEmail)
  154. print ''
  155. print 'Your identity is: %s <%s>' % (name, email)
  156. sys.stdout.write('is this correct [yes/no]? ')
  157. if 'yes' == sys.stdin.readline().strip():
  158. break
  159. if name != mp.UserName:
  160. mp.config.SetString('user.name', name)
  161. if email != mp.UserEmail:
  162. mp.config.SetString('user.email', email)
  163. def _HasColorSet(self, gc):
  164. for n in ['ui', 'diff', 'status']:
  165. if gc.Has('color.%s' % n):
  166. return True
  167. return False
  168. def _ConfigureColor(self):
  169. gc = self.manifest.globalConfig
  170. if self._HasColorSet(gc):
  171. return
  172. class _Test(Coloring):
  173. def __init__(self):
  174. Coloring.__init__(self, gc, 'test color display')
  175. self._on = True
  176. out = _Test()
  177. print ''
  178. print "Testing colorized output (for 'repo diff', 'repo status'):"
  179. for c in ['black','red','green','yellow','blue','magenta','cyan']:
  180. out.write(' ')
  181. out.printer(fg=c)(' %-6s ', c)
  182. out.write(' ')
  183. out.printer(fg='white', bg='black')(' %s ' % 'white')
  184. out.nl()
  185. for c in ['bold','dim','ul','reverse']:
  186. out.write(' ')
  187. out.printer(fg='black', attr=c)(' %-6s ', c)
  188. out.nl()
  189. sys.stdout.write('Enable color display in this user account (y/n)? ')
  190. a = sys.stdin.readline().strip().lower()
  191. if a in ('y', 'yes', 't', 'true', 'on'):
  192. gc.SetString('color.ui', 'auto')
  193. def Execute(self, opt, args):
  194. git_require(MIN_GIT_VERSION, fail=True)
  195. self._SyncManifest(opt)
  196. if isinstance(self.manifest, XmlManifest):
  197. self._LinkManifest(opt.manifest_name)
  198. if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror:
  199. self._ConfigureUser()
  200. self._ConfigureColor()
  201. if self.manifest.IsMirror:
  202. type = 'mirror '
  203. else:
  204. type = ''
  205. print ''
  206. print 'repo %sinitialized in %s' % (type, self.manifest.topdir)