From: W. Trevor King Date: Sat, 10 May 2014 19:16:38 +0000 (+1700) Subject: [PATCH 1/2] nmbug-status: Clarify errors for illegible configs X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=ccbaa3c5f09ee819fe9a4c0d004e74f60ede2b10;p=notmuch-archives.git [PATCH 1/2] nmbug-status: Clarify errors for illegible configs --- diff --git a/2c/73d1273bbd88449af8a2b4f9f4eb103e107a96 b/2c/73d1273bbd88449af8a2b4f9f4eb103e107a96 new file mode 100644 index 000000000..41fa58bf6 --- /dev/null +++ b/2c/73d1273bbd88449af8a2b4f9f4eb103e107a96 @@ -0,0 +1,191 @@ +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 9808D431FBF + for ; Sat, 10 May 2014 12:17:27 -0700 (PDT) +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 BGMCP752kGtw for ; + Sat, 10 May 2014 12:17:21 -0700 (PDT) +Received: from QMTA11.westchester.pa.mail.comcast.net + (qmta11.westchester.pa.mail.comcast.net [76.96.59.211]) + by olra.theworths.org (Postfix) with ESMTP id 4666B431FC0 + for ; Sat, 10 May 2014 12:17:21 -0700 (PDT) +Received: from omta12.westchester.pa.mail.comcast.net ([76.96.62.44]) + by QMTA11.westchester.pa.mail.comcast.net with comcast + id 0K1n1o0030xGWP85BKHH7N; Sat, 10 May 2014 19:17:17 +0000 +Received: from odin.tremily.us ([24.18.63.50]) + by omta12.westchester.pa.mail.comcast.net with comcast + id 0KHG1o008152l3L3YKHGXJ; Sat, 10 May 2014 19:17:17 +0000 +Received: from mjolnir.tremily.us (unknown [192.168.0.140]) + by odin.tremily.us (Postfix) with ESMTPS id DD217119F4AD; + Sat, 10 May 2014 12:17:15 -0700 (PDT) +Received: (nullmailer pid 7964 invoked by uid 1000); + Sat, 10 May 2014 19:16:43 -0000 +From: "W. Trevor King" +To: notmuch@notmuchmail.org +Subject: [PATCH 1/2] nmbug-status: Clarify errors for illegible configs +Date: Sat, 10 May 2014 12:16:38 -0700 +Message-Id: + <1c786e115a812384a8b397583027cf89316253cb.1399749244.git.wking@tremily.us> +X-Mailer: git-send-email 1.9.1.353.gc66d89d +In-Reply-To: +References: +In-Reply-To: +References: +DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=comcast.net; + s=q20140121; t=1399749437; + bh=Y6e1Qnl8mwd0e87DRQAmuwxtsjiw5fDa2eESfCs8cMU=; + h=Received:Received:Received:Received:From:To:Subject:Date: + Message-Id; + b=R6menPQ+vS6Kjj8UwbryJvUT2aBeW7FPNW3e5Ae/RiK19MKujIKrws2W0cElEeA0b + 8rv75kp2vBJXqvYsLwTpn9OD6gdw0sbdWKe+a+Gwpe+YzKLNzk4P9Ir6z7FDfkhdxr + gkF2pWYEJEWu7eTYex1s8AImiiGkoqIhPnj7Rexgk5Xj168yt+7SedyltpFXZVxxr1 + 0/bDs/Bjufhv7YtBcoivEzScwKSV0rP2sqwTsLoA6aXyAjGGHhw/Xt19EVIe5i4sSQ + LZ1+79OnvFrnfr1cA5dLsIdgLHPfpLdVqe/YdPVpMpL8VxAaPQUnthbZ1m0H78lP2A + RC4A62MAYOZAQ== +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: Sat, 10 May 2014 19:17:27 -0000 + +Carl Worth pointed out that errors like: + + $ ./nmbug-status + fatal: Not a git repository: '/home/cworth/.nmbug' + fatal: Not a git repository: '/home/cworth/.nmbug' + Traceback (most recent call last): + File "./nmbug-status", line 254, in + config = read_config(path=args.config) + File "./nmbug-status", line 73, in read_config + return json.load(fp) + File "/usr/lib/python2.7/json/__init__.py", line 290, in load + **kw) + File "/usr/lib/python2.7/json/__init__.py", line 338, in loads + return _default_decoder.decode(s) + File "/usr/lib/python2.7/json/decoder.py", line 366, in decode + obj, end = self.raw_decode(s, idx=_w(s, 0).end()) + File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode + raise ValueError("No JSON object could be decoded") + ValueError: No JSON object could be decoded + +are not particularly clear. With this commit, we'll get output like: + + $ ./nmbug-status + fatal: Not a git repository: '/home/wking/.nmbug' + No local branch 'config' in /home/wking/.nmbug. Checkout a local + config branch or explicitly set --config. + +which is much more accessible. I've also added user-friendly messages +for a number of other config-parsing errors. +--- + devel/nmbug/nmbug-status | 54 ++++++++++++++++++++++++++++++++++++++++-------- + 1 file changed, 45 insertions(+), 9 deletions(-) + +diff --git a/devel/nmbug/nmbug-status b/devel/nmbug/nmbug-status +index 03621bd..75a6e47 100755 +--- a/devel/nmbug/nmbug-status ++++ b/devel/nmbug/nmbug-status +@@ -49,28 +49,60 @@ if not hasattr(collections, 'OrderedDict'): # Python 2.6 or earlier + collections.OrderedDict = _OrderedDict + + ++class ConfigError (Exception): ++ """Errors with config file usage ++ """ ++ pass ++ ++ + def read_config(path=None, encoding=None): + "Read config from json file" + if not encoding: + encoding = _ENCODING + if path: +- fp = open(path) ++ try: ++ with open(path, 'rb') as f: ++ config_bytes = f.read() ++ except IOError as e: ++ raise ConfigError('Could not read config from {}'.format(path)) + else: + nmbhome = os.getenv('NMBGIT', os.path.expanduser('~/.nmbug')) ++ branch = 'config' ++ filename = 'status-config.json' + + # read only the first line from the pipe + sha1_bytes = subprocess.Popen( +- ['git', '--git-dir', nmbhome, 'show-ref', '-s', 'config'], ++ ['git', '--git-dir', nmbhome, 'show-ref', '-s', branch], + stdout=subprocess.PIPE).stdout.readline() + sha1 = sha1_bytes.decode(encoding).rstrip() ++ if not sha1: ++ raise ConfigError( ++ ("No local branch '{branch}' in {nmbgit}. " ++ 'Checkout a local {branch} branch or explicitly set --config.' ++ ).format(branch=branch, nmbgit=nmbhome)) + +- fp_byte_stream = subprocess.Popen( ++ p = 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) +- +- return json.load(fp) ++ '{}:{}'.format(sha1, filename)], ++ stdout=subprocess.PIPE) ++ config_bytes, err = p.communicate() ++ status = p.wait() ++ if status != 0: ++ raise ConfigError( ++ ("Missing status-config.json in branch '{branch}' of" ++ '{nmbgit}. Add the file or explicitly set --config.' ++ ).format(branch=branch, nmbgit=nmbhome)) ++ ++ config_json = config_bytes.decode(encoding) ++ try: ++ return json.loads(config_json) ++ except ValueError as e: ++ if not path: ++ path = "{} in branch '{}' of {}".format( ++ filename, branch, nmbhome) ++ raise ConfigError( ++ 'Could not parse JSON from the config file {}:\n{}'.format( ++ path, e)) + + + class Thread (list): +@@ -254,7 +286,11 @@ parser.add_argument('--get-query', help='get query for view', + + args = parser.parse_args() + +-config = read_config(path=args.config) ++try: ++ config = read_config(path=args.config) ++except ConfigError as e: ++ print(e) ++ sys.exit(1) + + _PAGES['text'] = Page() + _PAGES['html'] = HtmlPage( +-- +1.9.1.353.gc66d89d +