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