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