The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
| 16 | import os |
| 17 | import sys |
| 18 | |
| 19 | from color import Coloring |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 20 | from command import InteractiveCommand, MirrorSafeCommand |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | from error import ManifestParseError |
| 22 | from remote import Remote |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 23 | from project import SyncBuffer |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | from git_command import git, MIN_GIT_VERSION |
| 25 | |
Shawn O. Pearce | c95583b | 2009-03-03 17:47:06 -0800 | [diff] [blame] | 26 | class Init(InteractiveCommand, MirrorSafeCommand): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | common = True |
| 28 | helpSummary = "Initialize repo in the current directory" |
| 29 | helpUsage = """ |
| 30 | %prog [options] |
| 31 | """ |
| 32 | helpDescription = """ |
| 33 | The '%prog' command is run once to install and initialize repo. |
| 34 | The latest repo source code and manifest collection is downloaded |
| 35 | from the server and is installed in the .repo/ directory in the |
| 36 | current working directory. |
| 37 | |
| 38 | The optional <manifest> argument can be used to specify an alternate |
| 39 | manifest to be used. If no manifest is specified, the manifest |
| 40 | default.xml will be used. |
| 41 | """ |
| 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 | |
| 50 | # Manifest |
| 51 | g = p.add_option_group('Manifest options') |
| 52 | g.add_option('-u', '--manifest-url', |
| 53 | dest='manifest_url', |
| 54 | help='manifest repository location', metavar='URL') |
| 55 | g.add_option('-b', '--manifest-branch', |
| 56 | dest='manifest_branch', |
| 57 | help='manifest branch or revision', metavar='REVISION') |
| 58 | g.add_option('-m', '--manifest-name', |
| 59 | dest='manifest_name', default='default.xml', |
| 60 | help='initial manifest file', metavar='NAME.xml') |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 61 | g.add_option('--mirror', |
| 62 | dest='mirror', action='store_true', |
| 63 | help='mirror the forrest') |
| 64 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 65 | |
| 66 | # Tool |
| 67 | g = p.add_option_group('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 | |
| 78 | def _CheckGitVersion(self): |
| 79 | ver_str = git.version() |
| 80 | if not ver_str.startswith('git version '): |
| 81 | print >>sys.stderr, 'error: "%s" unsupported' % ver_str |
| 82 | sys.exit(1) |
| 83 | |
| 84 | ver_str = ver_str[len('git version '):].strip() |
| 85 | ver_act = tuple(map(lambda x: int(x), ver_str.split('.')[0:3])) |
| 86 | if ver_act < MIN_GIT_VERSION: |
| 87 | need = '.'.join(map(lambda x: str(x), MIN_GIT_VERSION)) |
| 88 | print >>sys.stderr, 'fatal: git %s or later required' % need |
| 89 | sys.exit(1) |
| 90 | |
| 91 | def _SyncManifest(self, opt): |
| 92 | m = self.manifest.manifestProject |
Shawn O. Pearce | 5470df6 | 2009-03-09 18:51:58 -0700 | [diff] [blame] | 93 | is_new = not m.Exists |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 94 | |
Shawn O. Pearce | 5470df6 | 2009-03-09 18:51:58 -0700 | [diff] [blame] | 95 | if is_new: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 96 | if not opt.manifest_url: |
| 97 | print >>sys.stderr, 'fatal: manifest url (-u) is required.' |
| 98 | sys.exit(1) |
| 99 | |
| 100 | if not opt.quiet: |
| 101 | print >>sys.stderr, 'Getting manifest ...' |
| 102 | print >>sys.stderr, ' from %s' % opt.manifest_url |
| 103 | m._InitGitDir() |
| 104 | |
| 105 | if opt.manifest_branch: |
| 106 | m.revision = opt.manifest_branch |
| 107 | else: |
| 108 | m.revision = 'refs/heads/master' |
| 109 | else: |
| 110 | if opt.manifest_branch: |
| 111 | m.revision = opt.manifest_branch |
| 112 | else: |
| 113 | m.PreSync() |
| 114 | |
| 115 | if opt.manifest_url: |
| 116 | r = m.GetRemote(m.remote.name) |
| 117 | r.url = opt.manifest_url |
| 118 | r.ResetFetch() |
| 119 | r.Save() |
| 120 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 121 | if opt.mirror: |
Shawn O. Pearce | 5470df6 | 2009-03-09 18:51:58 -0700 | [diff] [blame] | 122 | if is_new: |
| 123 | m.config.SetString('repo.mirror', 'true') |
| 124 | else: |
| 125 | print >>sys.stderr, 'fatal: --mirror not supported on existing client' |
| 126 | sys.exit(1) |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 127 | |
Shawn O. Pearce | 1fc99f4 | 2009-03-17 08:06:18 -0700 | [diff] [blame] | 128 | if not m.Sync_NetworkHalf(): |
| 129 | r = m.GetRemote(m.remote.name) |
| 130 | print >>sys.stderr, 'fatal: cannot obtain manifest %s' % r.url |
| 131 | sys.exit(1) |
| 132 | |
Shawn O. Pearce | 350cde4 | 2009-04-16 11:21:18 -0700 | [diff] [blame] | 133 | syncbuf = SyncBuffer(m.config) |
| 134 | m.Sync_LocalHalf(syncbuf) |
| 135 | syncbuf.Finish() |
| 136 | |
Shawn O. Pearce | df01883 | 2009-03-17 08:15:27 -0700 | [diff] [blame] | 137 | if is_new or m.CurrentBranch is None: |
Shawn O. Pearce | 0a389e9 | 2009-04-10 16:21:18 -0700 | [diff] [blame] | 138 | if not m.StartBranch('default'): |
| 139 | print >>sys.stderr, 'fatal: cannot create default in manifest' |
| 140 | sys.exit(1) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 141 | |
| 142 | def _LinkManifest(self, name): |
| 143 | if not name: |
| 144 | print >>sys.stderr, 'fatal: manifest name (-m) is required.' |
| 145 | sys.exit(1) |
| 146 | |
| 147 | try: |
| 148 | self.manifest.Link(name) |
| 149 | except ManifestParseError, e: |
| 150 | print >>sys.stderr, "fatal: manifest '%s' not available" % name |
| 151 | print >>sys.stderr, 'fatal: %s' % str(e) |
| 152 | sys.exit(1) |
| 153 | |
| 154 | def _PromptKey(self, prompt, key, value): |
| 155 | mp = self.manifest.manifestProject |
| 156 | |
| 157 | sys.stdout.write('%-10s [%s]: ' % (prompt, value)) |
| 158 | a = sys.stdin.readline().strip() |
| 159 | if a != '' and a != value: |
| 160 | mp.config.SetString(key, a) |
| 161 | |
| 162 | def _ConfigureUser(self): |
| 163 | mp = self.manifest.manifestProject |
| 164 | |
| 165 | print '' |
| 166 | self._PromptKey('Your Name', 'user.name', mp.UserName) |
| 167 | self._PromptKey('Your Email', 'user.email', mp.UserEmail) |
| 168 | |
| 169 | def _HasColorSet(self, gc): |
| 170 | for n in ['ui', 'diff', 'status']: |
| 171 | if gc.Has('color.%s' % n): |
| 172 | return True |
| 173 | return False |
| 174 | |
| 175 | def _ConfigureColor(self): |
| 176 | gc = self.manifest.globalConfig |
| 177 | if self._HasColorSet(gc): |
| 178 | return |
| 179 | |
| 180 | class _Test(Coloring): |
| 181 | def __init__(self): |
| 182 | Coloring.__init__(self, gc, 'test color display') |
| 183 | self._on = True |
| 184 | out = _Test() |
| 185 | |
| 186 | print '' |
| 187 | print "Testing colorized output (for 'repo diff', 'repo status'):" |
| 188 | |
| 189 | for c in ['black','red','green','yellow','blue','magenta','cyan']: |
| 190 | out.write(' ') |
| 191 | out.printer(fg=c)(' %-6s ', c) |
| 192 | out.write(' ') |
| 193 | out.printer(fg='white', bg='black')(' %s ' % 'white') |
| 194 | out.nl() |
| 195 | |
| 196 | for c in ['bold','dim','ul','reverse']: |
| 197 | out.write(' ') |
| 198 | out.printer(fg='black', attr=c)(' %-6s ', c) |
| 199 | out.nl() |
| 200 | |
| 201 | sys.stdout.write('Enable color display in this user account (y/n)? ') |
| 202 | a = sys.stdin.readline().strip().lower() |
| 203 | if a in ('y', 'yes', 't', 'true', 'on'): |
| 204 | gc.SetString('color.ui', 'auto') |
| 205 | |
| 206 | def Execute(self, opt, args): |
| 207 | self._CheckGitVersion() |
| 208 | self._SyncManifest(opt) |
| 209 | self._LinkManifest(opt.manifest_name) |
| 210 | |
Shawn O. Pearce | 8630f39 | 2009-03-19 10:17:12 -0700 | [diff] [blame] | 211 | if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 212 | self._ConfigureUser() |
| 213 | self._ConfigureColor() |
| 214 | |
Shawn O. Pearce | 8630f39 | 2009-03-19 10:17:12 -0700 | [diff] [blame] | 215 | if self.manifest.IsMirror: |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 216 | type = 'mirror ' |
| 217 | else: |
| 218 | type = '' |
| 219 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 220 | print '' |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 221 | print 'repo %sinitialized in %s' % (type, self.manifest.topdir) |