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