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