OLD | NEW |
(Empty) | |
| 1 # This file is part of the Adblock Plus web scripts, |
| 2 # Copyright (C) 2006-present eyeo GmbH |
| 3 # |
| 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 |
| 6 # published by the Free Software Foundation. |
| 7 # |
| 8 # Adblock Plus is distributed in the hope that it will be useful, |
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 # GNU General Public License for more details. |
| 12 # |
| 13 # You should have received a copy of the GNU General Public License |
| 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
| 16 """Generate a diff between two filter lists.""" |
| 17 |
| 18 import re |
| 19 import difflib |
| 20 import hashlib |
| 21 import base64 |
| 22 |
| 23 |
| 24 def generate_diff(base, latest): |
| 25 """Return a diff between two filter lists.""" |
| 26 # Write the comments |
| 27 diff = '[Adblock Plus Diff 2.0]\n' |
| 28 sp_comment = re.compile(r'!\s(.*):\s(.*)') |
| 29 a = [] |
| 30 b = [] |
| 31 ver = re.compile(r'(.*/)\d+(.txt)') |
| 32 for line in latest.splitlines(): |
| 33 m = sp_comment.match(line) |
| 34 if m: |
| 35 if m.groups()[0] not in {'Checksum', 'Diff-URL'}: |
| 36 diff += '! {}: {}\n'.format(m.groups()[0], m.groups()[1]) |
| 37 if m.groups()[0] == 'Version': |
| 38 version = m.groups()[1] |
| 39 if m.groups()[0] == 'Diff-URL': |
| 40 url = ver.match(m.groups()[1]) |
| 41 diff += '! Diff-URL: {}{}{}\n'.format(url.groups()[0], version, |
| 42 url.groups()[1]) |
| 43 elif not line.startswith('!'): |
| 44 a.append(line.strip()) |
| 45 |
| 46 # Get the diff between the rest of the lines |
| 47 for line in base.splitlines(): |
| 48 if not line.startswith('!'): |
| 49 b.append(line.strip()) |
| 50 diffy = difflib.ndiff(b, a) |
| 51 for item in diffy: |
| 52 if item.startswith('+ ') or item.startswith('- '): |
| 53 diff += item |
| 54 diff += '\n' |
| 55 |
| 56 # Add the checksum |
| 57 md5sum = hashlib.md5() |
| 58 for line in diff.splitlines(): |
| 59 if not line.strip(): |
| 60 md5sum.update(line.encode('utf-8') + b'\n') |
| 61 checksum = base64.b64encode(md5sum.digest()).rstrip(b'=') |
| 62 signed_diff = (diff[:24] + |
| 63 '! Checksum: {}\n'.format(checksum.decode('utf-8')) + |
| 64 diff[24:]) |
| 65 return signed_diff |
| 66 |
| 67 |
| 68 def publish_diff(diff, outfile): |
| 69 """Write the diff to the outfile.""" |
| 70 with open(outfile, 'w+') as outfile: |
| 71 outfile.write(diff) |
OLD | NEW |