From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 0CAAE1381FE for ; Tue, 7 Aug 2012 08:50:42 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id DC78EE0783; Tue, 7 Aug 2012 08:50:29 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 9A9EEE0783 for ; Tue, 7 Aug 2012 08:50:29 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id CA38F1B4039 for ; Tue, 7 Aug 2012 08:50:28 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id 8A767E5446 for ; Tue, 7 Aug 2012 08:50:26 +0000 (UTC) From: "André Erdmann" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "André Erdmann" Message-ID: <1344329359.9692add5a980cd10bd8e4841c9b8e55aa46a8d91.dywi@gentoo> Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/rpackage/ X-VCS-Repository: proj/R_overlay X-VCS-Files: roverlay/rpackage/descriptionreader.py X-VCS-Directories: roverlay/rpackage/ X-VCS-Committer: dywi X-VCS-Committer-Name: André Erdmann X-VCS-Revision: 9692add5a980cd10bd8e4841c9b8e55aa46a8d91 X-VCS-Branch: master Date: Tue, 7 Aug 2012 08:50:26 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: dd0cbc51-6620-4f85-9b55-539a37cb5b40 X-Archives-Hash: 9e7eb6a8cd34bc93d1dd5a4461829339 commit: 9692add5a980cd10bd8e4841c9b8e55aa46a8d91 Author: André Erdmann mailerd de> AuthorDate: Tue Aug 7 08:49:19 2012 +0000 Commit: André Erdmann mailerd de> CommitDate: Tue Aug 7 08:49:19 2012 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=9692add5 descriptionreader: use bytes_try_decode() from strutil --- roverlay/rpackage/descriptionreader.py | 61 +++++++++++++++++-------------- 1 files changed, 33 insertions(+), 28 deletions(-) diff --git a/roverlay/rpackage/descriptionreader.py b/roverlay/rpackage/descriptionreader.py index a5e645e..354ac94 100644 --- a/roverlay/rpackage/descriptionreader.py +++ b/roverlay/rpackage/descriptionreader.py @@ -9,6 +9,7 @@ __all__ = [ 'DescriptionReader', 'make_desc_packageinfo', ] import re +import sys import tarfile import os.path import time @@ -207,38 +208,42 @@ class DescriptionReader ( object ): "Starting to read file {f!r} ...\n".format ( f=filepath ) ) - if not ( isinstance ( filepath, str ) and filepath ): - raise Exception ( "bad usage" ) - - # read describes how to import the lines from a file (e.g. rstrip()) - # fh, th are file/tar handles - read = th = fh = None + try: + # read describes how to import the lines from a file (e.g. rstrip()) + # fh, th are file/tar handles + read = th = fh = None + + if tarfile.is_tarfile ( filepath ): + # filepath is a tarball, open tar handle + file handle + th = tarfile.open ( filepath, mode='r' ) + if pkg_name: + fh = th.extractfile ( + pkg_name + os.path.sep + \ + config.get ( 'DESCRIPTION.file_name' ) + ) + else: + fh = th.extractfile ( config.get ( 'DESCRIPTION.file_name' ) ) - if tarfile.is_tarfile ( filepath ): - # filepath is a tarball, open tar handle + file handle - th = tarfile.open ( filepath, mode='r' ) - if pkg_name: - fh = th.extractfile ( - pkg_name + os.path.sep + config.get ( 'DESCRIPTION.file_name' ) + else: + # open file handle only + # COULDFIX: .Z compressed tar files could be opened here + fh = open ( filepath, 'r' ) + + if sys.version_info >= ( 3, ): + # decode lines of they're only bytes, using isinstance ( <>, str ) + # 'cause isinstance ( , bytes ) returns True + # FIXME: encoding is unknown, could be ascii/iso8859*/utf8 + read_lines = tuple ( + strutil.bytes_try_decode ( l ).rstrip() for l in fh.readlines() ) else: - fh = th.extractfile ( config.get ( 'DESCRIPTION.file_name' ) ) - - else: - # open file handle only - # COULDFIX: .Z compressed tar files could be opened here - fh = open ( filepath, 'r' ) - - # decode lines of they're only bytes, using isinstance ( <>, str ) - # 'cause isinstance ( , bytes ) returns True - read_lines = tuple ( - ( line if isinstance ( line, str ) else line.decode() ).rstrip() - for line in fh.readlines() - ) + # python2 shouldn't need special decoding + read_lines = tuple ( l.rstrip() for l in fh.readlines() ) - fh.close() - if not th is None: th.close() - del fh, th + finally: + if 'fh' in locals() and fh: fh.close() + if 'th' in locals() and th: th.close() + del fh, th if read_lines and hasattr ( self, 'write_desc_file' ): try: