comparison fetch.py @ 26:d495b610046a

more stubbing; tests work again
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 14 Nov 2011 21:25:56 -0800
parents e628ce3ae49f
children 423b67ff4512
comparison
equal deleted inserted replaced
25:e628ce3ae49f 26:d495b610046a
30 if '#' in url: 30 if '#' in url:
31 url, self.subpath = url.rsplit('#') 31 url, self.subpath = url.rsplit('#')
32 if self.subpath: 32 if self.subpath:
33 self.subpath = self.subpath.split('/') 33 self.subpath = self.subpath.split('/')
34 self.url = url 34 self.url = url
35 self.clobber = clobber 35 self._clobber = clobber
36 36
37 def __call__(self, dest): 37 def __call__(self, dest):
38 raise NotImplementedError("Should be called by implementing class") 38 raise NotImplementedError("Should be called by implementing class")
39
40 def clobber(self, dest):
41 """clobbers if self._clobber is set"""
42 if self._clobber and os.path.exists(dest):
43 if os.path.isfile(dest):
44 os.remove(dest)
45 else:
46 shutil.rmtree(dest)
39 47
40 @classmethod 48 @classmethod
41 def doc(cls): 49 def doc(cls):
42 """return docstring for the instance""" 50 """return docstring for the instance"""
43 retval = getattr(cls, '__doc__', '').strip() 51 retval = getattr(cls, '__doc__', '').strip()
49 import urllib2 57 import urllib2
50 from StringIO import StringIO 58 from StringIO import StringIO
51 59
52 class FileFetcher(Fetcher): 60 class FileFetcher(Fetcher):
53 """fetch a single file""" 61 """fetch a single file"""
54 # Note: subpath for files is ignored 62 # Note: subpath and clobber for single files are ignored
55 63
56 type = 'file' 64 type = 'file'
57 65
58 @classmethod 66 @classmethod
59 def download(cls, url): 67 def download(cls, url):
73 """fetch and extract a tarball""" 81 """fetch and extract a tarball"""
74 82
75 type = 'tar' 83 type = 'tar'
76 84
77 def __call__(self, dest): 85 def __call__(self, dest):
86 if self.clobber:
87 shutil.rmtree(dest)
78 if os.path.exists(dest): 88 if os.path.exists(dest):
79 assert os.path.isdir(dest) 89 assert os.path.isdir(dest), "Destination must be a directory"
80 else: 90 else:
81 os.mkdirs(dest) 91 os.mkdirs(dest)
82 if self.subpath: 92 if self.subpath:
83 raise NotImplementedError("should extract only a subpath of a tarball but I haven't finished it yet") 93 raise NotImplementedError("should extract only a subpath of a tarball but I haven't finished it yet")
84 buffer = StringIO() 94 buffer = StringIO()
114 124
115 def __call__(self, dest): 125 def __call__(self, dest):
116 126
117 if self.subpath or self.export: 127 if self.subpath or self.export:
118 # can only export with a subpath 128 # can only export with a subpath
129 self.export(dest, subpath=self.subpath)
130 return
119 131
120
121 if os.path.exists(dest): 132 if os.path.exists(dest):
122 assert os.path.isdir(dest) 133 assert os.path.isdir(dest)
134 self.clone(dest)
123 135
124 def export(self, dest): 136 def export(self, dest):
125 """ 137 """
126 export a clone of the directory 138 export a clone of the directory
127 """ 139 """
140 dest = os.path.abspath(dest)
128 tmpdir = tempfile.mkdtmp() 141 tmpdir = tempfile.mkdtmp()
129 self.clone(tmpdir) 142 self.clone(tmpdir)
130 143 path = tmpdir
144 if self.subpath:
145 path = os.path.join([tmpdir] + self.subpath)
146 assert os.path.exists(path), "subpath %s of %s not found" % (os.path.sep.join(self.subpath), self.url)
147 self.clobber(dest)
148 if os.path.isdir(path):
149 if os.path.exists(dest):
150 assert os.path.isdir(dest), "source is a directory; destination is a file"
151 else:
152 os.makedirs(dest)
153 else:
154 if os.path.exists(dest):
155 assert os.path.isfile(dest), ""
156 else:
157 directory, filename = os.path.split(dest)
131 shutil.rmtree(tmpdir) 158 shutil.rmtree(tmpdir)
132 159
133 def clone(self, dest): 160 def clone(self, dest):
134 """ 161 """
135 clones into a directory 162 clones into a directory
136 """ 163 """
137 raise NotImplementedError("Abstract base class") 164 raise NotImplementedError("Abstract base class")
138 165
166 def update(self, dest):
167 """
168 updates a checkout
169 """
170 raise NotImplementedError("Abstract base class")
171
139 172
140 if which('hg'): 173 if which('hg'):
141 174
142 class HgFetcher(VCSFetcher): 175 class HgFetcher(VCSFetcher):
143 """checkout a mercurial repository""" 176 """checkout a mercurial repository"""