Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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.path | 23 import os |
Vasily Kuznetsov
2018/10/24 11:30:09
You can actually just `import os` here and `os.pat
rhowell
2018/10/26 21:42:21
Done.
| |
24 import re | |
25 | 24 |
26 from .renderer import render_diff | 25 from .renderer import render_diff |
26 from .parser import parse_filterlist | |
27 | 27 |
28 __all__ = ['main'] | 28 __all__ = ['main'] |
29 | 29 |
30 | 30 |
31 class MissingFilterListError(Exception): | 31 class MissingVersionError(Exception): |
32 """Error in finding a latest filter list, or base filter lists.""" | 32 """Unable to find Version in filter list.""" |
33 | 33 |
34 | 34 |
35 def parse_args(args_in=None): | 35 def _get_version(filterlist, filename): |
36 parser = argparse.ArgumentParser(description='Render a filter list diff.') | 36 for line in parse_filterlist(filterlist): |
37 parser.add_argument( | 37 if line.type == 'metadata' and line.key == 'Version': |
Vasily Kuznetsov
2018/10/24 11:30:09
The formatting of this statement is different from
rhowell
2018/10/26 21:42:19
Done.
| |
38 '-o', '--output_dir', help='The directory to write the diffs to', | 38 return line.value |
39 default='-', nargs=1) | 39 raise MissingVersionError('Unable to find Version in {}'.format(filename)) |
Vasily Kuznetsov
2018/10/24 11:30:09
Since -o is now a directory, it can't default to `
rhowell
2018/10/26 21:42:18
Is `os.getcwd()` the best way to do that? It makes
| |
40 parser.add_argument( | |
41 '-l', '--latest', help='The most recent version of the filter list', | |
42 nargs=1) | |
43 return parser.parse_known_args(args_in) | |
Vasily Kuznetsov
2018/10/24 11:30:09
Wouldn't it be better to add the positional argume
rhowell
2018/10/26 21:42:19
Done.
| |
44 | 40 |
45 | 41 |
46 def main(args_in=None): | 42 def parse_args(): |
43 parser = argparse.ArgumentParser(description='Render a filter list diff.') | |
44 parser.add_argument('latest', | |
45 help='The most recent version of the filter list') | |
46 parser.add_argument('-o', '--output_dir', default=os.getcwd(), | |
47 help='The directory to write the diffs to') | |
48 parser.add_argument('base_files', nargs='+', | |
49 help='One or more archived filter lists') | |
50 return parser.parse_args() | |
51 | |
52 | |
53 def main(): | |
47 """Entry point for the diff rendering script (fldiff).""" | 54 """Entry point for the diff rendering script (fldiff).""" |
48 arg = parse_args(args_in) | 55 args = parse_args() |
56 with io.open(args.latest, 'r', encoding='utf8') as latest_list: | |
57 latest = latest_list.readlines() | |
49 | 58 |
50 try: | 59 for base_file in args.base_files: |
51 if arg[0].latest is None: | 60 with io.open(base_file, 'r', encoding='utf8') as base_file: |
52 raise MissingFilterListError('The --latest list is required') | 61 base = base_file.readlines() |
Vasily Kuznetsov
2018/10/24 11:30:08
This would be better handled by the arguments pars
rhowell
2018/10/26 21:42:19
Yeah, using a positional argument makes sense, and
| |
53 with io.open(arg[0].latest[0].strip(), 'r', | |
54 encoding='utf8') as latest_list: | |
55 latest = latest_list.readlines() | |
56 if len(arg[1]) == 0: | |
57 raise MissingFilterListError('Archived lists are required') | |
Vasily Kuznetsov
2018/10/24 11:30:09
This should also be handled by the arguments parse
rhowell
2018/10/26 21:42:18
Done.
| |
58 for base_file in arg[1]: | |
59 with io.open(base_file, 'r', encoding='utf8') as base: | |
60 out_dir = arg[0].output_dir[0].strip() | |
Vasily Kuznetsov
2018/10/24 11:30:08
Do you think we need to strip the provided output
rhowell
2018/10/26 21:42:20
Good point. For now, when testing with the diction
| |
61 | 62 |
62 lines = render_diff(base, latest) | 63 lines = render_diff(base, latest) |
63 if out_dir == '-': | 64 try: |
64 for line in lines: | 65 version = _get_version(base, base_file.name) |
65 sys.stdout.write(line + '\n') | 66 except MissingVersionError as exc: |
66 else: | 67 sys.exit(exc) |
67 version = re.search(r'.*(\d+)\.txt', base_file).groups()[0] | 68 |
Vasily Kuznetsov
2018/10/24 11:30:08
I wonder if we can really depend on the version be
rhowell
2018/10/26 21:42:20
Yeah, I wasn't sure how reliable this would be. Do
| |
68 outfile = os.path.join(out_dir, | 69 outfile = os.path.join(args.output_dir, 'diff{}.txt'.format(version)) |
69 'diff{}.txt'.format(version)) | 70 with io.open(outfile, 'w', encoding='utf-8') as out_fp: |
Vasily Kuznetsov
2018/10/24 11:30:08
The format of the output filename should probably
rhowell
2018/10/26 21:42:20
Interesting idea. What did you have in mind? Like,
| |
70 with io.open(outfile, 'w', encoding='utf-8') as out_fp: | 71 for line in lines: |
71 for line in lines: | 72 out_fp.write(line + '\n') |
72 out_fp.write(line + '\n') | |
73 except MissingFilterListError as exc: | |
74 sys.exit(exc) | |
LEFT | RIGHT |