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

Side by Side Diff: cms/sources.py

Issue 29347181: Issue 4217 - Fix Prefix Regression Due To Mercurial Version Behavior Change (Closed)
Patch Set: Created July 1, 2016, 11:04 a.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 | « no previous file | 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 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-2016 Eyeo GmbH 2 # Copyright (C) 2006-2016 Eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 elif self.has_page(alternative_page): 51 elif self.has_page(alternative_page):
52 if not self.has_locale(locale, alternative_page): 52 if not self.has_locale(locale, alternative_page):
53 locale = default_locale 53 locale = default_locale
54 else: 54 else:
55 logging.warning('Link to %s cannot be resolved', page) 55 logging.warning('Link to %s cannot be resolved', page)
56 56
57 parts = page.split('/') 57 parts = page.split('/')
58 if parts[-1] == default_page: 58 if parts[-1] == default_page:
59 page = '/'.join(parts[:-1]) 59 page = '/'.join(parts[:-1])
60 60
61 path = '/%s/%s' % (locale, page) 61 path = '/{}/{}'.format(locale, page)
Sebastian Noack 2016/07/01 11:18:47 Replacing legacy string formatting (using the % op
62 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:]) 62 return locale, urlparse.urlunparse(parsed[0:2] + (path,) + parsed[3:])
63 63
64 def read_config(self): 64 def read_config(self):
65 configdata = self.read_file('settings.ini')[0] 65 configdata = self.read_file('settings.ini')[0]
66 config = ConfigParser.SafeConfigParser() 66 config = ConfigParser.SafeConfigParser()
67 config.readfp(StringIO(configdata)) 67 config.readfp(StringIO(configdata))
68 return config 68 return config
69 69
70 def exec_file(self, filename): 70 def exec_file(self, filename):
71 source, filename = self.read_file(filename) 71 source, filename = self.read_file(filename)
72 code = compile(source, filename, 'exec') 72 code = compile(source, filename, 'exec')
73 namespace = {} 73 namespace = {}
74 exec code in namespace 74 exec code in namespace
75 return namespace 75 return namespace
76 76
77 # 77 #
78 # Page helpers 78 # Page helpers
79 # 79 #
80 80
81 @staticmethod 81 @staticmethod
82 def page_filename(page, format): 82 def page_filename(page, format):
83 return 'pages/%s.%s' % (page, format) 83 return 'pages/{}.{}'.format(page, format)
84 84
85 def list_pages(self): 85 def list_pages(self):
86 for filename in self.list_files('pages'): 86 for filename in self.list_files('pages'):
87 root, ext = os.path.splitext(filename) 87 root, ext = os.path.splitext(filename)
88 format = ext[1:].lower() 88 format = ext[1:].lower()
89 yield root, format 89 yield root, format
90 90
91 def has_page(self, page, format=None): 91 def has_page(self, page, format=None):
92 if format is None: 92 if format is None:
93 from cms.converters import converters 93 from cms.converters import converters
94 return any( 94 return any(
95 self.has_page(page, format) 95 self.has_page(page, format)
96 for format in converters.iterkeys() 96 for format in converters.iterkeys()
97 ) 97 )
98 else: 98 else:
99 return self.has_file(self.page_filename(page, format)) 99 return self.has_file(self.page_filename(page, format))
100 100
101 def read_page(self, page, format): 101 def read_page(self, page, format):
102 return self.read_file(self.page_filename(page, format)) 102 return self.read_file(self.page_filename(page, format))
103 103
104 # 104 #
105 # Localizable files helpers 105 # Localizable files helpers
106 # 106 #
107 107
108 @staticmethod 108 @staticmethod
109 def localizable_file_filename(locale, filename): 109 def localizable_file_filename(locale, filename):
110 return 'locales/%s/%s' % (locale, filename) 110 return 'locales/{}/{}'.format(locale, filename)
111 111
112 def list_localizable_files(self): 112 def list_localizable_files(self):
113 default_locale = self.read_config().get('general', 'defaultlocale') 113 default_locale = self.read_config().get('general', 'defaultlocale')
114 return filter( 114 return filter(
115 lambda f: os.path.splitext(f)[1].lower() != '.json', 115 lambda f: os.path.splitext(f)[1].lower() != '.json',
116 self.list_files('locales/%s' % default_locale) 116 self.list_files('locales/{}'.format(default_locale))
117 ) 117 )
118 118
119 def has_localizable_file(self, locale, filename): 119 def has_localizable_file(self, locale, filename):
120 return self.has_file(self.localizable_file_filename(locale, filename)) 120 return self.has_file(self.localizable_file_filename(locale, filename))
121 121
122 def read_localizable_file(self, locale, filename): 122 def read_localizable_file(self, locale, filename):
123 return self.read_file(self.localizable_file_filename(locale, filename), binary=True)[0] 123 return self.read_file(self.localizable_file_filename(locale, filename), binary=True)[0]
124 124
125 # 125 #
126 # Static file helpers 126 # Static file helpers
127 # 127 #
128 128
129 @staticmethod 129 @staticmethod
130 def static_filename(filename): 130 def static_filename(filename):
131 return 'static/%s' % filename 131 return 'static/{}'.format(filename)
132 132
133 def list_static(self): 133 def list_static(self):
134 return self.list_files('static') 134 return self.list_files('static')
135 135
136 def has_static(self, filename): 136 def has_static(self, filename):
137 return self.has_file(self.static_filename(filename)) 137 return self.has_file(self.static_filename(filename))
138 138
139 def read_static(self, filename): 139 def read_static(self, filename):
140 return self.read_file(self.static_filename(filename), binary=True)[0] 140 return self.read_file(self.static_filename(filename), binary=True)[0]
141 141
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 result[key] = value['message'] 176 result[key] = value['message']
177 177
178 return result 178 return result
179 179
180 # 180 #
181 # Template helpers 181 # Template helpers
182 # 182 #
183 183
184 @staticmethod 184 @staticmethod
185 def template_filename(template): 185 def template_filename(template):
186 return 'templates/%s.tmpl' % template 186 return 'templates/{}.tmpl'.format(template)
187 187
188 def read_template(self, template): 188 def read_template(self, template):
189 return self.read_file(self.template_filename(template)) 189 return self.read_file(self.template_filename(template))
190 190
191 # 191 #
192 # Include helpers 192 # Include helpers
193 # 193 #
194 194
195 @staticmethod 195 @staticmethod
196 def include_filename(include, format): 196 def include_filename(include, format):
197 return 'includes/%s.%s' % (include, format) 197 return 'includes/{}.{}'.format(include, format)
198 198
199 def has_include(self, include, format): 199 def has_include(self, include, format):
200 return self.has_file(self.include_filename(include, format)) 200 return self.has_file(self.include_filename(include, format))
201 201
202 def read_include(self, include, format): 202 def read_include(self, include, format):
203 return self.read_file(self.include_filename(include, format)) 203 return self.read_file(self.include_filename(include, format))
204 204
205 205
206 class MercurialSource(Source): 206 class MercurialSource(Source):
207 def __init__(self, repo): 207 def __init__(self, repo):
208 command = ['hg', '-R', repo, 'archive', '-r', 'default', 208 command = ['hg', '-R', repo, 'archive', '-r', 'default',
209 '-t', 'uzip', '-p', '.', '-'] 209 '-t', 'uzip', '-p', 'root', '-']
210 data = subprocess.check_output(command) 210 data = subprocess.check_output(command)
211 self._archive = zipfile.ZipFile(StringIO(data), mode='r') 211 self._archive = zipfile.ZipFile(StringIO(data), mode='r')
212 212
213 command = ['hg', '-R', repo, 'id', '-n', '-r', 'default'] 213 command = ['hg', '-R', repo, 'id', '-n', '-r', 'default']
214 self.version = subprocess.check_output(command).strip() 214 self.version = subprocess.check_output(command).strip()
215 215
216 self._name = os.path.basename(repo.rstrip(os.path.sep)) 216 self._name = os.path.basename(repo.rstrip(os.path.sep))
217 217
218 def __enter__(self): 218 def __enter__(self):
219 return self 219 return self
220 220
221 def __exit__(self, type, value, traceback): 221 def __exit__(self, type, value, traceback):
222 self.close() 222 self.close()
223 return False 223 return False
224 224
225 def close(self): 225 def close(self):
226 self._archive.close() 226 self._archive.close()
227 227
228 def has_file(self, filename): 228 def has_file(self, filename):
229 try: 229 try:
230 self._archive.getinfo('./%s' % filename) 230 self._archive.getinfo('root/{}'.format(filename))
231 except KeyError: 231 except KeyError:
232 return False 232 return False
233 return True 233 return True
234 234
235 def read_file(self, filename, binary=False): 235 def read_file(self, filename, binary=False):
236 data = self._archive.read('./%s' % filename) 236 data = self._archive.read('root/{}'.format(filename))
237 if not binary: 237 if not binary:
238 data = data.decode('utf-8') 238 data = data.decode('utf-8')
239 return (data, '%s!%s' % (self._name, filename)) 239 return (data, '{}!{}'.format(self._name, filename))
240 240
241 def list_files(self, subdir): 241 def list_files(self, subdir):
242 prefix = './%s/' % subdir 242 prefix = 'root/{}/'.format(subdir)
243 for filename in self._archive.namelist(): 243 for filename in self._archive.namelist():
244 if filename.startswith(prefix): 244 if filename.startswith(prefix):
245 yield filename[len(prefix):] 245 yield filename[len(prefix):]
246 246
247 if os.name == 'posix': 247 if os.name == 'posix':
248 def get_cache_dir(self): 248 def get_cache_dir(self):
249 return '/var/cache/' + self._name 249 return '/var/cache/' + self._name
250 250
251 251
252 class FileSource(Source): 252 class FileSource(Source):
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 path = os.path.join(dir, filename) 292 path = os.path.join(dir, filename)
293 if os.path.isfile(path): 293 if os.path.isfile(path):
294 result.append(relpath + filename) 294 result.append(relpath + filename)
295 elif os.path.isdir(path): 295 elif os.path.isdir(path):
296 do_list(path, relpath + filename + '/') 296 do_list(path, relpath + filename + '/')
297 do_list(self.get_path(subdir), '') 297 do_list(self.get_path(subdir), '')
298 return result 298 return result
299 299
300 def get_cache_dir(self): 300 def get_cache_dir(self):
301 return os.path.join(self._dir, 'cache') 301 return os.path.join(self._dir, 'cache')
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld