Mercurial > hg > fetch
annotate fetch.py @ 36:d3558b202acb
whitespace
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 15 Nov 2011 12:19:38 -0800 |
parents | d9b9c4599a3d |
children | f30fe9183e64 |
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 |
36 | 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 |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
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): |
34
14c791d8db59
pass the dest to clobber for tarball
Jeff Hammel <jhammel@mozilla.com>
parents:
32
diff
changeset
|
86 self.clobber(dest) |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
87 if os.path.exists(dest): |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
88 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
|
89 else: |
35 | 90 os.makedirs(dest) |
17 | 91 if self.subpath: |
92 raise NotImplementedError("should extract only a subpath of a tarball but I haven't finished it yet") | |
15 | 93 buffer = StringIO() |
94 buffer.write(self.download(self.url)) | |
95 buffer.seek(0) | |
96 tf = tarfile.open(mode='r', fileobj=buffer) | |
97 tf.extract(dest) | |
7 | 98 |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
99 fetchers = [FileFetcher, TarballFetcher] |
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
100 |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
101 ### VCS fetchers |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
102 |
11
726c3d288733
* add convenience import in __init__
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
103 import subprocess |
19 | 104 try: |
105 from subprocess import check_call as call | |
106 except ImportErorr: | |
107 raise # we need check_call, kinda | |
11
726c3d288733
* add convenience import in __init__
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
108 |
17 | 109 class VCSFetcher(Fetcher): |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
110 |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
111 command = None |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
112 |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
113 def call(*args, **kwargs): |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
114 assert command is not None, "Abstract base class" |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
115 call([self.command] + list(args), **kwargs) |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
116 |
17 | 117 def __init__(self, url, export=True): |
118 """ | |
119 - export : whether to strip the versioning information | |
120 """ | |
121 Fetcher.__init__(self, url) | |
122 self.export = export | |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
123 self.prog = self.type # name of app program |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
124 self.vcs_dir = '.' + self.type # subdirectory for version control |
17 | 125 |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
126 def __call__(self, dest): |
25 | 127 |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
128 self.clobber(dest) |
25 | 129 if self.subpath or self.export: |
130 # can only export with a subpath | |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
131 self.export(dest, subpath=self.subpath) |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
132 return |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
133 |
25 | 134 if os.path.exists(dest): |
135 assert os.path.isdir(dest) | |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
136 else: |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
137 self.clone(dest) |
25 | 138 |
139 def export(self, dest): | |
140 """ | |
141 export a clone of the directory | |
142 """ | |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
143 dest = os.path.abspath(dest) |
25 | 144 tmpdir = tempfile.mkdtmp() |
145 self.clone(tmpdir) | |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
146 path = tmpdir |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
147 if self.subpath: |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
148 path = os.path.join([tmpdir] + self.subpath) |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
149 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
|
150 self.clobber(dest) |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
151 if os.path.isdir(path): |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
152 if os.path.exists(dest): |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
153 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
|
154 else: |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
155 os.makedirs(dest) |
27 | 156 shutil.copytree(path, dest) |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
157 else: |
27 | 158 if not os.path.exists(dest): |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
159 directory, filename = os.path.split(dest) |
27 | 160 if os.path.exists(directory): |
161 assert os.path.isdir(directory), "%s is not a directory" % directory | |
162 else: | |
163 os.makedirs(directory) | |
164 shutil.copy(path, dest) | |
25 | 165 shutil.rmtree(tmpdir) |
166 | |
167 def clone(self, dest): | |
168 """ | |
169 clones into a directory | |
170 """ | |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
171 raise NotImplementedError("Abstract base class") |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
172 |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
173 def update(self, dest): |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
174 """ |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
175 updates a checkout |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
176 """ |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
177 raise NotImplementedError("Abstract base class") |
25 | 178 |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
179 def versioned(self, directory): |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
180 return os.path.exists(os.path.join(directory, self.vcs_dir)) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
181 |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
182 |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
183 if which('hg'): |
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
184 |
17 | 185 class HgFetcher(VCSFetcher): |
15 | 186 """checkout a mercurial repository""" |
187 type = 'hg' | |
0 | 188 |
19 | 189 def __init__(self, url, export=True): |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
190 VCSFetcher.__init__(self, url, export=export) |
19 | 191 self.hg = which('hg') |
25 | 192 assert self.hg, "'hg' command not found" |
19 | 193 |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
194 def clone(self, dest): |
23 | 195 if os.path.exists(dest): |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
196 assert os.path.isdir(dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
197 call([self.hg, 'clone', self.url, dest]) |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
198 |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
199 def update(self, dest): |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
200 assert os.path.versioned(dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
201 assert os.path.exists(dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
202 call([self.hg, 'pull', self.url], cwd=dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
203 call([self.hg, 'update', '-C'], cwd=dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
204 |
11
726c3d288733
* add convenience import in __init__
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
205 |
15 | 206 fetchers.append(HgFetcher) |
6
86f6f99e421b
add types for unimplemented dispatchers
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
207 |
15 | 208 if which('git'): |
17 | 209 |
15 | 210 class GitFetcher(Fetcher): |
211 """checkout a git repository""" | |
212 type = 'git' | |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
213 |
20 | 214 def __init__(self, url, export=True): |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
215 VCSFetcher.__init__(self, url, export=export) |
23 | 216 self.git = which('git') |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
217 assert self.git, "'git' command not found" |
23 | 218 |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
219 def update(self, dest): |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
220 assert os.path.isdir(dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
221 assert os.path.versioned(dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
222 call([self.git, 'pull', self.url], cwd=dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
223 call([self.git, 'update'], cwd=dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
224 |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
225 def clone(self, dest): |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
226 if not os.path.exists(dest): |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
227 os.makedirs(dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
228 call([self.git, 'clone', self.url, dest]) |
20 | 229 |
230 fetchers.append(GitFetcher) | |
17 | 231 |
16 | 232 __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
|
233 |
0 | 234 class Fetch(object): |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
235 |
21 | 236 def __init__(self, fetchers=fetchers[:], relative_to=None, strict=True): |
15 | 237 self.fetchers = fetchers |
238 self.relative_to = relative_to | |
239 self.strict = strict | |
0 | 240 |
15 | 241 def fetcher(self, _type): |
242 """find the fetcher for the appropriate type""" | |
243 for fetcher in fetchers: | |
244 if fetcher.match(_type): | |
245 return fetcher | |
0 | 246 |
15 | 247 def __call__(self, url, destination, type, **options): |
248 fetcher = self.fetcher(type) | |
249 assert fetcher is not None, "No fetcher found for type '%s'" % type | |
250 fetcher = fetcher(url, **options) | |
251 fetcher(destination) | |
2 | 252 |
15 | 253 def fetch(self, *items): |
2 | 254 |
15 | 255 if self.strict: |
256 # ensure all the required fetchers are available | |
257 types = set([i['type'] for i in items]) | |
258 assert not [i for i in types | |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
259 if not [True for fetcher in fetchers |
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
260 if fetcher.match(i)]] |
4 | 261 |
15 | 262 for item in items: |
4 | 263 |
15 | 264 # fix up relative paths |
265 dest = item['dest'] | |
266 if not os.path.isabs(dest): | |
31
60e0e932570e
fix one error but not the one i was aiming for
Jeff Hammel <jhammel@mozilla.com>
parents:
29
diff
changeset
|
267 relative_to = self.relative_to |
60e0e932570e
fix one error but not the one i was aiming for
Jeff Hammel <jhammel@mozilla.com>
parents:
29
diff
changeset
|
268 if not relative_to: |
60e0e932570e
fix one error but not the one i was aiming for
Jeff Hammel <jhammel@mozilla.com>
parents:
29
diff
changeset
|
269 if isinstance(item['manifest'], basestring): |
60e0e932570e
fix one error but not the one i was aiming for
Jeff Hammel <jhammel@mozilla.com>
parents:
29
diff
changeset
|
270 relative_to = os.path.dirname(os.path.abspath(item['manifest'])) |
60e0e932570e
fix one error but not the one i was aiming for
Jeff Hammel <jhammel@mozilla.com>
parents:
29
diff
changeset
|
271 else: |
60e0e932570e
fix one error but not the one i was aiming for
Jeff Hammel <jhammel@mozilla.com>
parents:
29
diff
changeset
|
272 relative_to = os.getcwd() |
60e0e932570e
fix one error but not the one i was aiming for
Jeff Hammel <jhammel@mozilla.com>
parents:
29
diff
changeset
|
273 dest = os.path.normpath(os.path.join(relative_to, dest)) |
4 | 274 |
15 | 275 # fetch the items |
276 self(item['url'], destination=dest, type=item['type'], **item['options']) | |
0 | 277 |
21 | 278 |
0 | 279 format_string = "[URL] [destination] [type] <options>" |
280 def read_manifests(*manifests): | |
15 | 281 """ |
282 read some manifests and return the items | |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
283 |
15 | 284 Format: |
285 %s | |
286 """ % format_string | |
0 | 287 |
15 | 288 retval = [] |
0 | 289 |
15 | 290 for manifest in manifests: |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
291 if isinstance(manifest, basestring): |
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
292 assert os.path.exists(manifest), "manifest '%s' not found" % manifest |
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
293 f = file(manifest) |
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
294 else: |
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
295 f = manifest |
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
296 |
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
297 for line in f.readlines(): |
15 | 298 line = line.strip() |
299 if line.startswith('#') or not line: | |
300 continue | |
301 line = line.split() | |
302 if len(line) not in (3,4): | |
303 raise Exception("Format should be: %s; line %s" % (format_string, line)) | |
304 options = {} | |
305 if len(line) == 4: | |
306 option_string = line.pop().rstrip(',') | |
307 try: | |
308 options = dict([[j.strip() for j in i.split('=', 1)] | |
309 for i in option_string.split(',')]) | |
310 except: | |
311 raise Exception("Options format should be: key=value,key2=value2,...; got %s" % option_string) | |
0 | 312 |
15 | 313 url, dest, _type = line |
314 retval.append(dict(url=url, dest=dest, type=_type, options=options, manifest=manifest)) | |
315 return retval | |
0 | 316 |
2 | 317 def main(args=sys.argv[1:]): |
0 | 318 |
15 | 319 # parse command line options |
320 usage = '%prog [options] manifest [manifest] [...]' | |
0 | 321 |
15 | 322 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): |
323 def format_description(self, description): | |
324 if description: | |
325 return description + '\n' | |
326 else: | |
327 return '' | |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
328 |
15 | 329 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) |
330 parser.add_option('-o', '--output', | |
331 help="output relative to this location vs. the manifest location") | |
17 | 332 parser.add_option('-d', '--dest', # XXX unused |
15 | 333 action='append', |
334 help="output only these destinations") | |
335 parser.add_option('-s', '--strict', | |
336 action='store_true', default=False, | |
337 help="fail on error") | |
338 parser.add_option('--list-fetchers', dest='list_fetchers', | |
339 action='store_true', default=False, | |
340 help='list available fetchers and exit') | |
341 options, args = parser.parse_args(args) | |
0 | 342 |
15 | 343 if options.list_fetchers: |
17 | 344 types = set() |
345 for fetcher in fetchers: | |
346 if fetcher.type in types: | |
347 continue # occluded, should probably display separately | |
348 print '%s : %s' % (fetcher.type, fetcher.doc()) | |
349 types.add(fetcher.type) | |
15 | 350 parser.exit() |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
351 |
15 | 352 if not args: |
17 | 353 # TODO: could read from stdin |
15 | 354 parser.print_help() |
355 parser.exit() | |
0 | 356 |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
357 # sanity check |
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
358 assert not [i for i in args if not os.path.exists(i)] |
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
359 |
15 | 360 items = read_manifests(*args) |
32 | 361 fetch = Fetch(fetchers, relative_to=options.output, strict=options.strict) |
0 | 362 |
15 | 363 # download the files |
364 fetch.fetch(*items) | |
0 | 365 |
366 if __name__ == '__main__': | |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
367 main() |
0 | 368 |