Index: abp/filters/diff_script.py |
=================================================================== |
--- a/abp/filters/diff_script.py |
+++ b/abp/filters/diff_script.py |
@@ -20,41 +20,57 @@ |
import argparse |
import io |
import sys |
+import os |
from .renderer import render_diff |
+from .parser import parse_filterlist |
__all__ = ['main'] |
-def parse_args(): |
+class MissingFilterListError(Exception): |
+ """Error in finding a latest filter list, or base filter lists.""" |
+ |
+ |
+def _get_version(filterlist, base): |
Vasily Kuznetsov
2018/10/29 23:47:33
Nit: maybe this argument can just be called "filen
rhowell
2018/10/31 00:20:58
Done.
|
+ for line in parse_filterlist(filterlist): |
+ if line.type == 'metadata' and line.key == 'Version': |
+ return line.value |
+ raise MissingFilterListError('Unable to find Version in {}'.format(base)) |
+ |
+ |
+def parse_args(args_in=None): |
parser = argparse.ArgumentParser(description='Render a filter list diff.') |
- parser.add_argument( |
- 'base', help='the older filter list that needs to be updated', |
- nargs='?') |
- parser.add_argument( |
- 'latest', help='the most recent version of the filter list', |
- nargs='?') |
- parser.add_argument( |
- 'outfile', help='output file for filter list diff', |
- default='-', nargs='?') |
- return parser.parse_args() |
+ parser.add_argument('latest', |
+ help='The most recent version of the filter list') |
+ parser.add_argument('-o', '--output_dir', default=os.getcwd(), |
+ help='The directory to write the diffs to') |
+ parser.add_argument('base_files', nargs='+', |
+ help='One or more archived filter lists') |
+ return parser.parse_args(args_in) |
-def main(): |
+def main(args_in=None): |
"""Entry point for the diff rendering script (fldiff).""" |
- args = parse_args() |
+ arg = parse_args(args_in) |
- with io.open(args.base, 'r', encoding='utf-8') as base, \ |
- io.open(args.latest, 'r', encoding='utf-8') as latest: |
+ with io.open(arg.latest, 'r', encoding='utf8') as latest_list: |
+ latest = latest_list.readlines() |
+ |
+ for base_file in arg.base_files: |
+ with io.open(base_file, 'r', encoding='utf8') as base_file: |
+ base = base_file.readlines() |
lines = render_diff(base, latest) |
- if args.outfile == '-': |
- outfile = io.open(sys.stdout.fileno(), 'w', |
- closefd=False, |
- encoding=sys.stdout.encoding or 'utf-8') |
- else: |
- outfile = io.open(args.outfile, 'w', encoding='utf-8') |
+ try: |
+ version = _get_version(base, base_file.name) |
+ except MissingFilterListError as exc: |
+ sys.exit(exc) |
- with outfile: |
+ # TODO: After removing the dict test, this .strip() can go |
+ outfile = os.path.join(arg.output_dir.strip(), |
+ 'diff{}.txt'.format(version)) |
+ |
+ with io.open(outfile, 'w', encoding='utf-8') as out_fp: |
for line in lines: |
- print(line, file=outfile) |
+ out_fp.write(line + '\n') |