Index: abp/filters/diff_script.py |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/abp/filters/diff_script.py |
@@ -0,0 +1,62 @@ |
+# This file is part of Adblock Plus <https://adblockplus.org/>, |
+# Copyright (C) 2006-present eyeo GmbH |
+# |
+# Adblock Plus is free software: you can redistribute it and/or modify |
+# it under the terms of the GNU General Public License version 3 as |
+# published by the Free Software Foundation. |
+# |
+# Adblock Plus is distributed in the hope that it will be useful, |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+# GNU General Public License for more details. |
+# |
+# You should have received a copy of the GNU General Public License |
+# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ |
+"""Command line script for rendering Adblock Plus filter list diffs.""" |
+ |
+import argparse |
+import io |
+import sys |
+ |
+from collections import namedtuple |
+ |
+from .sources import NotFound |
+from .renderer import render_diff |
+__all__ = ['main'] |
+ |
+ |
+def parse_args(): |
+ parser = argparse.ArgumentParser(description='Render a filter list diff.') |
+ parser.add_argument( |
+ 'base', help='the older filter list that needs to be updated', |
+ default='-', nargs='?') |
Vasily Kuznetsov
2018/09/19 18:40:04
It seems that base and latest should be mandatory
rhowell
2018/09/21 08:37:09
Done.
|
+ parser.add_argument( |
+ 'latest', help='the most recent version of the filter list', |
+ default='-', nargs='?') |
+ parser.add_argument( |
+ 'outfile', help='output file for filter list diff', |
+ default='-', nargs='?') |
+ return parser.parse_args() |
+ |
+ |
+def main(args_in=None): |
+ """Entry point for the diff rendering script (diffrender).""" |
+ if args_in: |
+ Parse = namedtuple('Test', 'base, latest, outfile') |
Vasily Kuznetsov
2018/09/19 18:40:04
Are we going to use this code path? If yes, I woul
rhowell
2018/09/21 08:37:08
Good point. This was helpful for me while testing,
|
+ args = Parse(args_in['base'], args_in['latest'], args_in['outfile']) |
+ else: |
+ args = parse_args() |
+ |
+ with io.open(args.base, 'r') as base, io.open(args.latest, 'r') as latest: |
+ try: |
+ lines = render_diff(base, latest) |
Sebastian Noack
2018/09/18 19:13:18
Iterating over a file-object yields each line incl
Vasily Kuznetsov
2018/09/19 18:40:04
Actually metadata is also stripped of the line end
Sebastian Noack
2018/09/20 16:59:12
Ah, right, that is because by default . doesn't ma
rhowell
2018/09/21 08:37:09
Acknowledged.
|
+ if args.outfile == '-': |
+ for line in lines: |
+ sys.stdout.write(line + '\n') |
Sebastian Noack
2018/09/18 19:13:18
Does that work on Python 2 if there are any non-as
Sebastian Noack
2018/09/20 16:59:12
For reference, the problem is that in Python 2 if
Vasily Kuznetsov
2018/09/21 07:48:42
This is more or less the approach that we took in
rhowell
2018/09/21 08:37:08
This approach seems to be working well. Thanks, I
Sebastian Noack
2018/09/26 11:55:47
I just noticed, we might have the same issue in re
Vasily Kuznetsov
2018/09/26 16:50:40
Yeah, makes sense. Perhaps we could move this piec
|
+ else: |
+ with io.open(args.outfile, 'w', encoding='utf-8') as out_fp: |
+ for line in lines: |
+ out_fp.write(line + '\n') |
+ except NotFound as exc: |
+ sys.exit(exc) |