annotate fetch.py @ 21:0706968f01bb

stub testing
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 09 Nov 2011 19:59:04 -0800
parents 738d84b4de52
children 12ad9ab11860
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 fetch stuff from the interwebs
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 """
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 import os
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import sys
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 import optparse
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10
8
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
11 __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
12
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
13 def which(executable, path=os.environ['PATH']):
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
14 """python equivalent of which; should really be in the stdlib"""
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
15 dirs = path.split(os.pathsep)
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
16 for dir in dirs:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
17 if os.path.isfile(os.path.join(dir, executable)):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
18 return os.path.join(dir, executable)
7
4d3d0e1324f5 flush out tarfile
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
19
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 class Fetcher(object):
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
21 """abstract base class for resource fetchers"""
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
23 @classmethod
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
24 def match(cls, _type):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
25 return _type == cls.type
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26
17
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
27 def __init__(self, url, clobber=False):
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
28 self.subpath = None
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
29 if '#' in url:
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
30 url, self.subpath = url.rsplit('#')
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
31 self.url = url
18
64f89df1b966 comment
Jeff Hammel <jhammel@mozilla.com>
parents: 17
diff changeset
32 # self.clobber = clobber # unused
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
34 def __call__(self, dest):
17
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
35 raise NotImplementedError("Should be called by implementing class")
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
36
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
37 @classmethod
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
38 def doc(cls):
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
39 """return docstring for the instance"""
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
40 retval = getattr(cls, '__doc__', '').strip()
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
41 return ' '.join(retval.split())
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42
7
4d3d0e1324f5 flush out tarfile
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
43 ### standard dispatchers - always available
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44
7
4d3d0e1324f5 flush out tarfile
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
45 import tarfile
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 import urllib2
7
4d3d0e1324f5 flush out tarfile
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
47 from StringIO import StringIO
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48
5
5b78d1411cdd typo; this now works
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
49 class FileFetcher(Fetcher):
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
50 """fetch a single file"""
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
52 type = 'file'
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
54 @classmethod
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
55 def download(cls, url):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
56 return urllib2.urlopen(url).read()
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
58 def __call__(self, dest):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
59 if os.path.isdir(dest):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
60 filename = self.url.rsplit('/', 1)[-1]
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
61 dest = os.path.join(dest, filename)
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
62 f = file(dest, 'w')
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
63 f.write(self.download(self.url))
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
64 f.close()
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65
6
86f6f99e421b add types for unimplemented dispatchers
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
66
5
5b78d1411cdd typo; this now works
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
67 class TarballFetcher(FileFetcher):
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
68 """fetch and extract a tarball"""
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
70 type = 'tar'
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
72 def __call__(self, dest):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
73 assert os.path.isdir(dest)
17
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
74 if self.subpath:
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
75 raise NotImplementedError("should extract only a subpath of a tarball but I haven't finished it yet")
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
76 buffer = StringIO()
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
77 buffer.write(self.download(self.url))
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
78 buffer.seek(0)
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
79 tf = tarfile.open(mode='r', fileobj=buffer)
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
80 tf.extract(dest)
7
4d3d0e1324f5 flush out tarfile
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
81
8
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
82 fetchers = [FileFetcher, TarballFetcher]
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
83
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
84 ### VCS fetchers using executable
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
85
11
726c3d288733 * add convenience import in __init__
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
86 import subprocess
19
d69041957c0e more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
87 try:
d69041957c0e more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
88 from subprocess import check_call as call
d69041957c0e more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
89 except ImportErorr:
d69041957c0e more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
90 raise # we need check_call, kinda
11
726c3d288733 * add convenience import in __init__
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
91
17
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
92 class VCSFetcher(Fetcher):
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
93 def __init__(self, url, export=True):
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
94 """
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
95 - export : whether to strip the versioning information
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
96 """
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
97 Fetcher.__init__(self, url)
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
98 self.export = export
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
99
8
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
100 if which('hg'):
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
101
17
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
102 class HgFetcher(VCSFetcher):
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
103 """checkout a mercurial repository"""
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
104 type = 'hg'
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
105
19
d69041957c0e more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
106 def __init__(self, url, export=True):
d69041957c0e more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
107 VCSFetcher.__init__(self, url, export=True)
d69041957c0e more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
108 self.hg = which('hg')
d69041957c0e more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
109
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
110 def __call__(self, dest):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
111 if os.path.exits(dest):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
112 assert os.path.isdir(dest) and os.path.exists(os.path.join(dest, '.hg'))
19
d69041957c0e more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
113 call([self.hg, 'pull', self.url], cwd=dest)
d69041957c0e more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
114 call([self.hg, 'update'], cwd=dest)
d69041957c0e more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
115 else:
d69041957c0e more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
116 pass
17
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
117 raise NotImplementedError("TODO! Sorry!")
11
726c3d288733 * add convenience import in __init__
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
118
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
119 fetchers.append(HgFetcher)
6
86f6f99e421b add types for unimplemented dispatchers
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
120
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
121 if which('git'):
17
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
122
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
123 class GitFetcher(Fetcher):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
124 """checkout a git repository"""
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
125 type = 'git'
8
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
126
20
738d84b4de52 start documenting
Jeff Hammel <jhammel@mozilla.com>
parents: 19
diff changeset
127 def __init__(self, url, export=True):
738d84b4de52 start documenting
Jeff Hammel <jhammel@mozilla.com>
parents: 19
diff changeset
128 VCSFetcher.__init__(self, url, export=True)
738d84b4de52 start documenting
Jeff Hammel <jhammel@mozilla.com>
parents: 19
diff changeset
129 self.hg = which('git')
738d84b4de52 start documenting
Jeff Hammel <jhammel@mozilla.com>
parents: 19
diff changeset
130
738d84b4de52 start documenting
Jeff Hammel <jhammel@mozilla.com>
parents: 19
diff changeset
131 fetchers.append(GitFetcher)
17
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
132
16
c77d29a10e08 get it working
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
133 __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
134
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
135 class Fetch(object):
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
136
21
0706968f01bb stub testing
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
137 def __init__(self, fetchers=fetchers[:], relative_to=None, strict=True):
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
138 self.fetchers = fetchers
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
139 self.relative_to = relative_to
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
140 self.strict = strict
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
141
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
142 def fetcher(self, _type):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
143 """find the fetcher for the appropriate type"""
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
144 for fetcher in fetchers:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
145 if fetcher.match(_type):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
146 return fetcher
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
147
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
148 def __call__(self, url, destination, type, **options):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
149 fetcher = self.fetcher(type)
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
150 assert fetcher is not None, "No fetcher found for type '%s'" % type
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
151 fetcher = fetcher(url, **options)
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
152 fetcher(destination)
2
0c344ed749ac fix typos
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
153
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
154 def fetch(self, *items):
2
0c344ed749ac fix typos
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
155
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
156 if self.strict:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
157 # ensure all the required fetchers are available
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
158 types = set([i['type'] for i in items])
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
159 assert not [i for i in types
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
160 if [True for fetcher in fetchers if fetcher.match(i)]]
4
a984091d681a fix up destination
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
161
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
162 for item in items:
4
a984091d681a fix up destination
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
163
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
164 # fix up relative paths
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
165 dest = item['dest']
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
166 if not os.path.isabs(dest):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
167 relative_to = self.relative_to or os.path.dirname(os.path.abspath(item['manifest']))
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
168 dest = os.path.join(relative_to, dest)
4
a984091d681a fix up destination
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
169
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
170 # fetch the items
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
171 self(item['url'], destination=dest, type=item['type'], **item['options'])
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
172
21
0706968f01bb stub testing
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
173
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
174 format_string = "[URL] [destination] [type] <options>"
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
175 def read_manifests(*manifests):
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
176 """
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
177 read some manifests and return the items
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
178
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
179 Format:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
180 %s
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
181 """ % format_string
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
182
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
183 # sanity check
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
184 assert not [i for i in manifests if not os.path.exists(i)]
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
185
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
186 retval = []
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
187
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
188 for manifest in manifests:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
189 for line in file(i).readlines():
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
190 line = line.strip()
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
191 if line.startswith('#') or not line:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
192 continue
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
193 line = line.split()
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
194 if len(line) not in (3,4):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
195 raise Exception("Format should be: %s; line %s" % (format_string, line))
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
196 options = {}
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
197 if len(line) == 4:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
198 option_string = line.pop().rstrip(',')
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
199 try:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
200 options = dict([[j.strip() for j in i.split('=', 1)]
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
201 for i in option_string.split(',')])
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
202 except:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
203 raise Exception("Options format should be: key=value,key2=value2,...; got %s" % option_string)
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
204
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
205 url, dest, _type = line
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
206 retval.append(dict(url=url, dest=dest, type=_type, options=options, manifest=manifest))
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
207 return retval
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
208
2
0c344ed749ac fix typos
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
209 def main(args=sys.argv[1:]):
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
210
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
211 # parse command line options
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
212 usage = '%prog [options] manifest [manifest] [...]'
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
213
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
214 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
215 def format_description(self, description):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
216 if description:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
217 return description + '\n'
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
218 else:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
219 return ''
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
220
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
221 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter())
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
222 parser.add_option('-o', '--output',
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
223 help="output relative to this location vs. the manifest location")
17
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
224 parser.add_option('-d', '--dest', # XXX unused
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
225 action='append',
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
226 help="output only these destinations")
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
227 parser.add_option('-s', '--strict',
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
228 action='store_true', default=False,
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
229 help="fail on error")
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
230 parser.add_option('--list-fetchers', dest='list_fetchers',
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
231 action='store_true', default=False,
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
232 help='list available fetchers and exit')
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
233 options, args = parser.parse_args(args)
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
234
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
235 if options.list_fetchers:
17
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
236 types = set()
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
237 for fetcher in fetchers:
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
238 if fetcher.type in types:
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
239 continue # occluded, should probably display separately
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
240 print '%s : %s' % (fetcher.type, fetcher.doc())
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
241 types.add(fetcher.type)
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
242 parser.exit()
8
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
243
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
244 if not args:
17
e2af4bc5159c more polishing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
245 # TODO: could read from stdin
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
246 parser.print_help()
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
247 parser.exit()
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
248
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
249 items = read_manifests(*args)
16
c77d29a10e08 get it working
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
250 fetch = Fetch(fetchers, strict=options.strict)
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
251
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
252 # download the files
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
253 fetch.fetch(*items)
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
254
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
255 if __name__ == '__main__':
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
256 main()
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
257