annotate fetch.py @ 15:bc7d6763357e

clean up spacing
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 09 Nov 2011 16:29:58 -0800
parents 3fee8ecd1af8
children c77d29a10e08
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
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
27 def __init__(self, url):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
28 self.url = url
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
30 def __call__(self, dest):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
31 raise NotImplementedError
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32
7
4d3d0e1324f5 flush out tarfile
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
33 ### standard dispatchers - always available
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34
7
4d3d0e1324f5 flush out tarfile
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
35 import tarfile
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 import urllib2
7
4d3d0e1324f5 flush out tarfile
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
37 from StringIO import StringIO
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38
5
5b78d1411cdd typo; this now works
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
39 class FileFetcher(Fetcher):
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
40 """fetch a single file"""
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
42 type = 'file'
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
44 @classmethod
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
45 def download(cls, url):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
46 return urllib2.urlopen(url).read()
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
48 def __call__(self, dest):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
49 if os.path.isdir(dest):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
50 filename = self.url.rsplit('/', 1)[-1]
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
51 dest = os.path.join(dest, filename)
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
52 f = file(dest, 'w')
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
53 f.write(self.download(self.url))
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
54 f.close()
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55
6
86f6f99e421b add types for unimplemented dispatchers
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
56
5
5b78d1411cdd typo; this now works
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
57 class TarballFetcher(FileFetcher):
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
58 """fetch and extract a tarball"""
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
60 type = 'tar'
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
62 def __call__(self, dest):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
63 assert os.path.isdir(dest)
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
64 buffer = StringIO()
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
65 buffer.write(self.download(self.url))
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
66 buffer.seek(0)
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
67 tf = tarfile.open(mode='r', fileobj=buffer)
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
68 tf.extract(dest)
7
4d3d0e1324f5 flush out tarfile
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
69
8
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
70 fetchers = [FileFetcher, TarballFetcher]
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
71
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
72 ### 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
73
11
726c3d288733 * add convenience import in __init__
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
74 import subprocess
726c3d288733 * add convenience import in __init__
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
75
8
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
76 if which('hg'):
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
77
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
78 class HgFetcher(Fetcher):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
79 """checkout a mercurial repository"""
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
80 type = 'hg'
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
81
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
82 def __call__(self, dest):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
83 if os.path.exits(dest):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
84 assert os.path.isdir(dest) and os.path.exists(os.path.join(dest, '.hg'))
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
85 pass # TODO
11
726c3d288733 * add convenience import in __init__
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
86
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
87 fetchers.append(HgFetcher)
6
86f6f99e421b add types for unimplemented dispatchers
Jeff Hammel <jhammel@mozilla.com>
parents: 5
diff changeset
88
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
89 if which('git'):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
90 class GitFetcher(Fetcher):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
91 """checkout a git repository"""
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
92 type = 'git'
8
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
93
10
0b534c8881de make fetchers a dict keyed on class name
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
94 fetchers = dict([(i.__name__, i) for i in fetchers])
0b534c8881de make fetchers a dict keyed on class name
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
95 __all__ += fetchers.keys()
8
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
96
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
97 class Fetch(object):
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
98
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
99 def __init__(self, fetchers, relative_to=None, strict=True):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
100 self.fetchers = fetchers
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
101 self.relative_to = relative_to
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
102 self.strict = strict
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
103
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
104 def fetcher(self, _type):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
105 """find the fetcher for the appropriate type"""
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
106 for fetcher in fetchers:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
107 if fetcher.match(_type):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
108 return fetcher
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
109
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
110 def __call__(self, url, destination, type, **options):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
111 fetcher = self.fetcher(type)
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
112 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
113 fetcher = fetcher(url, **options)
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
114 fetcher(destination)
2
0c344ed749ac fix typos
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
115
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
116 def fetch(self, *items):
2
0c344ed749ac fix typos
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
117
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
118 if self.strict:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
119 # ensure all the required fetchers are available
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
120 types = set([i['type'] for i in items])
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
121 assert not [i for i in types
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
122 if [True for fetcher in fetchers if fetcher.match(i)]]
4
a984091d681a fix up destination
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
123
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
124 for item in items:
4
a984091d681a fix up destination
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
125
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
126 # fix up relative paths
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
127 dest = item['dest']
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
128 if not os.path.isabs(dest):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
129 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
130 dest = os.path.join(relative_to, dest)
4
a984091d681a fix up destination
Jeff Hammel <jhammel@mozilla.com>
parents: 3
diff changeset
131
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
132 # fetch the items
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
133 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
134
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
135 format_string = "[URL] [destination] [type] <options>"
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
136 def read_manifests(*manifests):
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
137 """
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
138 read some manifests and return the items
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
139
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
140 Format:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
141 %s
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
142 """ % format_string
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
143
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
144 # sanity check
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
145 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
146
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
147 retval = []
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
148
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
149 for manifest in manifests:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
150 for line in file(i).readlines():
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
151 line = line.strip()
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
152 if line.startswith('#') or not line:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
153 continue
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
154 line = line.split()
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
155 if len(line) not in (3,4):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
156 raise Exception("Format should be: %s; line %s" % (format_string, line))
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
157 options = {}
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
158 if len(line) == 4:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
159 option_string = line.pop().rstrip(',')
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
160 try:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
161 options = dict([[j.strip() for j in i.split('=', 1)]
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
162 for i in option_string.split(',')])
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
163 except:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
164 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
165
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
166 url, dest, _type = line
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
167 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
168 return retval
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
169
2
0c344ed749ac fix typos
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
170 def main(args=sys.argv[1:]):
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
171
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
172 # parse command line options
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
173 usage = '%prog [options] manifest [manifest] [...]'
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
174
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
175 class PlainDescriptionFormatter(optparse.IndentedHelpFormatter):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
176 def format_description(self, description):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
177 if description:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
178 return description + '\n'
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
179 else:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
180 return ''
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
181
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
182 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter())
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
183 parser.add_option('-o', '--output',
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
184 help="output relative to this location vs. the manifest location")
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
185 parser.add_option('-d', '--dest',
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
186 action='append',
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
187 help="output only these destinations")
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
188 parser.add_option('-s', '--strict',
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
189 action='store_true', default=False,
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
190 help="fail on error")
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
191 parser.add_option('--list-fetchers', dest='list_fetchers',
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
192 action='store_true', default=False,
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
193 help='list available fetchers and exit')
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
194 options, args = parser.parse_args(args)
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
195
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
196 if options.list_fetchers:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
197 for name in sorted(fetchers.keys()):
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
198 print name
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
199 parser.exit()
8
cf00d46b1bfb pretend like we have a pluggable system to start debugging it
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
200
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
201 if not args:
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
202 parser.print_help()
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
203 parser.exit()
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 items = read_manifests(*args)
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
206 fetch = Fetch(fetchers.values(), strict=options.strict)
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
207
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
208 # download the files
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
209 fetch.fetch(*items)
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
210
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
211 if __name__ == '__main__':
15
bc7d6763357e clean up spacing
Jeff Hammel <jhammel@mozilla.com>
parents: 13
diff changeset
212 main()
0
3497a30190d2 initial commit of fetch, WIP
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
213