comparison fetch.py @ 17:e2af4bc5159c

more polishing
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 09 Nov 2011 16:54:51 -0800
parents c77d29a10e08
children 64f89df1b966
comparison
equal deleted inserted replaced
16:c77d29a10e08 17:e2af4bc5159c
22 22
23 @classmethod 23 @classmethod
24 def match(cls, _type): 24 def match(cls, _type):
25 return _type == cls.type 25 return _type == cls.type
26 26
27 def __init__(self, url): 27 def __init__(self, url, clobber=False):
28 self.subpath = None
29 if '#' in url:
30 url, self.subpath = url.rsplit('#')
28 self.url = url 31 self.url = url
29 32
30 def __call__(self, dest): 33 def __call__(self, dest):
31 raise NotImplementedError 34 raise NotImplementedError("Should be called by implementing class")
35
36 @classmethod
37 def doc(cls):
38 """return docstring for the instance"""
39 retval = getattr(cls, '__doc__', '').strip()
40 return ' '.join(retval.split())
32 41
33 ### standard dispatchers - always available 42 ### standard dispatchers - always available
34 43
35 import tarfile 44 import tarfile
36 import urllib2 45 import urllib2
59 68
60 type = 'tar' 69 type = 'tar'
61 70
62 def __call__(self, dest): 71 def __call__(self, dest):
63 assert os.path.isdir(dest) 72 assert os.path.isdir(dest)
73 if self.subpath:
74 raise NotImplementedError("should extract only a subpath of a tarball but I haven't finished it yet")
64 buffer = StringIO() 75 buffer = StringIO()
65 buffer.write(self.download(self.url)) 76 buffer.write(self.download(self.url))
66 buffer.seek(0) 77 buffer.seek(0)
67 tf = tarfile.open(mode='r', fileobj=buffer) 78 tf = tarfile.open(mode='r', fileobj=buffer)
68 tf.extract(dest) 79 tf.extract(dest)
71 82
72 ### VCS fetchers using executable 83 ### VCS fetchers using executable
73 84
74 import subprocess 85 import subprocess
75 86
87 class VCSFetcher(Fetcher):
88 def __init__(self, url, export=True):
89 """
90 - export : whether to strip the versioning information
91 """
92 Fetcher.__init__(self, url)
93 self.export = export
94
76 if which('hg'): 95 if which('hg'):
77 96
78 class HgFetcher(Fetcher): 97 class HgFetcher(VCSFetcher):
79 """checkout a mercurial repository""" 98 """checkout a mercurial repository"""
80 type = 'hg' 99 type = 'hg'
81 100
82 def __call__(self, dest): 101 def __call__(self, dest):
83 if os.path.exits(dest): 102 if os.path.exits(dest):
84 assert os.path.isdir(dest) and os.path.exists(os.path.join(dest, '.hg')) 103 assert os.path.isdir(dest) and os.path.exists(os.path.join(dest, '.hg'))
85 pass # TODO 104 raise NotImplementedError("TODO! Sorry!")
86 105
87 fetchers.append(HgFetcher) 106 fetchers.append(HgFetcher)
88 107
89 if which('git'): 108 if which('git'):
109
90 class GitFetcher(Fetcher): 110 class GitFetcher(Fetcher):
91 """checkout a git repository""" 111 """checkout a git repository"""
92 type = 'git' 112 type = 'git'
113
114 fetchers
93 115
94 __all__ += [i.__name__ for i in fetchers] 116 __all__ += [i.__name__ for i in fetchers]
95 117
96 class Fetch(object): 118 class Fetch(object):
97 119
179 return '' 201 return ''
180 202
181 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) 203 parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter())
182 parser.add_option('-o', '--output', 204 parser.add_option('-o', '--output',
183 help="output relative to this location vs. the manifest location") 205 help="output relative to this location vs. the manifest location")
184 parser.add_option('-d', '--dest', 206 parser.add_option('-d', '--dest', # XXX unused
185 action='append', 207 action='append',
186 help="output only these destinations") 208 help="output only these destinations")
187 parser.add_option('-s', '--strict', 209 parser.add_option('-s', '--strict',
188 action='store_true', default=False, 210 action='store_true', default=False,
189 help="fail on error") 211 help="fail on error")
191 action='store_true', default=False, 213 action='store_true', default=False,
192 help='list available fetchers and exit') 214 help='list available fetchers and exit')
193 options, args = parser.parse_args(args) 215 options, args = parser.parse_args(args)
194 216
195 if options.list_fetchers: 217 if options.list_fetchers:
196 218 types = set()
197 for name in sorted(fetchers.keys()): 219 for fetcher in fetchers:
198 print name 220 if fetcher.type in types:
221 continue # occluded, should probably display separately
222 print '%s : %s' % (fetcher.type, fetcher.doc())
223 types.add(fetcher.type)
199 parser.exit() 224 parser.exit()
200 225
201 if not args: 226 if not args:
227 # TODO: could read from stdin
202 parser.print_help() 228 parser.print_help()
203 parser.exit() 229 parser.exit()
204 230
205 items = read_manifests(*args) 231 items = read_manifests(*args)
206 fetch = Fetch(fetchers, strict=options.strict) 232 fetch = Fetch(fetchers, strict=options.strict)