Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: cms/sources.py

Issue 5488197752586240: Issue 2440 - implement Source.get_cache_dir() to be used by custom filters/globals (Closed)
Patch Set: Created April 30, 2015, 1:49 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cms/converters.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH 4 # Copyright (C) 2006-2015 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 class MercurialSource(Source): 204 class MercurialSource(Source):
205 def __init__(self, repo): 205 def __init__(self, repo):
206 command = ["hg", "-R", repo, "archive", "-r", "default", 206 command = ["hg", "-R", repo, "archive", "-r", "default",
207 "-t", "uzip", "-p", ".", "-"] 207 "-t", "uzip", "-p", ".", "-"]
208 data = subprocess.check_output(command) 208 data = subprocess.check_output(command)
209 self._archive = zipfile.ZipFile(StringIO(data), mode="r") 209 self._archive = zipfile.ZipFile(StringIO(data), mode="r")
210 210
211 command = ["hg", "-R", repo, "id", "-n", "-r", "default"] 211 command = ["hg", "-R", repo, "id", "-n", "-r", "default"]
212 self.version = subprocess.check_output(command).strip() 212 self.version = subprocess.check_output(command).strip()
213 213
214 self._origin = urlparse.urlsplit(repo).path.strip('/')
215
214 def __enter__(self): 216 def __enter__(self):
215 return self 217 return self
216 218
217 def __exit__(self, type, value, traceback): 219 def __exit__(self, type, value, traceback):
218 self.close() 220 self.close()
219 return False 221 return False
220 222
221 def close(self): 223 def close(self):
222 self._archive.close() 224 self._archive.close()
223 225
224 def has_file(self, filename): 226 def has_file(self, filename):
225 try: 227 try:
226 self._archive.getinfo("./%s" % filename) 228 self._archive.getinfo("./%s" % filename)
227 except KeyError: 229 except KeyError:
228 return False 230 return False
229 return True 231 return True
230 232
231 def read_file(self, filename, binary=False): 233 def read_file(self, filename, binary=False):
232 result = self._archive.read("./%s" % filename) 234 result = self._archive.read("./%s" % filename)
233 if not binary: 235 if not binary:
234 result = result.decode("utf-8") 236 result = result.decode("utf-8")
235 return result 237 return result
236 238
237 def list_files(self, subdir): 239 def list_files(self, subdir):
238 prefix = "./%s/" % subdir 240 prefix = "./%s/" % subdir
239 for filename in self._archive.namelist(): 241 for filename in self._archive.namelist():
240 if filename.startswith(prefix): 242 if filename.startswith(prefix):
241 yield filename[len(prefix):] 243 yield filename[len(prefix):]
242 244
245 if os.name == "posix":
246 def get_cache_dir(self):
247 return "/var/cache/" + self._origin
248
243 class FileSource(Source): 249 class FileSource(Source):
244 def __init__(self, dir): 250 def __init__(self, dir):
245 self._dir = dir 251 self._dir = dir
246 252
247 def __enter__(self): 253 def __enter__(self):
248 return self 254 return self
249 255
250 def __exit__(self, type, value, traceback): 256 def __exit__(self, type, value, traceback):
251 return False 257 return False
252 258
(...skipping 20 matching lines...) Expand all
273 return 279 return
274 280
275 for filename in files: 281 for filename in files:
276 path = os.path.join(dir, filename) 282 path = os.path.join(dir, filename)
277 if os.path.isfile(path): 283 if os.path.isfile(path):
278 result.append(relpath + filename) 284 result.append(relpath + filename)
279 elif os.path.isdir(path): 285 elif os.path.isdir(path):
280 do_list(path, relpath + filename + "/") 286 do_list(path, relpath + filename + "/")
281 do_list(self.get_path(subdir), "") 287 do_list(self.get_path(subdir), "")
282 return result 288 return result
289
290 def get_cache_dir(self):
291 return os.path.join(self._dir, "cache")
OLDNEW
« no previous file with comments | « cms/converters.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld