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