comparison fetch.py @ 28:5ecb6507931b

fix vcs fetchers to almost follow a pattern
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 14 Nov 2011 22:12:49 -0800
parents 423b67ff4512
children 1c963875e6cd
comparison
equal deleted inserted replaced
27:423b67ff4512 28:5ecb6507931b
81 """fetch and extract a tarball""" 81 """fetch and extract a tarball"""
82 82
83 type = 'tar' 83 type = 'tar'
84 84
85 def __call__(self, dest): 85 def __call__(self, dest):
86 if self.clobber: 86 self.clobber()
87 shutil.rmtree(dest)
88 if os.path.exists(dest): 87 if os.path.exists(dest):
89 assert os.path.isdir(dest), "Destination must be a directory" 88 assert os.path.isdir(dest), "Destination must be a directory"
90 else: 89 else:
91 os.mkdirs(dest) 90 os.mkdirs(dest)
92 if self.subpath: 91 if self.subpath:
119 """ 118 """
120 - export : whether to strip the versioning information 119 - export : whether to strip the versioning information
121 """ 120 """
122 Fetcher.__init__(self, url) 121 Fetcher.__init__(self, url)
123 self.export = export 122 self.export = export
123 self.prog = self.type # name of app program
124 self.vcs_dir = '.' + self.type # subdirectory for version control
124 125
125 def __call__(self, dest): 126 def __call__(self, dest):
126 127
128 self.clobber(dest)
127 if self.subpath or self.export: 129 if self.subpath or self.export:
128 # can only export with a subpath 130 # can only export with a subpath
129 self.export(dest, subpath=self.subpath) 131 self.export(dest, subpath=self.subpath)
130 return 132 return
131 133
132 if os.path.exists(dest): 134 if os.path.exists(dest):
133 assert os.path.isdir(dest) 135 assert os.path.isdir(dest)
136 else:
134 self.clone(dest) 137 self.clone(dest)
135 138
136 def export(self, dest): 139 def export(self, dest):
137 """ 140 """
138 export a clone of the directory 141 export a clone of the directory
170 def update(self, dest): 173 def update(self, dest):
171 """ 174 """
172 updates a checkout 175 updates a checkout
173 """ 176 """
174 raise NotImplementedError("Abstract base class") 177 raise NotImplementedError("Abstract base class")
175 178
176 179 def versioned(self, directory):
180 return os.path.exists(os.path.join(directory, self.vcs_dir))
181
182
177 if which('hg'): 183 if which('hg'):
178 184
179 class HgFetcher(VCSFetcher): 185 class HgFetcher(VCSFetcher):
180 """checkout a mercurial repository""" 186 """checkout a mercurial repository"""
181 type = 'hg' 187 type = 'hg'
182 188
183 def __init__(self, url, export=True): 189 def __init__(self, url, export=True):
184 VCSFetcher.__init__(self, url, export=True) 190 VCSFetcher.__init__(self, url, export=export)
185 self.hg = which('hg') 191 self.hg = which('hg')
186 assert self.hg, "'hg' command not found" 192 assert self.hg, "'hg' command not found"
187 193
188 def __call__(self, dest): 194 def clone(self, dest):
189 if os.path.exists(dest): 195 if os.path.exists(dest):
190 assert os.path.isdir(dest) and os.path.exists(os.path.join(dest, '.hg')) 196 assert os.path.isdir(dest)
191 call([self.hg, 'pull', self.url], cwd=dest) 197 call([self.hg, 'clone', self.url, dest])
192 call([self.hg, 'update'], cwd=dest) 198
193 else: 199 def update(self, dest):
194 if not os.path.exists(dest): 200 assert os.path.versioned(dest)
195 os.mkdirs(dest) 201 assert os.path.exists(dest)
196 call([self.hg, 'clone', self.url, dest]) 202 call([self.hg, 'pull', self.url], cwd=dest)
203 call([self.hg, 'update', '-C'], cwd=dest)
204
197 205
198 fetchers.append(HgFetcher) 206 fetchers.append(HgFetcher)
199
200 207
201 if which('git'): 208 if which('git'):
202 209
203 class GitFetcher(Fetcher): 210 class GitFetcher(Fetcher):
204 """checkout a git repository""" 211 """checkout a git repository"""
205 type = 'git' 212 type = 'git'
206 213
207 def __init__(self, url, export=True): 214 def __init__(self, url, export=True):
208 VCSFetcher.__init__(self, url, export=True) 215 VCSFetcher.__init__(self, url, export=export)
209 self.git = which('git') 216 self.git = which('git')
210 217 assert self.git, "'git' command not found"
211 def __call__(self, dest): 218
212 if os.path.exists(dest): 219 def update(self, dest):
213 assert os.path.isdir(dest) and os.path.exists(os.path.join(dest, '.git')) 220 assert os.path.isdir(dest)
214 call([self.git, 'pull', self.url], cwd=dest) 221 assert os.path.versioned(dest)
215 call([self.hg, 'update'], cwd=dest) 222 call([self.git, 'pull', self.url], cwd=dest)
216 else: 223 call([self.git, 'update'], cwd=dest)
217 if not os.path.exists(dest): 224
218 os.mkdirs(dest) 225 def clone(self, dest):
219 call([self.hg, 'clone', self.url, dest]) 226 if not os.path.exists(dest):
220 227 os.makedirs(dest)
228 call([self.git, 'clone', self.url, dest])
221 229
222 fetchers.append(GitFetcher) 230 fetchers.append(GitFetcher)
223 231
224 __all__ += [i.__name__ for i in fetchers] 232 __all__ += [i.__name__ for i in fetchers]
225 233