[PATCH 03/17] nmbug-status: Decode Popen output using the user's locale
authorW. Trevor King <wking@tremily.us>
Mon, 3 Feb 2014 10:59:21 +0000 (02:59 +1600)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:59:36 +0000 (09:59 -0800)
73/a4be0fe31a7f7fafb79fb95294f27e0cebf976 [new file with mode: 0644]

diff --git a/73/a4be0fe31a7f7fafb79fb95294f27e0cebf976 b/73/a4be0fe31a7f7fafb79fb95294f27e0cebf976
new file mode 100644 (file)
index 0000000..652f785
--- /dev/null
@@ -0,0 +1,148 @@
+Return-Path: <wking@tremily.us>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id DA9BC429E37\r
+       for <notmuch@notmuchmail.org>; Mon,  3 Feb 2014 03:00:52 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5\r
+       tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001]\r
+       autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id oufyJVMxC3mG for <notmuch@notmuchmail.org>;\r
+       Mon,  3 Feb 2014 03:00:44 -0800 (PST)\r
+Received: from qmta06.westchester.pa.mail.comcast.net\r
+       (qmta06.westchester.pa.mail.comcast.net [76.96.62.56])\r
+       by olra.theworths.org (Postfix) with ESMTP id 346BD431E82\r
+       for <notmuch@notmuchmail.org>; Mon,  3 Feb 2014 03:00:40 -0800 (PST)\r
+Received: from omta24.westchester.pa.mail.comcast.net ([76.96.62.76])\r
+       by qmta06.westchester.pa.mail.comcast.net with comcast\r
+       id Mmyc1n0051ei1Bg56n0fTQ; Mon, 03 Feb 2014 11:00:39 +0000\r
+Received: from odin.tremily.us ([24.18.63.50])\r
+       by omta24.westchester.pa.mail.comcast.net with comcast\r
+       id Mn0e1n00C152l3L3kn0fgZ; Mon, 03 Feb 2014 11:00:39 +0000\r
+Received: from mjolnir.tremily.us (unknown [192.168.0.140])\r
+       by odin.tremily.us (Postfix) with ESMTPS id 66E23FB4D45;\r
+       Mon,  3 Feb 2014 03:00:38 -0800 (PST)\r
+Received: (nullmailer pid 682 invoked by uid 1000);\r
+       Mon, 03 Feb 2014 10:59:41 -0000\r
+From: "W. Trevor King" <wking@tremily.us>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 03/17] nmbug-status: Decode Popen output using the user's\r
+       locale\r
+Date: Mon,  3 Feb 2014 02:59:21 -0800\r
+Message-Id:\r
+ <9a07face73274cbb977b8a8e4e983e64b0863531.1391424512.git.wking@tremily.us>\r
+X-Mailer: git-send-email 1.8.5.2.8.g0f6c0d1\r
+In-Reply-To: <cover.1391424512.git.wking@tremily.us>\r
+References: <cover.1391424512.git.wking@tremily.us>\r
+In-Reply-To: <cover.1391424512.git.wking@tremily.us>\r
+References: <cover.1391424512.git.wking@tremily.us>\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=comcast.net;\r
+       s=q20121106; t=1391425239;\r
+       bh=1aQ15C7VOfb5SpJX2PiuZjmexLCSbY7tx6l56EDHxmQ=;\r
+       h=Received:Received:Received:Received:From:To:Subject:Date:\r
+       Message-Id;\r
+       b=TMdHbFIg1lQ9PJ/IDfoz4OhY9vahsGG0R3YCe5MBoAzaf+MNVMU06dDvGvKTEUWVU\r
+       JOulrby7W7CTKT/rl+Z1fgMOtM2BkrxNH7GfnSbb/Rc3CDl746r8siK6dkmuqJn+RN\r
+       RQMUa4lxZ/JxZy/QoBZQGMclg38f+HphLVaC7cBKDb2cAOZkA/89Hs/kdGorRECbHM\r
+       gLfAaqneASlg0a9gkIgeOc/th6BM1coiQtBmgwU5gczB/jYugniguauHAUARcz03gG\r
+       7Ms43ojc7q177Di5HDfMvVM+tKUVD/TFS1so0NZ4yTc8aMkZNu+O/Wr1+v4Q9+NK5v\r
+       WHQuBEEVaeUFA==\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Mon, 03 Feb 2014 11:00:53 -0000\r
+\r
+Avoid:\r
+\r
+  $ ./nmbug-status --list-views\r
+  Traceback (most recent call last):\r
+    File "./nmbug-status", line 47, in <module>\r
+      'cat-file', 'blob', sha1+':status-config.json'],\r
+  TypeError: can't concat bytes to str\r
+\r
+by explicitly converting the byte-stream read from Popen into a\r
+Unicode string.  On Python 2, this conversion is str -> unicode; on\r
+Python 3 it is bytes -> str.\r
+\r
+_ENCODING is derived from the user's locale (or system default) in an\r
+attempt to match Git's output encoding.  It may be more robust to skip\r
+the encoding/decoding by using a Python wrapper like pygit2 [1] for\r
+Git access.  That's a fairly heavy dependency though, and using the\r
+locale will probably work.\r
+\r
+[1]: http://www.pygit2.org/\r
+---\r
+ devel/nmbug/nmbug-status | 25 ++++++++++++++++---------\r
+ 1 file changed, 16 insertions(+), 9 deletions(-)\r
+\r
+diff --git a/devel/nmbug/nmbug-status b/devel/nmbug/nmbug-status\r
+index 6525176..a7a391d 100755\r
+--- a/devel/nmbug/nmbug-status\r
++++ b/devel/nmbug/nmbug-status\r
+@@ -8,8 +8,10 @@\r
\r
+ from __future__ import print_function\r
\r
++import codecs\r
+ import datetime\r
+ import email.utils\r
++import locale\r
+ import urllib\r
+ import json\r
+ import argparse\r
+@@ -17,6 +19,10 @@ import os\r
+ import sys\r
+ import subprocess\r
\r
++\r
++_ENCODING = locale.getpreferredencoding() or sys.getdefaultencoding()\r
++\r
++\r
+ # parse command line arguments\r
\r
+ parser = argparse.ArgumentParser()\r
+@@ -37,15 +43,16 @@ else:\r
+     nmbhome = os.getenv('NMBGIT', os.path.expanduser('~/.nmbug'))\r
\r
+     # read only the first line from the pipe\r
+-    sha1 = subprocess.Popen(['git', '--git-dir', nmbhome,\r
+-                             'show-ref', '-s', 'config'],\r
+-                            stdout=subprocess.PIPE).stdout.readline()\r
+-\r
+-    sha1 = sha1.rstrip()\r
+-\r
+-    fp = subprocess.Popen(['git', '--git-dir', nmbhome,\r
+-                           'cat-file', 'blob', sha1+':status-config.json'],\r
+-                          stdout=subprocess.PIPE).stdout\r
++    sha1_bytes = subprocess.Popen(\r
++        ['git', '--git-dir', nmbhome, 'show-ref', '-s', 'config'],\r
++        stdout=subprocess.PIPE).stdout.readline()\r
++    sha1 = sha1_bytes.decode(_ENCODING).rstrip()\r
++\r
++    fp_byte_stream = subprocess.Popen(\r
++        ['git', '--git-dir', nmbhome, 'cat-file', 'blob',\r
++         sha1+':status-config.json'],\r
++        stdout=subprocess.PIPE).stdout\r
++    fp = codecs.getreader(encoding=_ENCODING)(stream=fp_byte_stream)\r
\r
+ config = json.load(fp)\r
\r
+-- \r
+1.8.5.2.8.g0f6c0d1\r
+\r