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