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 |
| 20 from .renderer import _insert_checksum |
| 21 |
| 22 |
| 23 def generate_diff(base, latest): |
| 24 """Return a diff between two filter lists.""" |
| 25 # Collect the special comments |
| 26 diff = '[Adblock Plus Diff 2.0]\n' |
| 27 sp_comment = re.compile(r'!\s(.*):\s(.*)') |
| 28 latest_fl = set() |
| 29 latest_md = set() |
| 30 latest_keys = set() |
| 31 base_fl = set() |
| 32 base_md = set() |
| 33 base_keys = set() |
| 34 for line in latest.splitlines(): |
| 35 m = sp_comment.match(line) |
| 36 if m: |
| 37 latest_md.add(line) |
| 38 latest_keys.add(m.groups()[0]) |
| 39 elif not line.startswith('!'): |
| 40 latest_fl.add(line.strip()) |
| 41 |
| 42 # Get the diff between the rest of the lines |
| 43 for line in base.splitlines(): |
| 44 if not line.startswith('!'): |
| 45 base_fl.add(line.strip()) |
| 46 else: |
| 47 m = sp_comment.match(line) |
| 48 if m: |
| 49 base_md.add(line) |
| 50 base_keys.add(m.groups()[0]) |
| 51 new_md = latest_md - base_md |
| 52 removed_keys = base_keys - latest_keys |
| 53 add_fl = latest_fl - base_fl |
| 54 remove_fl = base_fl - latest_fl |
| 55 for item in new_md: |
| 56 diff += item + '\n' |
| 57 for key in removed_keys: |
| 58 # If a special comment has been removed, enter it with a blank value |
| 59 # sp the client will set it back to the default value |
| 60 diff += '! {}:\n'.format(key) |
| 61 for item in add_fl: |
| 62 diff += '+ {}\n'.format(item) |
| 63 for item in remove_fl: |
| 64 diff += '- {}\n'.format(item) |
| 65 |
| 66 # Add the checksum |
| 67 _insert_checksum(diff) |
| 68 return diff |
| 69 |
| 70 |
| 71 def publish_diff(diff, outfile): |
| 72 """Write the diff to the outfile.""" |
| 73 with open(outfile, 'w+') as outfile: |
| 74 outfile.write(diff) |
OLD | NEW |