[PATCH v2 4/6] extractor: Re-add 'main' method
Stephen Finucane
stephen at that.guru
Thu Dec 1 05:42:45 AEDT 2016
This was removed in '3c2ee2d'. Re-add it, so it can be used by scripts
found in 'tools'. Unlike the original version, this returns error codes
if a patch is not found, and it uses argparse as optparse is deprecated.
Signed-off-by: Stephen Finucane <stephen at that.guru>
Cc: Paul Jakma <paul at jakma.org>
Cc: Tom Rini <trini at konsulko.com>
---
patchwork/extractor.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 50 insertions(+), 3 deletions(-)
diff --git a/patchwork/extractor.py b/patchwork/extractor.py
index 74fe4ab..f6c3c7d 100644
--- a/patchwork/extractor.py
+++ b/patchwork/extractor.py
@@ -21,11 +21,21 @@
"""Extract comments and (optional) diffs from mbox files."""
+import argparse
import codecs
+import email
import hashlib
import re
+import sys
-from django.utils import six
+try:
+ # Python 2
+ text_type = unicode
+ PY2 = True
+except NameError:
+ # Python 3
+ text_type = str
+ PY2 = False
HUNK_RE = re.compile(r'^\@\@ -\d+(?:,(\d+))? \+\d+(?:,(\d+))? \@\@')
FILENAME_RE = re.compile(r'^(---|\+\+\+) (\S+)')
@@ -208,7 +218,7 @@ def find_content(mail):
payload = part.get_payload(decode=True)
subtype = part.get_content_subtype()
- if not isinstance(payload, six.text_type):
+ if not isinstance(payload, text_type):
charset = part.get_content_charset()
# Check that we have a charset that we understand. Otherwise,
@@ -228,7 +238,7 @@ def find_content(mail):
for cset in try_charsets:
try:
- payload = six.text_type(payload, cset)
+ payload = text_type(payload, cset)
break
except UnicodeDecodeError:
payload = None
@@ -296,3 +306,40 @@ def hash_diff(diff):
hash.update((line + '\n').encode('utf-8'))
return hash
+
+
+def main(args):
+ parser = argparse.ArgumentParser()
+ parser.add_argument('-p', '--patch', action='store_true',
+ dest='print_patch', help='print parsed patch')
+ parser.add_argument('-c', '--comment', action='store_true',
+ dest='print_comment', help='print parsed comment')
+ parser.add_argument('-#', '--hash', action='store_true',
+ dest='print_hash', help='print patch hash')
+
+ args = parser.parse_args()
+
+ if PY2:
+ mail = email.message_from_file(sys.stdin)
+ else:
+ mail = email.message_from_binary_file(sys.stdin.buffer)
+
+ patch, comment = find_content(mail)
+
+ if args.print_hash and patch:
+ print(hash_diff(patch).hexdigest())
+ return 0
+
+ if args.print_patch and patch:
+ print(patch)
+ return 0
+
+ if args.print_comment and comment:
+ print(comment)
+ return 0
+
+ return -1
+
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv))
--
2.9.3
More information about the Patchwork
mailing list