Mercurial > hg > fetch
annotate fetch.py @ 20:738d84b4de52
start documenting
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 09 Nov 2011 19:55:15 -0800 |
parents | d69041957c0e |
children | 0706968f01bb |
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 |
20 | 127 def __init__(self, url, export=True): |
128 VCSFetcher.__init__(self, url, export=True) | |
129 self.hg = which('git') | |
130 | |
131 fetchers.append(GitFetcher) | |
17 | 132 |
16 | 133 __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
|
134 |
0 | 135 class Fetch(object): |
136 | |
15 | 137 def __init__(self, fetchers, relative_to=None, strict=True): |
138 self.fetchers = fetchers | |
139 self.relative_to = relative_to | |
140 self.strict = strict | |
0 | 141 |
15 | 142 def fetcher(self, _type): |
143 """find the fetcher for the appropriate type""" | |
144 for fetcher in fetchers: | |
145 if fetcher.match(_type): | |
146 return fetcher | |
0 | 147 |
15 | 148 def __call__(self, url, destination, type, **options): |
149 fetcher = self.fetcher(type) | |
150 assert fetcher is not None, "No fetcher found for type '%s'" % type | |
151 fetcher = fetcher(url, **options) | |
152 fetcher(destination) | |
2 | 153 |
15 | 154 def fetch(self, *items): |
2 | 155 |
15 | 156 if self.strict: |
157 # ensure all the required fetchers are available | |
158 types = set([i['type'] for i in items]) | |
159 assert not [i for i in types | |
160 if [True for fetcher in fetchers if fetcher.match(i)]] | |
4 | 161 |
15 | 162 for item in items: |
4 | 163 |
15 | 164 # fix up relative paths |
165 dest = item['dest'] | |
166 if not os.path.isabs(dest): | |
167 relative_to = self.relative_to or os.path.dirname(os.path.abspath(item['manifest'])) | |
168 dest = os.path.join(relative_to, dest) | |
4 | 169 |
15 | 170 # fetch the items |
171 self(item['url'], destination=dest, type=item['type'], **item['options']) | |
0 | 172 |
173 format_string = "[URL] [destination] [type] <options>" | |
174 def read_manifests(*manifests): | |
15 | 175 """ |
176 read some manifests and return the items | |
177 | |
178 Format: | |
179 %s | |
180 """ % format_string | |
0 | 181 |
15 | 182 # sanity check |
183 assert not [i for i in manifests if not os.path.exists(i)] | |
0 | 184 |
15 | 185 retval = [] |
0 | 186 |
15 | 187 for manifest in manifests: |
188 for line in file(i).readlines(): | |
189 line = line.strip() | |
190 if line.startswith('#') or not line: | |
191 continue | |
192 line = line.split() | |
193 if len(line) not in (3,4): | |
194 raise Exception("Format should be: %s; line %s" % (format_string, line)) | |
195 options = {} | |
196 if len(line) == 4: | |
197 option_string = line.pop().rstrip(',') | |
198 try: | |
199 options = dict([[j.strip() for j in i.split('=', 1)] | |
200 for i in option_string.split(',')]) | |
201 except: | |
202 raise Exception("Options format should be: key=value,key2=value2,...; got %s" % option_string) | |
0 | 203 |
15 | 204 url, dest, _type = line |
205 retval.append(dict(url=url, dest=dest, type=_type, options=options, manifest=manifest)) | |
206 return retval | |
0 | 207 |
2 | 208 def main(args=sys.argv[1:]): |
0 | 209 |
15 | 210 # parse command line options |
211 usage = '%prog [options] manifest [manifest] [...]' | |
0 | 212 |
15 | 213 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): |
214 def format_description(self, description): | |
215 if description: | |
216 return description + '\n' | |
217 else: | |
218 return '' | |
0 | 219 |
15 | 220 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) |
221 parser.add_option('-o', '--output', | |
222 help="output relative to this location vs. the manifest location") | |
17 | 223 parser.add_option('-d', '--dest', # XXX unused |
15 | 224 action='append', |
225 help="output only these destinations") | |
226 parser.add_option('-s', '--strict', | |
227 action='store_true', default=False, | |
228 help="fail on error") | |
229 parser.add_option('--list-fetchers', dest='list_fetchers', | |
230 action='store_true', default=False, | |
231 help='list available fetchers and exit') | |
232 options, args = parser.parse_args(args) | |
0 | 233 |
15 | 234 if options.list_fetchers: |
17 | 235 types = set() |
236 for fetcher in fetchers: | |
237 if fetcher.type in types: | |
238 continue # occluded, should probably display separately | |
239 print '%s : %s' % (fetcher.type, fetcher.doc()) | |
240 types.add(fetcher.type) | |
15 | 241 parser.exit() |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
242 |
15 | 243 if not args: |
17 | 244 # TODO: could read from stdin |
15 | 245 parser.print_help() |
246 parser.exit() | |
0 | 247 |
15 | 248 items = read_manifests(*args) |
16 | 249 fetch = Fetch(fetchers, strict=options.strict) |
0 | 250 |
15 | 251 # download the files |
252 fetch.fetch(*items) | |
0 | 253 |
254 if __name__ == '__main__': | |
15 | 255 main() |
0 | 256 |