diff options
Diffstat (limited to 'subcmds/init.py')
-rw-r--r-- | subcmds/init.py | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/subcmds/init.py b/subcmds/init.py new file mode 100644 index 00000000..03f358d1 --- /dev/null +++ b/subcmds/init.py | |||
@@ -0,0 +1,193 @@ | |||
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 | ||
20 | from command import InteractiveCommand | ||
21 | from error import ManifestParseError | ||
22 | from remote import Remote | ||
23 | from git_command import git, MIN_GIT_VERSION | ||
24 | |||
25 | class Init(InteractiveCommand): | ||
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') | ||
60 | |||
61 | # Tool | ||
62 | g = p.add_option_group('Version options') | ||
63 | g.add_option('--repo-url', | ||
64 | dest='repo_url', | ||
65 | help='repo repository location', metavar='URL') | ||
66 | g.add_option('--repo-branch', | ||
67 | dest='repo_branch', | ||
68 | help='repo branch or revision', metavar='REVISION') | ||
69 | g.add_option('--no-repo-verify', | ||
70 | dest='no_repo_verify', action='store_true', | ||
71 | help='do not verify repo source code') | ||
72 | |||
73 | def _CheckGitVersion(self): | ||
74 | ver_str = git.version() | ||
75 | if not ver_str.startswith('git version '): | ||
76 | print >>sys.stderr, 'error: "%s" unsupported' % ver_str | ||
77 | sys.exit(1) | ||
78 | |||
79 | ver_str = ver_str[len('git version '):].strip() | ||
80 | ver_act = tuple(map(lambda x: int(x), ver_str.split('.')[0:3])) | ||
81 | if ver_act < MIN_GIT_VERSION: | ||
82 | need = '.'.join(map(lambda x: str(x), MIN_GIT_VERSION)) | ||
83 | print >>sys.stderr, 'fatal: git %s or later required' % need | ||
84 | sys.exit(1) | ||
85 | |||
86 | def _SyncManifest(self, opt): | ||
87 | m = self.manifest.manifestProject | ||
88 | |||
89 | if not m.Exists: | ||
90 | if not opt.manifest_url: | ||
91 | print >>sys.stderr, 'fatal: manifest url (-u) is required.' | ||
92 | sys.exit(1) | ||
93 | |||
94 | if not opt.quiet: | ||
95 | print >>sys.stderr, 'Getting manifest ...' | ||
96 | print >>sys.stderr, ' from %s' % opt.manifest_url | ||
97 | m._InitGitDir() | ||
98 | |||
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 | |||
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 | |||
115 | m.Sync_NetworkHalf() | ||
116 | m.Sync_LocalHalf() | ||
117 | m.StartBranch('default') | ||
118 | |||
119 | def _LinkManifest(self, name): | ||
120 | if not name: | ||
121 | print >>sys.stderr, 'fatal: manifest name (-m) is required.' | ||
122 | sys.exit(1) | ||
123 | |||
124 | try: | ||
125 | self.manifest.Link(name) | ||
126 | except ManifestParseError, e: | ||
127 | print >>sys.stderr, "fatal: manifest '%s' not available" % name | ||
128 | print >>sys.stderr, 'fatal: %s' % str(e) | ||
129 | sys.exit(1) | ||
130 | |||
131 | def _PromptKey(self, prompt, key, value): | ||
132 | mp = self.manifest.manifestProject | ||
133 | |||
134 | sys.stdout.write('%-10s [%s]: ' % (prompt, value)) | ||
135 | a = sys.stdin.readline().strip() | ||
136 | if a != '' and a != value: | ||
137 | mp.config.SetString(key, a) | ||
138 | |||
139 | def _ConfigureUser(self): | ||
140 | mp = self.manifest.manifestProject | ||
141 | |||
142 | print '' | ||
143 | self._PromptKey('Your Name', 'user.name', mp.UserName) | ||
144 | self._PromptKey('Your Email', 'user.email', mp.UserEmail) | ||
145 | |||
146 | def _HasColorSet(self, gc): | ||
147 | for n in ['ui', 'diff', 'status']: | ||
148 | if gc.Has('color.%s' % n): | ||
149 | return True | ||
150 | return False | ||
151 | |||
152 | def _ConfigureColor(self): | ||
153 | gc = self.manifest.globalConfig | ||
154 | if self._HasColorSet(gc): | ||
155 | return | ||
156 | |||
157 | class _Test(Coloring): | ||
158 | def __init__(self): | ||
159 | Coloring.__init__(self, gc, 'test color display') | ||
160 | self._on = True | ||
161 | out = _Test() | ||
162 | |||
163 | print '' | ||
164 | print "Testing colorized output (for 'repo diff', 'repo status'):" | ||
165 | |||
166 | for c in ['black','red','green','yellow','blue','magenta','cyan']: | ||
167 | out.write(' ') | ||
168 | out.printer(fg=c)(' %-6s ', c) | ||
169 | out.write(' ') | ||
170 | out.printer(fg='white', bg='black')(' %s ' % 'white') | ||
171 | out.nl() | ||
172 | |||
173 | for c in ['bold','dim','ul','reverse']: | ||
174 | out.write(' ') | ||
175 | out.printer(fg='black', attr=c)(' %-6s ', c) | ||
176 | out.nl() | ||
177 | |||
178 | sys.stdout.write('Enable color display in this user account (y/n)? ') | ||
179 | a = sys.stdin.readline().strip().lower() | ||
180 | if a in ('y', 'yes', 't', 'true', 'on'): | ||
181 | gc.SetString('color.ui', 'auto') | ||
182 | |||
183 | def Execute(self, opt, args): | ||
184 | self._CheckGitVersion() | ||
185 | self._SyncManifest(opt) | ||
186 | self._LinkManifest(opt.manifest_name) | ||
187 | |||
188 | if os.isatty(0) and os.isatty(1): | ||
189 | self._ConfigureUser() | ||
190 | self._ConfigureColor() | ||
191 | |||
192 | print '' | ||
193 | print 'repo initialized in %s' % self.manifest.topdir | ||