From 3a1bb6fca44178f9d00290b7ed3f710d707041ed Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 11 Feb 2014 10:40:24 +1600 Subject: [PATCH] [PATCH v2 03/20] nmbug-status: Decode Popen output using the user's locale --- 8d/6de2526416b62c03a5a50108181305aea29957 | 149 ++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 8d/6de2526416b62c03a5a50108181305aea29957 diff --git a/8d/6de2526416b62c03a5a50108181305aea29957 b/8d/6de2526416b62c03a5a50108181305aea29957 new file mode 100644 index 000000000..590da4ef9 --- /dev/null +++ b/8d/6de2526416b62c03a5a50108181305aea29957 @@ -0,0 +1,149 @@ +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 1803F431FAF + for ; Mon, 10 Feb 2014 10:42:11 -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 xrG6UdFxYm2a for ; + Mon, 10 Feb 2014 10:42:03 -0800 (PST) +Received: from qmta08.westchester.pa.mail.comcast.net + (qmta08.westchester.pa.mail.comcast.net [76.96.62.80]) + by olra.theworths.org (Postfix) with ESMTP id 9077B431FD7 + for ; Mon, 10 Feb 2014 10:41:55 -0800 (PST) +Received: from omta05.westchester.pa.mail.comcast.net ([76.96.62.43]) + by qmta08.westchester.pa.mail.comcast.net with comcast + id QgdL1n0050vyq2s58ihuq7; Mon, 10 Feb 2014 18:41:54 +0000 +Received: from odin.tremily.us ([24.18.63.50]) + by omta05.westchester.pa.mail.comcast.net with comcast + id Qiht1n00T152l3L3RihuQu; Mon, 10 Feb 2014 18:41:54 +0000 +Received: from mjolnir.tremily.us (unknown [192.168.0.140]) + by odin.tremily.us (Postfix) with ESMTPS id 3EB3910167A7; + Mon, 10 Feb 2014 10:41:53 -0800 (PST) +Received: (nullmailer pid 1248 invoked by uid 1000); + Mon, 10 Feb 2014 18:40:44 -0000 +From: "W. Trevor King" +To: notmuch@notmuchmail.org +Subject: [PATCH v2 03/20] nmbug-status: Decode Popen output using the user's + locale +Date: Mon, 10 Feb 2014 10:40:24 -0800 +Message-Id: + <9a07face73274cbb977b8a8e4e983e64b0863531.1392056624.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=1392057714; + bh=1aQ15C7VOfb5SpJX2PiuZjmexLCSbY7tx6l56EDHxmQ=; + h=Received:Received:Received:Received:From:To:Subject:Date: + Message-Id; + b=bAKg/W+4GY85Y5zC2WGRsEjQaKEGAvSkoyllB/sJTORWTQBslG2odDp+1wPac03mk + qHaC7yUl/+ovX2e1IYZOsBZlZZUfjbU084969rUFg50a+RlFh8nASFPAjxnAipE9fU + GvKYwhLqDk86K4xSdavatN8jKke9q/dGJHE45WDaGakb4MQq84TqiPgf49ks1vvvaw + OPmzAh6r1f0qE3HnGqJ7lxt6eCFzaMwYvWlL8nRBNdsE9N5ipzuVc0iMTg3oTfoUR+ + gb/4Z2ZnD1PTpX8HpL0coDYHHMhzdHSUvOZY61oYaWTnVjX5dgYP+QCPdI4fYw4+RF + 9jLtQinMZJYIg== +Cc: Tomi Ollila +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, 10 Feb 2014 18:42:11 -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