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

Delta Between Two Patch Sets: abp/filters/diff_script.py

Issue 29922555: Issue 7059 - Modify fldiff so it can handle multiple files (Closed) Base URL: https://hg.adblockplus.org/python-abp
Left Patch Set: Address comments on PS1 Created Oct. 26, 2018, 9:40 p.m.
Right Patch Set: Address comments on PS4 Created Oct. 31, 2018, 7:11 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « README.md ('k') | tests/test_diff_script.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
(...skipping 10 matching lines...) Expand all
21 import io 21 import io
22 import sys 22 import sys
23 import os 23 import os
24 24
25 from .renderer import render_diff 25 from .renderer import render_diff
26 from .parser import parse_filterlist 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 _get_version(filterlist): 35 def _get_version(filterlist, filename):
36 for line in parse_filterlist(filterlist): 36 for line in parse_filterlist(filterlist):
37 if line.type == 'header':
38 base = line.version
39 if line.type == 'metadata' and line.key == 'Version': 37 if line.type == 'metadata' and line.key == 'Version':
40 return line.value 38 return line.value
41 raise MissingFilterListError('Unable to find Version in [{}]'.format(base)) 39 raise MissingVersionError('Unable to find Version in {}'.format(filename))
Vasily Kuznetsov 2018/10/29 16:31:48 Wouldn't it be more helpful to give filename in th
rhowell 2018/10/29 20:01:40 Yeah. With this (old) method, it returns the heade
42 40
43 41
44 def parse_args(args_in=None): 42 def parse_args():
45 parser = argparse.ArgumentParser(description='Render a filter list diff.') 43 parser = argparse.ArgumentParser(description='Render a filter list diff.')
46 parser.add_argument('latest', 44 parser.add_argument('latest',
47 help='The most recent version of the filter list') 45 help='The most recent version of the filter list')
48 parser.add_argument('-o', '--output_dir', default=os.getcwd(), 46 parser.add_argument('-o', '--output_dir', default=os.getcwd(),
49 help='The directory to write the diffs to') 47 help='The directory to write the diffs to')
50 parser.add_argument('base_files', nargs='+', 48 parser.add_argument('base_files', nargs='+',
51 help='One or more archived filter lists') 49 help='One or more archived filter lists')
52 return parser.parse_args(args_in) 50 return parser.parse_args()
53 51
54 52
55 def main(args_in=None): 53 def main():
56 """Entry point for the diff rendering script (fldiff).""" 54 """Entry point for the diff rendering script (fldiff)."""
57 arg = parse_args(args_in) 55 args = parse_args()
58 56 with io.open(args.latest, 'r', encoding='utf8') as latest_list:
59 with io.open(arg.latest, 'r', encoding='utf8') as latest_list:
60 latest = latest_list.readlines() 57 latest = latest_list.readlines()
61 58
62 for base_file in arg.base_files: 59 for base_file in args.base_files:
63 with io.open(base_file, 'r', encoding='utf8') as base_file: 60 with io.open(base_file, 'r', encoding='utf8') as base_file:
64 base = base_file.readlines() 61 base = base_file.readlines()
65 lines = render_diff(base, latest)
66 try:
67 version = _get_version(base)
68 except MissingFilterListError as exc:
69 sys.exit(exc)
70 62
71 # TODO: After removing the dict test, this .strip() can go 63 lines = render_diff(base, latest)
72 outfile = os.path.join(arg.output_dir.strip(), 64 try:
73 'diff{}.txt'.format(version)) 65 version = _get_version(base, base_file.name)
66 except MissingVersionError as exc:
67 sys.exit(exc)
74 68
75 with io.open(outfile, 'w', encoding='utf-8') as out_fp: 69 outfile = os.path.join(args.output_dir, 'diff{}.txt'.format(version))
76 for line in lines: 70 with io.open(outfile, 'w', encoding='utf-8') as out_fp:
77 out_fp.write(line + '\n') 71 for line in lines:
72 out_fp.write(line + '\n')
LEFTRIGHT

Powered by Google App Engine
This is Rietveld