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