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