Mercurial > hg > fetch
annotate fetch.py @ 27:423b67ff4512
more stubbing
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 14 Nov 2011 21:32:54 -0800 |
parents | d495b610046a |
children | 5ecb6507931b |
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) |
27 | 153 shutil.copytree(path, dest) |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
154 else: |
27 | 155 if not os.path.exists(dest): |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
156 directory, filename = os.path.split(dest) |
27 | 157 if os.path.exists(directory): |
158 assert os.path.isdir(directory), "%s is not a directory" % directory | |
159 else: | |
160 os.makedirs(directory) | |
161 shutil.copy(path, dest) | |
25 | 162 shutil.rmtree(tmpdir) |
163 | |
164 def clone(self, dest): | |
165 """ | |
166 clones into a directory | |
167 """ | |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
168 raise NotImplementedError("Abstract base class") |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
169 |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
170 def update(self, dest): |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
171 """ |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
172 updates a checkout |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
173 """ |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
174 raise NotImplementedError("Abstract base class") |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
175 |
25 | 176 |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
177 if which('hg'): |
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
178 |
17 | 179 class HgFetcher(VCSFetcher): |
15 | 180 """checkout a mercurial repository""" |
181 type = 'hg' | |
0 | 182 |
19 | 183 def __init__(self, url, export=True): |
184 VCSFetcher.__init__(self, url, export=True) | |
185 self.hg = which('hg') | |
25 | 186 assert self.hg, "'hg' command not found" |
19 | 187 |
15 | 188 def __call__(self, dest): |
23 | 189 if os.path.exists(dest): |
15 | 190 assert os.path.isdir(dest) and os.path.exists(os.path.join(dest, '.hg')) |
19 | 191 call([self.hg, 'pull', self.url], cwd=dest) |
192 call([self.hg, 'update'], cwd=dest) | |
193 else: | |
23 | 194 if not os.path.exists(dest): |
195 os.mkdirs(dest) | |
196 call([self.hg, 'clone', self.url, dest]) | |
11
726c3d288733
* add convenience import in __init__
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
197 |
15 | 198 fetchers.append(HgFetcher) |
6
86f6f99e421b
add types for unimplemented dispatchers
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
199 |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
200 |
15 | 201 if which('git'): |
17 | 202 |
15 | 203 class GitFetcher(Fetcher): |
204 """checkout a git repository""" | |
205 type = 'git' | |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
206 |
20 | 207 def __init__(self, url, export=True): |
208 VCSFetcher.__init__(self, url, export=True) | |
23 | 209 self.git = which('git') |
210 | |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
211 def __call__(self, dest): |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
212 if os.path.exists(dest): |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
213 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
|
214 call([self.git, 'pull', self.url], cwd=dest) |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
215 call([self.hg, 'update'], cwd=dest) |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
216 else: |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
217 if not os.path.exists(dest): |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
218 os.mkdirs(dest) |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
219 call([self.hg, 'clone', self.url, dest]) |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
220 |
20 | 221 |
222 fetchers.append(GitFetcher) | |
17 | 223 |
16 | 224 __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
|
225 |
0 | 226 class Fetch(object): |
227 | |
21 | 228 def __init__(self, fetchers=fetchers[:], relative_to=None, strict=True): |
15 | 229 self.fetchers = fetchers |
230 self.relative_to = relative_to | |
231 self.strict = strict | |
0 | 232 |
15 | 233 def fetcher(self, _type): |
234 """find the fetcher for the appropriate type""" | |
235 for fetcher in fetchers: | |
236 if fetcher.match(_type): | |
237 return fetcher | |
0 | 238 |
15 | 239 def __call__(self, url, destination, type, **options): |
240 fetcher = self.fetcher(type) | |
241 assert fetcher is not None, "No fetcher found for type '%s'" % type | |
242 fetcher = fetcher(url, **options) | |
243 fetcher(destination) | |
2 | 244 |
15 | 245 def fetch(self, *items): |
2 | 246 |
15 | 247 if self.strict: |
248 # ensure all the required fetchers are available | |
249 types = set([i['type'] for i in items]) | |
250 assert not [i for i in types | |
251 if [True for fetcher in fetchers if fetcher.match(i)]] | |
4 | 252 |
15 | 253 for item in items: |
4 | 254 |
15 | 255 # fix up relative paths |
256 dest = item['dest'] | |
257 if not os.path.isabs(dest): | |
258 relative_to = self.relative_to or os.path.dirname(os.path.abspath(item['manifest'])) | |
259 dest = os.path.join(relative_to, dest) | |
4 | 260 |
15 | 261 # fetch the items |
262 self(item['url'], destination=dest, type=item['type'], **item['options']) | |
0 | 263 |
21 | 264 |
0 | 265 format_string = "[URL] [destination] [type] <options>" |
266 def read_manifests(*manifests): | |
15 | 267 """ |
268 read some manifests and return the items | |
269 | |
270 Format: | |
271 %s | |
272 """ % format_string | |
0 | 273 |
15 | 274 # sanity check |
275 assert not [i for i in manifests if not os.path.exists(i)] | |
0 | 276 |
15 | 277 retval = [] |
0 | 278 |
15 | 279 for manifest in manifests: |
280 for line in file(i).readlines(): | |
281 line = line.strip() | |
282 if line.startswith('#') or not line: | |
283 continue | |
284 line = line.split() | |
285 if len(line) not in (3,4): | |
286 raise Exception("Format should be: %s; line %s" % (format_string, line)) | |
287 options = {} | |
288 if len(line) == 4: | |
289 option_string = line.pop().rstrip(',') | |
290 try: | |
291 options = dict([[j.strip() for j in i.split('=', 1)] | |
292 for i in option_string.split(',')]) | |
293 except: | |
294 raise Exception("Options format should be: key=value,key2=value2,...; got %s" % option_string) | |
0 | 295 |
15 | 296 url, dest, _type = line |
297 retval.append(dict(url=url, dest=dest, type=_type, options=options, manifest=manifest)) | |
298 return retval | |
0 | 299 |
2 | 300 def main(args=sys.argv[1:]): |
0 | 301 |
15 | 302 # parse command line options |
303 usage = '%prog [options] manifest [manifest] [...]' | |
0 | 304 |
15 | 305 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): |
306 def format_description(self, description): | |
307 if description: | |
308 return description + '\n' | |
309 else: | |
310 return '' | |
0 | 311 |
15 | 312 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) |
313 parser.add_option('-o', '--output', | |
314 help="output relative to this location vs. the manifest location") | |
17 | 315 parser.add_option('-d', '--dest', # XXX unused |
15 | 316 action='append', |
317 help="output only these destinations") | |
318 parser.add_option('-s', '--strict', | |
319 action='store_true', default=False, | |
320 help="fail on error") | |
321 parser.add_option('--list-fetchers', dest='list_fetchers', | |
322 action='store_true', default=False, | |
323 help='list available fetchers and exit') | |
324 options, args = parser.parse_args(args) | |
0 | 325 |
15 | 326 if options.list_fetchers: |
17 | 327 types = set() |
328 for fetcher in fetchers: | |
329 if fetcher.type in types: | |
330 continue # occluded, should probably display separately | |
331 print '%s : %s' % (fetcher.type, fetcher.doc()) | |
332 types.add(fetcher.type) | |
15 | 333 parser.exit() |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
334 |
15 | 335 if not args: |
17 | 336 # TODO: could read from stdin |
15 | 337 parser.print_help() |
338 parser.exit() | |
0 | 339 |
15 | 340 items = read_manifests(*args) |
16 | 341 fetch = Fetch(fetchers, strict=options.strict) |
0 | 342 |
15 | 343 # download the files |
344 fetch.fetch(*items) | |
0 | 345 |
346 if __name__ == '__main__': | |
15 | 347 main() |
0 | 348 |