Mercurial > hg > fetch
annotate fetch.py @ 45:87c22bbcda2b
use correct function name
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 15 Nov 2011 14:30:16 -0800 |
parents | b98c8a8a881b |
children | fb05b7616bd8 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
4 fetch stuff from the interwebs | |
5 """ | |
6 | |
44 | 7 import optparse |
0 | 8 import os |
23 | 9 import shutil |
0 | 10 import sys |
44 | 11 import tempfile |
0 | 12 |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
13 __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
|
14 |
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
15 def which(executable, path=os.environ['PATH']): |
15 | 16 """python equivalent of which; should really be in the stdlib""" |
17 dirs = path.split(os.pathsep) | |
18 for dir in dirs: | |
19 if os.path.isfile(os.path.join(dir, executable)): | |
20 return os.path.join(dir, executable) | |
7 | 21 |
0 | 22 class Fetcher(object): |
15 | 23 """abstract base class for resource fetchers""" |
0 | 24 |
15 | 25 @classmethod |
26 def match(cls, _type): | |
27 return _type == cls.type | |
0 | 28 |
37
f30fe9183e64
remove clobber functionality for now; this should live in the master Fetch class so that resources arent multiply clobbered
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
29 def __init__(self, url): |
17 | 30 self.subpath = None |
31 if '#' in url: | |
32 url, self.subpath = url.rsplit('#') | |
25 | 33 if self.subpath: |
34 self.subpath = self.subpath.split('/') | |
15 | 35 self.url = url |
0 | 36 |
15 | 37 def __call__(self, dest): |
17 | 38 raise NotImplementedError("Should be called by implementing class") |
39 | |
40 @classmethod | |
41 def doc(cls): | |
42 """return docstring for the instance""" | |
43 retval = getattr(cls, '__doc__', '').strip() | |
44 return ' '.join(retval.split()) | |
0 | 45 |
7 | 46 ### standard dispatchers - always available |
0 | 47 |
7 | 48 import tarfile |
0 | 49 import urllib2 |
7 | 50 from StringIO import StringIO |
0 | 51 |
5 | 52 class FileFetcher(Fetcher): |
15 | 53 """fetch a single file""" |
37
f30fe9183e64
remove clobber functionality for now; this should live in the master Fetch class so that resources arent multiply clobbered
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
54 # Note: subpath for single files is ignored |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
55 |
15 | 56 type = 'file' |
0 | 57 |
15 | 58 @classmethod |
59 def download(cls, url): | |
60 return urllib2.urlopen(url).read() | |
0 | 61 |
15 | 62 def __call__(self, dest): |
25 | 63 |
15 | 64 if os.path.isdir(dest): |
65 filename = self.url.rsplit('/', 1)[-1] | |
66 dest = os.path.join(dest, filename) | |
67 f = file(dest, 'w') | |
68 f.write(self.download(self.url)) | |
69 f.close() | |
0 | 70 |
6
86f6f99e421b
add types for unimplemented dispatchers
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
71 |
5 | 72 class TarballFetcher(FileFetcher): |
15 | 73 """fetch and extract a tarball""" |
0 | 74 |
15 | 75 type = 'tar' |
0 | 76 |
15 | 77 def __call__(self, dest): |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
78 if os.path.exists(dest): |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
79 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
|
80 else: |
35 | 81 os.makedirs(dest) |
15 | 82 buffer = StringIO() |
83 buffer.write(self.download(self.url)) | |
84 buffer.seek(0) | |
85 tf = tarfile.open(mode='r', fileobj=buffer) | |
38 | 86 members = tf.getmembers() |
39
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
87 if self.subpath: |
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
88 |
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
89 # build list of files to extract |
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
90 _members = [] |
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
91 |
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
92 toppath = None |
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
93 for member in members: |
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
94 split = member.name.split(os.path.sep) |
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
95 if toppath: |
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
96 # ensure that for subpaths that only one top level directory exists |
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
97 # XXX needed? |
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
98 assert toppath == split[0], "Multiple top-level archives found" |
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
99 else: |
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
100 toppath = split[0] |
40
e103ae19c2a0
get subpaths working for tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
101 if split and split[1:len(self.subpath)+1] == self.subpath: |
e103ae19c2a0
get subpaths working for tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
102 member.name = os.path.sep.join(split[1:]) |
e103ae19c2a0
get subpaths working for tarballs
Jeff Hammel <jhammel@mozilla.com>
parents:
39
diff
changeset
|
103 _members.append(member) |
39
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
104 |
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
105 members = _members |
8addc3712e75
partially and borken support for subpaths
Jeff Hammel <jhammel@mozilla.com>
parents:
38
diff
changeset
|
106 |
38 | 107 for member in members: |
108 tf.extract(member, dest) | |
7 | 109 |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
110 fetchers = [FileFetcher, TarballFetcher] |
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
111 |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
112 ### VCS fetchers |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
113 |
11
726c3d288733
* add convenience import in __init__
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
114 import subprocess |
19 | 115 try: |
116 from subprocess import check_call as call | |
117 except ImportErorr: | |
118 raise # we need check_call, kinda | |
11
726c3d288733
* add convenience import in __init__
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
119 |
17 | 120 class VCSFetcher(Fetcher): |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
121 |
42
6e978ddf5135
self._export vs self.export, the function
Jeff Hammel <jhammel@mozilla.com>
parents:
40
diff
changeset
|
122 command = None # name of the VCS command (currently unused) |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
123 |
17 | 124 def __init__(self, url, export=True): |
125 """ | |
126 - export : whether to strip the versioning information | |
127 """ | |
128 Fetcher.__init__(self, url) | |
42
6e978ddf5135
self._export vs self.export, the function
Jeff Hammel <jhammel@mozilla.com>
parents:
40
diff
changeset
|
129 self._export = export |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
130 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
|
131 self.vcs_dir = '.' + self.type # subdirectory for version control |
17 | 132 |
42
6e978ddf5135
self._export vs self.export, the function
Jeff Hammel <jhammel@mozilla.com>
parents:
40
diff
changeset
|
133 def call(*args, **kwargs): |
6e978ddf5135
self._export vs self.export, the function
Jeff Hammel <jhammel@mozilla.com>
parents:
40
diff
changeset
|
134 assert command is not None, "Abstract base class" |
6e978ddf5135
self._export vs self.export, the function
Jeff Hammel <jhammel@mozilla.com>
parents:
40
diff
changeset
|
135 call([self.command] + list(args), **kwargs) |
6e978ddf5135
self._export vs self.export, the function
Jeff Hammel <jhammel@mozilla.com>
parents:
40
diff
changeset
|
136 |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
137 def __call__(self, dest): |
25 | 138 |
42
6e978ddf5135
self._export vs self.export, the function
Jeff Hammel <jhammel@mozilla.com>
parents:
40
diff
changeset
|
139 if self.subpath or self._export: |
25 | 140 # can only export with a subpath |
43 | 141 self.export(dest) |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
142 return |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
143 |
25 | 144 if os.path.exists(dest): |
145 assert os.path.isdir(dest) | |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
146 else: |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
147 self.clone(dest) |
25 | 148 |
149 def export(self, dest): | |
150 """ | |
151 export a clone of the directory | |
152 """ | |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
153 dest = os.path.abspath(dest) |
45 | 154 tmpdir = tempfile.mkdtemp() |
25 | 155 self.clone(tmpdir) |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
156 path = tmpdir |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
157 if self.subpath: |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
158 path = os.path.join([tmpdir] + self.subpath) |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
159 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
|
160 if os.path.isdir(path): |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
161 if os.path.exists(dest): |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
162 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
|
163 else: |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
164 os.makedirs(dest) |
27 | 165 shutil.copytree(path, dest) |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
166 else: |
27 | 167 if not os.path.exists(dest): |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
168 directory, filename = os.path.split(dest) |
27 | 169 if os.path.exists(directory): |
170 assert os.path.isdir(directory), "%s is not a directory" % directory | |
171 else: | |
172 os.makedirs(directory) | |
173 shutil.copy(path, dest) | |
25 | 174 shutil.rmtree(tmpdir) |
175 | |
176 def clone(self, dest): | |
177 """ | |
178 clones into a directory | |
179 """ | |
24
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
180 raise NotImplementedError("Abstract base class") |
b1f65f3bd1bc
pretend to flesh out git fetcher
Jeff Hammel <jhammel@mozilla.com>
parents:
23
diff
changeset
|
181 |
26
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
182 def update(self, dest): |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
183 """ |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
184 updates a checkout |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
185 """ |
d495b610046a
more stubbing; tests work again
Jeff Hammel <jhammel@mozilla.com>
parents:
25
diff
changeset
|
186 raise NotImplementedError("Abstract base class") |
25 | 187 |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
188 def versioned(self, directory): |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
189 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
|
190 |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
191 |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
192 if which('hg'): |
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
193 |
17 | 194 class HgFetcher(VCSFetcher): |
15 | 195 """checkout a mercurial repository""" |
196 type = 'hg' | |
0 | 197 |
19 | 198 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
|
199 VCSFetcher.__init__(self, url, export=export) |
19 | 200 self.hg = which('hg') |
25 | 201 assert self.hg, "'hg' command not found" |
19 | 202 |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
203 def clone(self, dest): |
23 | 204 if os.path.exists(dest): |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
205 assert os.path.isdir(dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
206 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
|
207 |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
208 def update(self, dest): |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
209 assert os.path.versioned(dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
210 assert os.path.exists(dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
211 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
|
212 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
|
213 |
11
726c3d288733
* add convenience import in __init__
Jeff Hammel <jhammel@mozilla.com>
parents:
10
diff
changeset
|
214 |
15 | 215 fetchers.append(HgFetcher) |
6
86f6f99e421b
add types for unimplemented dispatchers
Jeff Hammel <jhammel@mozilla.com>
parents:
5
diff
changeset
|
216 |
15 | 217 if which('git'): |
17 | 218 |
15 | 219 class GitFetcher(Fetcher): |
220 """checkout a git repository""" | |
221 type = 'git' | |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
222 |
20 | 223 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
|
224 VCSFetcher.__init__(self, url, export=export) |
23 | 225 self.git = which('git') |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
226 assert self.git, "'git' command not found" |
23 | 227 |
28
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
228 def update(self, dest): |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
229 assert os.path.isdir(dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
230 assert os.path.versioned(dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
231 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
|
232 call([self.git, 'update'], cwd=dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
233 |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
234 def clone(self, dest): |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
235 if not os.path.exists(dest): |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
236 os.makedirs(dest) |
5ecb6507931b
fix vcs fetchers to almost follow a pattern
Jeff Hammel <jhammel@mozilla.com>
parents:
27
diff
changeset
|
237 call([self.git, 'clone', self.url, dest]) |
20 | 238 |
239 fetchers.append(GitFetcher) | |
17 | 240 |
16 | 241 __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
|
242 |
0 | 243 class Fetch(object): |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
244 |
37
f30fe9183e64
remove clobber functionality for now; this should live in the master Fetch class so that resources arent multiply clobbered
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
245 def __init__(self, fetchers=fetchers[:], relative_to=None, strict=True, clobber=True): |
15 | 246 self.fetchers = fetchers |
247 self.relative_to = relative_to | |
248 self.strict = strict | |
37
f30fe9183e64
remove clobber functionality for now; this should live in the master Fetch class so that resources arent multiply clobbered
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
249 self._clobber = clobber |
0 | 250 |
15 | 251 def fetcher(self, _type): |
252 """find the fetcher for the appropriate type""" | |
253 for fetcher in fetchers: | |
254 if fetcher.match(_type): | |
255 return fetcher | |
0 | 256 |
15 | 257 def __call__(self, url, destination, type, **options): |
258 fetcher = self.fetcher(type) | |
259 assert fetcher is not None, "No fetcher found for type '%s'" % type | |
260 fetcher = fetcher(url, **options) | |
261 fetcher(destination) | |
2 | 262 |
37
f30fe9183e64
remove clobber functionality for now; this should live in the master Fetch class so that resources arent multiply clobbered
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
263 def clobber(self, dest): |
f30fe9183e64
remove clobber functionality for now; this should live in the master Fetch class so that resources arent multiply clobbered
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
264 """clobbers if self._clobber is set""" |
f30fe9183e64
remove clobber functionality for now; this should live in the master Fetch class so that resources arent multiply clobbered
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
265 if self._clobber and os.path.exists(dest): |
f30fe9183e64
remove clobber functionality for now; this should live in the master Fetch class so that resources arent multiply clobbered
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
266 if os.path.isfile(dest): |
f30fe9183e64
remove clobber functionality for now; this should live in the master Fetch class so that resources arent multiply clobbered
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
267 os.remove(dest) |
f30fe9183e64
remove clobber functionality for now; this should live in the master Fetch class so that resources arent multiply clobbered
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
268 else: |
f30fe9183e64
remove clobber functionality for now; this should live in the master Fetch class so that resources arent multiply clobbered
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
269 shutil.rmtree(dest) |
f30fe9183e64
remove clobber functionality for now; this should live in the master Fetch class so that resources arent multiply clobbered
Jeff Hammel <jhammel@mozilla.com>
parents:
36
diff
changeset
|
270 |
15 | 271 def fetch(self, *items): |
2 | 272 |
15 | 273 if self.strict: |
274 # ensure all the required fetchers are available | |
275 types = set([i['type'] for i in items]) | |
276 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
|
277 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
|
278 if fetcher.match(i)]] |
4 | 279 |
15 | 280 for item in items: |
4 | 281 |
15 | 282 # fix up relative paths |
283 dest = item['dest'] | |
284 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
|
285 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
|
286 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
|
287 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
|
288 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
|
289 else: |
60e0e932570e
fix one error but not the one i was aiming for
Jeff Hammel <jhammel@mozilla.com>
parents:
29
diff
changeset
|
290 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
|
291 dest = os.path.normpath(os.path.join(relative_to, dest)) |
4 | 292 |
15 | 293 # fetch the items |
294 self(item['url'], destination=dest, type=item['type'], **item['options']) | |
0 | 295 |
21 | 296 |
0 | 297 format_string = "[URL] [destination] [type] <options>" |
298 def read_manifests(*manifests): | |
15 | 299 """ |
300 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
|
301 |
15 | 302 Format: |
303 %s | |
304 """ % format_string | |
0 | 305 |
15 | 306 retval = [] |
0 | 307 |
15 | 308 for manifest in manifests: |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
309 if isinstance(manifest, basestring): |
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
310 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
|
311 f = file(manifest) |
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
312 else: |
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
313 f = manifest |
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
314 |
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
315 for line in f.readlines(): |
15 | 316 line = line.strip() |
317 if line.startswith('#') or not line: | |
318 continue | |
319 line = line.split() | |
320 if len(line) not in (3,4): | |
321 raise Exception("Format should be: %s; line %s" % (format_string, line)) | |
322 options = {} | |
323 if len(line) == 4: | |
324 option_string = line.pop().rstrip(',') | |
325 try: | |
326 options = dict([[j.strip() for j in i.split('=', 1)] | |
327 for i in option_string.split(',')]) | |
328 except: | |
329 raise Exception("Options format should be: key=value,key2=value2,...; got %s" % option_string) | |
0 | 330 |
15 | 331 url, dest, _type = line |
332 retval.append(dict(url=url, dest=dest, type=_type, options=options, manifest=manifest)) | |
333 return retval | |
0 | 334 |
2 | 335 def main(args=sys.argv[1:]): |
0 | 336 |
15 | 337 # parse command line options |
338 usage = '%prog [options] manifest [manifest] [...]' | |
0 | 339 |
15 | 340 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): |
341 def format_description(self, description): | |
342 if description: | |
343 return description + '\n' | |
344 else: | |
345 return '' | |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
346 |
15 | 347 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) |
348 parser.add_option('-o', '--output', | |
349 help="output relative to this location vs. the manifest location") | |
17 | 350 parser.add_option('-d', '--dest', # XXX unused |
15 | 351 action='append', |
352 help="output only these destinations") | |
353 parser.add_option('-s', '--strict', | |
354 action='store_true', default=False, | |
355 help="fail on error") | |
356 parser.add_option('--list-fetchers', dest='list_fetchers', | |
357 action='store_true', default=False, | |
358 help='list available fetchers and exit') | |
359 options, args = parser.parse_args(args) | |
0 | 360 |
15 | 361 if options.list_fetchers: |
17 | 362 types = set() |
363 for fetcher in fetchers: | |
364 if fetcher.type in types: | |
365 continue # occluded, should probably display separately | |
366 print '%s : %s' % (fetcher.type, fetcher.doc()) | |
367 types.add(fetcher.type) | |
15 | 368 parser.exit() |
8
cf00d46b1bfb
pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents:
7
diff
changeset
|
369 |
15 | 370 if not args: |
17 | 371 # TODO: could read from stdin |
15 | 372 parser.print_help() |
373 parser.exit() | |
0 | 374 |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
375 # sanity check |
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
376 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
|
377 |
15 | 378 items = read_manifests(*args) |
32 | 379 fetch = Fetch(fetchers, relative_to=options.output, strict=options.strict) |
0 | 380 |
15 | 381 # download the files |
382 fetch.fetch(*items) | |
0 | 383 |
384 if __name__ == '__main__': | |
29
1c963875e6cd
add a test for manifest and fix resulting bugs
Jeff Hammel <jhammel@mozilla.com>
parents:
28
diff
changeset
|
385 main() |
0 | 386 |