| OLD | NEW |
| 1 # This file is part of Adblock Plus <https://adblockplus.org/>, | 1 # This file is part of Adblock Plus <https://adblockplus.org/>, |
| 2 # Copyright (C) 2006-present eyeo GmbH | 2 # Copyright (C) 2006-present 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 |
| 11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. |
| 12 # | 12 # |
| 13 # You should have received a copy of the GNU General Public License | 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/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 15 | 15 |
| 16 """Command line script for rendering Adblock Plus filter list diffs.""" | 16 """Command line script for rendering Adblock Plus filter list diffs.""" |
| 17 | 17 |
| 18 from __future__ import print_function | 18 from __future__ import print_function |
| 19 | 19 |
| 20 import argparse | 20 import argparse |
| 21 import io | 21 import io |
| 22 import sys | 22 import sys |
| 23 import os |
| 23 | 24 |
| 24 from .renderer import render_diff | 25 from .renderer import render_diff |
| 26 from .parser import parse_filterlist |
| 25 | 27 |
| 26 __all__ = ['main'] | 28 __all__ = ['main'] |
| 27 | 29 |
| 28 | 30 |
| 31 class MissingVersionError(Exception): |
| 32 """Unable to find Version in filter list.""" |
| 33 |
| 34 |
| 35 def _get_version(filterlist, filename): |
| 36 for line in parse_filterlist(filterlist): |
| 37 if line.type == 'metadata' and line.key == 'Version': |
| 38 return line.value |
| 39 raise MissingVersionError('Unable to find Version in {}'.format(filename)) |
| 40 |
| 41 |
| 29 def parse_args(): | 42 def parse_args(): |
| 30 parser = argparse.ArgumentParser(description='Render a filter list diff.') | 43 parser = argparse.ArgumentParser(description='Render a filter list diff.') |
| 31 parser.add_argument( | 44 parser.add_argument('latest', |
| 32 'base', help='the older filter list that needs to be updated', | 45 help='The most recent version of the filter list') |
| 33 nargs='?') | 46 parser.add_argument('-o', '--output_dir', default=os.getcwd(), |
| 34 parser.add_argument( | 47 help='The directory to write the diffs to') |
| 35 'latest', help='the most recent version of the filter list', | 48 parser.add_argument('base_files', nargs='+', |
| 36 nargs='?') | 49 help='One or more archived filter lists') |
| 37 parser.add_argument( | |
| 38 'outfile', help='output file for filter list diff', | |
| 39 default='-', nargs='?') | |
| 40 return parser.parse_args() | 50 return parser.parse_args() |
| 41 | 51 |
| 42 | 52 |
| 43 def main(): | 53 def main(): |
| 44 """Entry point for the diff rendering script (fldiff).""" | 54 """Entry point for the diff rendering script (fldiff).""" |
| 45 args = parse_args() | 55 args = parse_args() |
| 56 with io.open(args.latest, 'r', encoding='utf8') as latest_list: |
| 57 latest = latest_list.readlines() |
| 46 | 58 |
| 47 with io.open(args.base, 'r', encoding='utf-8') as base, \ | 59 for base_file in args.base_files: |
| 48 io.open(args.latest, 'r', encoding='utf-8') as latest: | 60 with io.open(base_file, 'r', encoding='utf8') as base_file: |
| 61 base = base_file.readlines() |
| 49 | 62 |
| 50 lines = render_diff(base, latest) | 63 lines = render_diff(base, latest) |
| 51 if args.outfile == '-': | 64 try: |
| 52 outfile = io.open(sys.stdout.fileno(), 'w', | 65 version = _get_version(base, base_file.name) |
| 53 closefd=False, | 66 except MissingVersionError as exc: |
| 54 encoding=sys.stdout.encoding or 'utf-8') | 67 sys.exit(exc) |
| 55 else: | |
| 56 outfile = io.open(args.outfile, 'w', encoding='utf-8') | |
| 57 | 68 |
| 58 with outfile: | 69 outfile = os.path.join(args.output_dir, 'diff{}.txt'.format(version)) |
| 70 with io.open(outfile, 'w', encoding='utf-8') as out_fp: |
| 59 for line in lines: | 71 for line in lines: |
| 60 print(line, file=outfile) | 72 out_fp.write(line + '\n') |
| OLD | NEW |