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