Mercurial > hg > fetch
annotate fetch.py @ 26:d495b610046a
more stubbing; tests work again
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 14 Nov 2011 21:25:56 -0800 |
parents | e628ce3ae49f |
children | 423b67ff4512 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
4 fetch stuff from the interwebs | |
5 """ | |
6 | |
7 import os | |
23 | 8 import shutil |
0 | 9 import sys |
10 import optparse | |
11 | |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
12 __all__ = ['Fetcher', 'Fetch', 'main'] |
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
13 |
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
14 def which(executable, path=os.environ['PATH']): |
15 | 15 """python equivalent of which; should really be in the stdlib""" |
16 dirs = path.split(os.pathsep) | |
17 for dir in dirs: | |
18 if os.path.isfile(os.path.join(dir, executable)): | |
19 return os.path.join(dir, executable) | |
7 | 20 |
0 | 21 class Fetcher(object): |
15 | 22 """abstract base class for resource fetchers""" |
0 | 23 |
15 | 24 @classmethod |
25 def match(cls, _type): | |
26 return _type == cls.type | |
0 | 27 |
23 | 28 def __init__(self, url, clobber=True): |
17 | 29 self.subpath = None |
30 if '#' in url: | |
31 url, self.subpath = url.rsplit('#') | |
25 | 32 if self.subpath: |
33 self.subpath = self.subpath.split('/') | |
15 | 34 self.url = url |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
35 self._clobber = clobber |
0 | 36 |
15 | 37 def __call__(self, dest): |
17 | 38 raise NotImplementedError("Should be called by implementing class") |
39 | |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
40 def clobber(self, dest): |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
41 """clobbers if self._clobber is set""" |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
42 if self._clobber and os.path.exists(dest): |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
43 if os.path.isfile(dest): |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
44 os.remove(dest) |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
45 else: |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
46 shutil.rmtree(dest) |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
47 |
17 | 48 @classmethod |
49 def doc(cls): | |
50 """return docstring for the instance""" | |
51 retval = getattr(cls, '__doc__', '').strip() | |
52 return ' '.join(retval.split()) | |
0 | 53 |
7 | 54 ### standard dispatchers - always available |
0 | 55 |
7 | 56 import tarfile |
0 | 57 import urllib2 |
7 | 58 from StringIO import StringIO |
0 | 59 |
5 | 60 class FileFetcher(Fetcher): |
15 | 61 """fetch a single file""" |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
62 # Note: subpath and clobber for single files are ignored |
0 | 63 |
15 | 64 type = 'file' |
0 | 65 |
15 | 66 @classmethod |
67 def download(cls, url): | |
68 return urllib2.urlopen(url).read() | |
0 | 69 |
15 | 70 def __call__(self, dest): |
25 | 71 |
15 | 72 if os.path.isdir(dest): |
73 filename = self.url.rsplit('/', 1)[-1] | |
74 dest = os.path.join(dest, filename) | |
75 f = file(dest, 'w') | |
76 f.write(self.download(self.url)) | |
77 f.close() | |
0 | 78 |
6
86f6f99e421b
add types for unimplemented dispatchers
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
79 |
5 | 80 class TarballFetcher(FileFetcher): |
15 | 81 """fetch and extract a tarball""" |
0 | 82 |
15 | 83 type = 'tar' |
0 | 84 |
15 | 85 def __call__(self, dest): |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
86 if self.clobber: |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
87 shutil.rmtree(dest) |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
88 if os.path.exists(dest): |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
89 assert os.path.isdir(dest), "Destination must be a directory" |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
90 else: |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
91 os.mkdirs(dest) |
17 | 92 if self.subpath: |
93 raise NotImplementedError("should extract only a subpath of a tarball but I haven't finished it yet") | |
15 | 94 buffer = StringIO() |
95 buffer.write(self.download(self.url)) | |
96 buffer.seek(0) | |
97 tf = tarfile.open(mode='r', fileobj=buffer) | |
98 tf.extract(dest) | |
7 | 99 |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
100 fetchers = [FileFetcher, TarballFetcher] |
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
101 |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
102 ### VCS fetchers |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
103 |
11
726c3d288733
* add convenience import in __init__
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
104 import subprocess |
19 | 105 try: |
106 from subprocess import check_call as call | |
107 except ImportErorr: | |
108 raise # we need check_call, kinda | |
11
726c3d288733
* add convenience import in __init__
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
109 |
17 | 110 class VCSFetcher(Fetcher): |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
111 |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
112 command = None |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
113 |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
114 def call(*args, **kwargs): |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
115 assert command is not None, "Abstract base class" |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
116 call([self.command] + list(args), **kwargs) |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
117 |
17 | 118 def __init__(self, url, export=True): |
119 """ | |
120 - export : whether to strip the versioning information | |
121 """ | |
122 Fetcher.__init__(self, url) | |
123 self.export = export | |
124 | |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
125 def __call__(self, dest): |
25 | 126 |
127 if self.subpath or self.export: | |
128 # can only export with a subpath | |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
129 self.export(dest, subpath=self.subpath) |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
130 return |
25 | 131 |
132 if os.path.exists(dest): | |
133 assert os.path.isdir(dest) | |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
134 self.clone(dest) |
25 | 135 |
136 def export(self, dest): | |
137 """ | |
138 export a clone of the directory | |
139 """ | |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
140 dest = os.path.abspath(dest) |
25 | 141 tmpdir = tempfile.mkdtmp() |
142 self.clone(tmpdir) | |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
143 path = tmpdir |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
144 if self.subpath: |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
145 path = os.path.join([tmpdir] + self.subpath) |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
146 assert os.path.exists(path), "subpath %s of %s not found" % (os.path.sep.join(self.subpath), self.url) |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
147 self.clobber(dest) |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
148 if os.path.isdir(path): |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
149 if os.path.exists(dest): |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
150 assert os.path.isdir(dest), "source is a directory; destination is a file" |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
151 else: |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
152 os.makedirs(dest) |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
153 else: |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
154 if os.path.exists(dest): |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
155 assert os.path.isfile(dest), "" |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
156 else: |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
157 directory, filename = os.path.split(dest) |
25 | 158 shutil.rmtree(tmpdir) |
159 | |
160 def clone(self, dest): | |
161 """ | |
162 clones into a directory | |
163 """ | |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
164 raise NotImplementedError("Abstract base class") |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
165 |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
166 def update(self, dest): |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
167 """ |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
168 updates a checkout |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
169 """ |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
170 raise NotImplementedError("Abstract base class") |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
171 |
25 | 172 |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
173 if which('hg'): |
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
174 |
17 | 175 class HgFetcher(VCSFetcher): |
15 | 176 """checkout a mercurial repository""" |
177 type = 'hg' | |
0 | 178 |
19 | 179 def __init__(self, url, export=True): |
180 VCSFetcher.__init__(self, url, export=True) | |
181 self.hg = which('hg') | |
25 | 182 assert self.hg, "'hg' command not found" |
19 | 183 |
15 | 184 def __call__(self, dest): |
23 | 185 if os.path.exists(dest): |
15 | 186 assert os.path.isdir(dest) and os.path.exists(os.path.join(dest, '.hg')) |
19 | 187 call([self.hg, 'pull', self.url], cwd=dest) |
188 call([self.hg, 'update'], cwd=dest) | |
189 else: | |
23 | 190 if not os.path.exists(dest): |
191 os.mkdirs(dest) | |
192 call([self.hg, 'clone', self.url, dest]) | |
11
726c3d288733
* add convenience import in __init__
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
193 |
15 | 194 fetchers.append(HgFetcher) |
6
86f6f99e421b
add types for unimplemented dispatchers
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
195 |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
196 |
15 | 197 if which('git'): |
17 | 198 |
15 | 199 class GitFetcher(Fetcher): |
200 """checkout a git repository""" | |
201 type = 'git' | |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
202 |
20 | 203 def __init__(self, url, export=True): |
204 VCSFetcher.__init__(self, url, export=True) | |
23 | 205 self.git = which('git') |
206 | |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
207 def __call__(self, dest): |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
208 if os.path.exists(dest): |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
209 assert os.path.isdir(dest) and os.path.exists(os.path.join(dest, '.git')) |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
210 call([self.git, 'pull', self.url], cwd=dest) |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
211 call([self.hg, 'update'], cwd=dest) |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
212 else: |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
213 if not os.path.exists(dest): |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
214 os.mkdirs(dest) |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
215 call([self.hg, 'clone', self.url, dest]) |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
216 |
20 | 217 |
218 fetchers.append(GitFetcher) | |
17 | 219 |
16 | 220 __all__ += [i.__name__ for i in fetchers] |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
221 |
0 | 222 class Fetch(object): |
223 | |
21 | 224 def __init__(self, fetchers=fetchers[:], relative_to=None, strict=True): |
15 | 225 self.fetchers = fetchers |
226 self.relative_to = relative_to | |
227 self.strict = strict | |
0 | 228 |
15 | 229 def fetcher(self, _type): |
230 """find the fetcher for the appropriate type""" | |
231 for fetcher in fetchers: | |
232 if fetcher.match(_type): | |
233 return fetcher | |
0 | 234 |
15 | 235 def __call__(self, url, destination, type, **options): |
236 fetcher = self.fetcher(type) | |
237 assert fetcher is not None, "No fetcher found for type '%s'" % type | |
238 fetcher = fetcher(url, **options) | |
239 fetcher(destination) | |
2 | 240 |
15 | 241 def fetch(self, *items): |
2 | 242 |
15 | 243 if self.strict: |
244 # ensure all the required fetchers are available | |
245 types = set([i['type'] for i in items]) | |
246 assert not [i for i in types | |
247 if [True for fetcher in fetchers if fetcher.match(i)]] | |
4 | 248 |
15 | 249 for item in items: |
4 | 250 |
15 | 251 # fix up relative paths |
252 dest = item['dest'] | |
253 if not os.path.isabs(dest): | |
254 relative_to = self.relative_to or os.path.dirname(os.path.abspath(item['manifest'])) | |
255 dest = os.path.join(relative_to, dest) | |
4 | 256 |
15 | 257 # fetch the items |
258 self(item['url'], destination=dest, type=item['type'], **item['options']) | |
0 | 259 |
21 | 260 |
0 | 261 format_string = "[URL] [destination] [type] <options>" |
262 def read_manifests(*manifests): | |
15 | 263 """ |
264 read some manifests and return the items | |
265 | |
266 Format: | |
267 %s | |
268 """ % format_string | |
0 | 269 |
15 | 270 # sanity check |
271 assert not [i for i in manifests if not os.path.exists(i)] | |
0 | 272 |
15 | 273 retval = [] |
0 | 274 |
15 | 275 for manifest in manifests: |
276 for line in file(i).readlines(): | |
277 line = line.strip() | |
278 if line.startswith('#') or not line: | |
279 continue | |
280 line = line.split() | |
281 if len(line) not in (3,4): | |
282 raise Exception("Format should be: %s; line %s" % (format_string, line)) | |
283 options = {} | |
284 if len(line) == 4: | |
285 option_string = line.pop().rstrip(',') | |
286 try: | |
287 options = dict([[j.strip() for j in i.split('=', 1)] | |
288 for i in option_string.split(',')]) | |
289 except: | |
290 raise Exception("Options format should be: key=value,key2=value2,...; got %s" % option_string) | |
0 | 291 |
15 | 292 url, dest, _type = line |
293 retval.append(dict(url=url, dest=dest, type=_type, options=options, manifest=manifest)) | |
294 return retval | |
0 | 295 |
2 | 296 def main(args=sys.argv[1:]): |
0 | 297 |
15 | 298 # parse command line options |
299 usage = '%prog [options] manifest [manifest] [...]' | |
0 | 300 |
15 | 301 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): |
302 def format_description(self, description): | |
303 if description: | |
304 return description + '\n' | |
305 else: | |
306 return '' | |
0 | 307 |
15 | 308 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) |
309 parser.add_option('-o', '--output', | |
310 help="output relative to this location vs. the manifest location") | |
17 | 311 parser.add_option('-d', '--dest', # XXX unused |
15 | 312 action='append', |
313 help="output only these destinations") | |
314 parser.add_option('-s', '--strict', | |
315 action='store_true', default=False, | |
316 help="fail on error") | |
317 parser.add_option('--list-fetchers', dest='list_fetchers', | |
318 action='store_true', default=False, | |
319 help='list available fetchers and exit') | |
320 options, args = parser.parse_args(args) | |
0 | 321 |
15 | 322 if options.list_fetchers: |
17 | 323 types = set() |
324 for fetcher in fetchers: | |
325 if fetcher.type in types: | |
326 continue # occluded, should probably display separately | |
327 print '%s : %s' % (fetcher.type, fetcher.doc()) | |
328 types.add(fetcher.type) | |
15 | 329 parser.exit() |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
330 |
15 | 331 if not args: |
17 | 332 # TODO: could read from stdin |
15 | 333 parser.print_help() |
334 parser.exit() | |
0 | 335 |
15 | 336 items = read_manifests(*args) |
16 | 337 fetch = Fetch(fetchers, strict=options.strict) |
0 | 338 |
15 | 339 # download the files |
340 fetch.fetch(*items) | |
0 | 341 |
342 if __name__ == '__main__': | |
15 | 343 main() |
0 | 344 |