annotate fetch.py @ 14:626b027134ea

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