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

Side by Side Diff: sitescripts/subscriptions/combineSubscriptions.py

Issue 6282217760227328: Issue 199 - [filter lists] Don`t allow multiple Expires comments (Closed)
Patch Set: Created March 24, 2014, 7:52 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 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # coding: utf-8 2 # coding: utf-8
3 3
4 # This file is part of the Adblock Plus web scripts, 4 # This file is part of the Adblock Plus web scripts,
5 # Copyright (C) 2006-2014 Eyeo GmbH 5 # Copyright (C) 2006-2014 Eyeo GmbH
6 # 6 #
7 # Adblock Plus is free software: you can redistribute it and/or modify 7 # Adblock Plus is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License version 3 as 8 # it under the terms of the GNU General Public License version 3 as
9 # published by the Free Software Foundation. 9 # published by the Free Software Foundation.
10 # 10 #
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 if len(lines) > 0: 82 if len(lines) > 0:
83 header = lines.pop(0) 83 header = lines.pop(0)
84 if not re.search(r"\[Adblock(?:\s*Plus\s*([\d\.]+)?)?\]", header, re.I): 84 if not re.search(r"\[Adblock(?:\s*Plus\s*([\d\.]+)?)?\]", header, re.I):
85 raise Exception("This is not a valid Adblock Plus subscription file.") 85 raise Exception("This is not a valid Adblock Plus subscription file.")
86 86
87 lines = resolve_includes(source_name, sources, lines, timeout) 87 lines = resolve_includes(source_name, sources, lines, timeout)
88 seen = set(["checksum", "version"]) 88 seen = set(["checksum", "version"])
89 def check_line(line): 89 def check_line(line):
90 if line == "": 90 if line == "":
91 return False 91 return False
92 match = re.search(r"^\s*!\s*(Redirect|Homepage|Title|Checksum|Version)\s*:", line, re.M | re.I) 92 match = re.search(r"^\s*!\s*(Redirect|Homepage|Title|Checksum|Version|Expire s)\s*:", line, re.M | re.I)
93 if not match: 93 if not match:
94 return True 94 return True
95 key = match.group(1).lower() 95 key = match.group(1).lower()
96 if key in seen: 96 if key in seen:
97 return False 97 return False
98 seen.add(key) 98 seen.add(key)
99 return True 99 return True
100 lines = filter(check_line, lines) 100 lines = filter(check_line, lines)
101 101
102 write_tpl(save_file, os.path.splitext(filename)[0] + ".tpl", lines) 102 write_tpl(save_file, os.path.splitext(filename)[0] + ".tpl", lines)
(...skipping 28 matching lines...) Expand all
131 except urllib2.URLError, e: 131 except urllib2.URLError, e:
132 error = e 132 error = e
133 time.sleep(5) 133 time.sleep(5)
134 if error: 134 if error:
135 raise error 135 raise error
136 136
137 # We should really get the charset from the headers rather than assuming 137 # We should really get the charset from the headers rather than assuming
138 # that it is UTF-8. However, some of the Google Code mirrors are 138 # that it is UTF-8. However, some of the Google Code mirrors are
139 # misconfigured and will return ISO-8859-1 as charset instead of UTF-8. 139 # misconfigured and will return ISO-8859-1 as charset instead of UTF-8.
140 newlines = data.decode("utf-8").splitlines() 140 newlines = data.decode("utf-8").splitlines()
141 newlines = filter(lambda l: not re.search(r"^\s*!.*?\bExpires\s*(?::|aft er)\s*(\d+)\s*(h)?", l, re.M | re.I), newlines) 141 newlines = filter(lambda l: not re.search(r"^\s*!\s*(Redirect|Homepage|T itle|Version|Expires)\s*:", l, re.M | re.I), newlines)
142 newlines = filter(lambda l: not re.search(r"^\s*!\s*(Redirect|Homepage|T itle|Version)\s*:", l, re.M | re.I), newlines)
143 else: 142 else:
144 result.append("! *** %s ***" % filename) 143 result.append("! *** %s ***" % filename)
145 144
146 include_source = source_name 145 include_source = source_name
147 if ":" in filename: 146 if ":" in filename:
148 include_source, filename = filename.split(":", 1) 147 include_source, filename = filename.split(":", 1)
149 if not include_source in sources: 148 if not include_source in sources:
150 raise Exception('Cannot include file from repository "%s", this reposi tory is unknown' % include_source) 149 raise Exception('Cannot include file from repository "%s", this reposi tory is unknown' % include_source)
151 150
152 source = sources[include_source] 151 source = sources[include_source]
(...skipping 11 matching lines...) Expand all
164 line = "" 163 line = ""
165 result.append(line) 164 result.append(line)
166 return result 165 return result
167 166
168 def write_tpl(save_file, filename, lines): 167 def write_tpl(save_file, filename, lines):
169 result = [] 168 result = []
170 result.append("msFilterList") 169 result.append("msFilterList")
171 for line in lines: 170 for line in lines:
172 if re.search(r"^\s*!", line): 171 if re.search(r"^\s*!", line):
173 # This is a comment. Handle "Expires" comment in a special way, keep the r est. 172 # This is a comment. Handle "Expires" comment in a special way, keep the r est.
174 match = re.search(r"\bExpires\s*(?::|after)\s*(\d+)\s*(h)?", line, re.I) 173 match = re.search(r"^\s*!\s*Expires\s*:\s*(\d+)\s*(h)?", line, re.I)
175 if match: 174 if match:
176 interval = int(match.group(1)) 175 interval = int(match.group(1))
177 if match.group(2): 176 if match.group(2):
178 interval = int(interval / 24) 177 interval = int(interval / 24)
179 result.append(": Expires=%i" % interval) 178 result.append(": Expires=%i" % interval)
180 else: 179 else:
181 result.append(re.sub(r"^\s*!", "#", re.sub(r"--!$", "--#", line))) 180 result.append(re.sub(r"^\s*!", "#", re.sub(r"--!$", "--#", line)))
182 elif line.find("#") >= 0: 181 elif line.find("#") >= 0:
183 # Element hiding rules are not supported in MSIE, drop them 182 # Element hiding rules are not supported in MSIE, drop them
184 pass 183 pass
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 317
319 timeout = 30 318 timeout = 30
320 for option, value in opts: 319 for option, value in opts:
321 if option in ("-h", "--help"): 320 if option in ("-h", "--help"):
322 usage() 321 usage()
323 sys.exit() 322 sys.exit()
324 elif option in ("-t", "--timeout"): 323 elif option in ("-t", "--timeout"):
325 timeout = int(value) 324 timeout = int(value)
326 325
327 combine_subscriptions(sources, target_dir, timeout) 326 combine_subscriptions(sources, target_dir, timeout)
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