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 EC85B13877A for ; Mon, 16 Jun 2014 03:37:41 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id A7422E0BB1; Mon, 16 Jun 2014 03:37:35 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 6FD59E0BAB for ; Mon, 16 Jun 2014 03:37:33 +0000 (UTC) Received: from spoonbill.gentoo.org (spoonbill.gentoo.org [81.93.255.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 355DB33FFF1 for ; Mon, 16 Jun 2014 03:37:32 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by spoonbill.gentoo.org (Postfix) with ESMTP id 153ED18873 for ; Mon, 16 Jun 2014 03:37:30 +0000 (UTC) From: "Brian Dolbec" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Brian Dolbec" Message-ID: <1402792601.b47238ef6ef407c7c07a94b885b7243a089c797f.dol-sen@gentoo> Subject: [gentoo-commits] proj/layman:gsoc2014 commit in: layman/overlays/, layman/, layman/tests/ X-VCS-Repository: proj/layman X-VCS-Files: layman/dbbase.py layman/makeconf.py layman/overlays/tar.py layman/remotedb.py layman/reposconf.py layman/tests/external.py X-VCS-Directories: layman/overlays/ layman/ layman/tests/ X-VCS-Committer: dol-sen X-VCS-Committer-Name: Brian Dolbec X-VCS-Revision: b47238ef6ef407c7c07a94b885b7243a089c797f X-VCS-Branch: gsoc2014 Date: Mon, 16 Jun 2014 03:37:30 +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: 3a0f08d4-035b-4cae-a1ff-1a2aeeb1b2c4 X-Archives-Hash: 2a8d9dabc68c50bd1a0f0af6f395578e commit: b47238ef6ef407c7c07a94b885b7243a089c797f Author: Devan Franchini gentoo org> AuthorDate: Thu Jun 12 21:17:02 2014 +0000 Commit: Brian Dolbec gmail com> CommitDate: Sun Jun 15 00:36:41 2014 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/layman.git;a=commit;h=b47238ef Implements the "with" syntax to open files --- layman/dbbase.py | 10 +++++----- layman/makeconf.py | 16 +++++----------- layman/overlays/tar.py | 9 +++++---- layman/remotedb.py | 14 ++++++-------- layman/reposconf.py | 6 ++++-- layman/tests/external.py | 5 ++--- 6 files changed, 27 insertions(+), 33 deletions(-) diff --git a/layman/dbbase.py b/layman/dbbase.py index 92d5cf9..e17e8f3 100644 --- a/layman/dbbase.py +++ b/layman/dbbase.py @@ -131,8 +131,8 @@ class DbBase(object): '''Read the overlay definition file.''' try: - df = fileopen(path, 'r') - document = df.read() + with fileopen(path, 'r') as df: + document = df.read() except Exception as error: if not self.ignore_init_read_errors: @@ -235,9 +235,9 @@ class DbBase(object): indent(tree) tree = ET.ElementTree(tree) try: - f = fileopen(path, 'w') - tree.write(f, encoding=_UNICODE) - f.close() + with fileopen(path, 'w') as f: + tree.write(f, encoding=_UNICODE) + except Exception as error: raise Exception('Failed to write to local overlays file: ' + path + '\nError was:\n' + str(error)) diff --git a/layman/makeconf.py b/layman/makeconf.py index f1eba09..15ad537 100644 --- a/layman/makeconf.py +++ b/layman/makeconf.py @@ -22,7 +22,7 @@ import codecs import re from layman.utils import path -from layman.compatibility import cmp_to_key +from layman.compatibility import cmp_to_key, fileopen #=============================================================================== # @@ -276,11 +276,8 @@ class ConfigHandler: return False try: - make_conf = codecs.open(self.path, 'w', 'utf-8') - - make_conf.write(content) - - make_conf.close() + with fileopen(self.path, 'w') as make_conf: + make_conf.write(content) except Exception as error: self.output.error('MakeConf: ConfigHandler.write(); Failed to write "'\ @@ -293,11 +290,8 @@ class ConfigHandler: Returns the content of the /var/lib/layman/make.conf file. ''' try: - make_conf = codecs.open(self.path, 'r', 'utf-8') - - self.data = make_conf.read() - - make_conf.close() + with fileopen(self.path, 'r') as make_conf: + self.data = make_conf.read() except Exception as error: self.output.error('ConfigHandler: content(); Failed to read "'\ diff --git a/layman/overlays/tar.py b/layman/overlays/tar.py index acbeece..fc15c56 100644 --- a/layman/overlays/tar.py +++ b/layman/overlays/tar.py @@ -34,8 +34,9 @@ import tempfile import xml.etree.ElementTree as ET # Python 2.5 -from layman.utils import path +from layman.compatibility import fileopen from layman.overlays.source import OverlaySource, require_supported +from layman.utils import path from layman.version import VERSION from sslfetch.connections import Connector @@ -120,9 +121,9 @@ class TarOverlay(OverlaySource): pkg = path([base, self.parent.name + ext]) try: - out_file = open(pkg, 'w+b') - out_file.write(tar) - out_file.close() + with fileopen(pkg, 'w+b') as out_file: + out_file.write(tar) + except Exception as error: raise Exception('Failed to store tar package in ' + pkg + '\nError was:' + str(error)) diff --git a/layman/remotedb.py b/layman/remotedb.py index f883799..79f4ec6 100644 --- a/layman/remotedb.py +++ b/layman/remotedb.py @@ -17,7 +17,6 @@ '''Handles different storage files.''' from __future__ import unicode_literals -from __future__ import with_statement __version__ = "$Id: db.py 309 2007-04-09 16:23:38Z wrobel $" @@ -261,8 +260,9 @@ class RemoteDB(DbBase): if url_timestamp != timestamp: self.output.debug('RemoteDB._fetch_file() opening file', 2) # Fetch the remote list - with open(filepath) as connection: + with fileopen(filepath) as connection: olist = connection.read() + else: self.output.info('Remote list already up to date: %s' % url, 4) @@ -324,14 +324,12 @@ class RemoteDB(DbBase): def write_cache(olist, mpath, tpath=None, timestamp=None): has_updates = False try: - out_file = fileopen(mpath, 'w') - out_file.write(olist) - out_file.close() + with fileopen(mpath, 'w') as out_file: + out_file.write(olist) if timestamp is not None and tpath is not None: - out_file = fileopen(tpath, 'w') - out_file.write(str(timestamp)) - out_file.close() + with fileopen(tpath, 'w') as out_file: + out_file.write(str(timestamp)) has_updates = True diff --git a/layman/reposconf.py b/layman/reposconf.py index a7a0166..c550a13 100644 --- a/layman/reposconf.py +++ b/layman/reposconf.py @@ -24,7 +24,8 @@ except: # Import for Python2 import ConfigParser -from layman.utils import path +from layman.compatibility import fileopen +from layman.utils import path class ConfigHandler: @@ -117,8 +118,9 @@ class ConfigHandler: @return boolean: represents a successful write. ''' try: - with open(self.path, 'w') as laymanconf: + with fileopen(self.path, 'w') as laymanconf: self.repo_conf.write(laymanconf) + return True except IOError as error: self.output.error('ReposConf: ConfigHandler.write(); Failed to write "'\ diff --git a/layman/tests/external.py b/layman/tests/external.py index 3505eeb..82825e2 100755 --- a/layman/tests/external.py +++ b/layman/tests/external.py @@ -99,9 +99,8 @@ class TarAddRemoveSync(unittest.TestCase): """ % { 'temp_tarball_url':urllib.pathname2url(temp_tarball_path), 'repo_name':repo_name} (fd, temp_collection_path) = tempfile.mkstemp() - f = os.fdopen(fd, 'w') - f.write(xml_text) - f.close() + with os.fdopen(fd, 'w') as f: + f.write(xml_text) # Make playground directory temp_dir_path = tempfile.mkdtemp() 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 D281113877A for ; Mon, 16 Jun 2014 03:40:32 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 0F010E0BD0; Mon, 16 Jun 2014 03:40:25 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 40227E0BCC for ; Mon, 16 Jun 2014 03:40:23 +0000 (UTC) Received: from spoonbill.gentoo.org (spoonbill.gentoo.org [81.93.255.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id EAEDF33FDB5 for ; Mon, 16 Jun 2014 03:40:21 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by spoonbill.gentoo.org (Postfix) with ESMTP id ED5C218873 for ; Mon, 16 Jun 2014 03:40:19 +0000 (UTC) From: "Brian Dolbec" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Brian Dolbec" Message-ID: <1402792601.b47238ef6ef407c7c07a94b885b7243a089c797f.dol-sen@gentoo> Subject: [gentoo-commits] proj/layman:master commit in: layman/overlays/, layman/, layman/tests/ X-VCS-Repository: proj/layman X-VCS-Files: layman/dbbase.py layman/makeconf.py layman/overlays/tar.py layman/remotedb.py layman/reposconf.py layman/tests/external.py X-VCS-Directories: layman/overlays/ layman/ layman/tests/ X-VCS-Committer: dol-sen X-VCS-Committer-Name: Brian Dolbec X-VCS-Revision: b47238ef6ef407c7c07a94b885b7243a089c797f X-VCS-Branch: master Date: Mon, 16 Jun 2014 03:40:19 +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: ab734280-ee09-49bd-a658-ee9cd0102531 X-Archives-Hash: 465f4df88ca2f88d8a170d135ba16abf Message-ID: <20140616034019.rH2ODvglvBljKF7k_uKCfw6bY_P-yvPZixT4nf-RMbE@z> commit: b47238ef6ef407c7c07a94b885b7243a089c797f Author: Devan Franchini gentoo org> AuthorDate: Thu Jun 12 21:17:02 2014 +0000 Commit: Brian Dolbec gmail com> CommitDate: Sun Jun 15 00:36:41 2014 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/layman.git;a=commit;h=b47238ef Implements the "with" syntax to open files --- layman/dbbase.py | 10 +++++----- layman/makeconf.py | 16 +++++----------- layman/overlays/tar.py | 9 +++++---- layman/remotedb.py | 14 ++++++-------- layman/reposconf.py | 6 ++++-- layman/tests/external.py | 5 ++--- 6 files changed, 27 insertions(+), 33 deletions(-) diff --git a/layman/dbbase.py b/layman/dbbase.py index 92d5cf9..e17e8f3 100644 --- a/layman/dbbase.py +++ b/layman/dbbase.py @@ -131,8 +131,8 @@ class DbBase(object): '''Read the overlay definition file.''' try: - df = fileopen(path, 'r') - document = df.read() + with fileopen(path, 'r') as df: + document = df.read() except Exception as error: if not self.ignore_init_read_errors: @@ -235,9 +235,9 @@ class DbBase(object): indent(tree) tree = ET.ElementTree(tree) try: - f = fileopen(path, 'w') - tree.write(f, encoding=_UNICODE) - f.close() + with fileopen(path, 'w') as f: + tree.write(f, encoding=_UNICODE) + except Exception as error: raise Exception('Failed to write to local overlays file: ' + path + '\nError was:\n' + str(error)) diff --git a/layman/makeconf.py b/layman/makeconf.py index f1eba09..15ad537 100644 --- a/layman/makeconf.py +++ b/layman/makeconf.py @@ -22,7 +22,7 @@ import codecs import re from layman.utils import path -from layman.compatibility import cmp_to_key +from layman.compatibility import cmp_to_key, fileopen #=============================================================================== # @@ -276,11 +276,8 @@ class ConfigHandler: return False try: - make_conf = codecs.open(self.path, 'w', 'utf-8') - - make_conf.write(content) - - make_conf.close() + with fileopen(self.path, 'w') as make_conf: + make_conf.write(content) except Exception as error: self.output.error('MakeConf: ConfigHandler.write(); Failed to write "'\ @@ -293,11 +290,8 @@ class ConfigHandler: Returns the content of the /var/lib/layman/make.conf file. ''' try: - make_conf = codecs.open(self.path, 'r', 'utf-8') - - self.data = make_conf.read() - - make_conf.close() + with fileopen(self.path, 'r') as make_conf: + self.data = make_conf.read() except Exception as error: self.output.error('ConfigHandler: content(); Failed to read "'\ diff --git a/layman/overlays/tar.py b/layman/overlays/tar.py index acbeece..fc15c56 100644 --- a/layman/overlays/tar.py +++ b/layman/overlays/tar.py @@ -34,8 +34,9 @@ import tempfile import xml.etree.ElementTree as ET # Python 2.5 -from layman.utils import path +from layman.compatibility import fileopen from layman.overlays.source import OverlaySource, require_supported +from layman.utils import path from layman.version import VERSION from sslfetch.connections import Connector @@ -120,9 +121,9 @@ class TarOverlay(OverlaySource): pkg = path([base, self.parent.name + ext]) try: - out_file = open(pkg, 'w+b') - out_file.write(tar) - out_file.close() + with fileopen(pkg, 'w+b') as out_file: + out_file.write(tar) + except Exception as error: raise Exception('Failed to store tar package in ' + pkg + '\nError was:' + str(error)) diff --git a/layman/remotedb.py b/layman/remotedb.py index f883799..79f4ec6 100644 --- a/layman/remotedb.py +++ b/layman/remotedb.py @@ -17,7 +17,6 @@ '''Handles different storage files.''' from __future__ import unicode_literals -from __future__ import with_statement __version__ = "$Id: db.py 309 2007-04-09 16:23:38Z wrobel $" @@ -261,8 +260,9 @@ class RemoteDB(DbBase): if url_timestamp != timestamp: self.output.debug('RemoteDB._fetch_file() opening file', 2) # Fetch the remote list - with open(filepath) as connection: + with fileopen(filepath) as connection: olist = connection.read() + else: self.output.info('Remote list already up to date: %s' % url, 4) @@ -324,14 +324,12 @@ class RemoteDB(DbBase): def write_cache(olist, mpath, tpath=None, timestamp=None): has_updates = False try: - out_file = fileopen(mpath, 'w') - out_file.write(olist) - out_file.close() + with fileopen(mpath, 'w') as out_file: + out_file.write(olist) if timestamp is not None and tpath is not None: - out_file = fileopen(tpath, 'w') - out_file.write(str(timestamp)) - out_file.close() + with fileopen(tpath, 'w') as out_file: + out_file.write(str(timestamp)) has_updates = True diff --git a/layman/reposconf.py b/layman/reposconf.py index a7a0166..c550a13 100644 --- a/layman/reposconf.py +++ b/layman/reposconf.py @@ -24,7 +24,8 @@ except: # Import for Python2 import ConfigParser -from layman.utils import path +from layman.compatibility import fileopen +from layman.utils import path class ConfigHandler: @@ -117,8 +118,9 @@ class ConfigHandler: @return boolean: represents a successful write. ''' try: - with open(self.path, 'w') as laymanconf: + with fileopen(self.path, 'w') as laymanconf: self.repo_conf.write(laymanconf) + return True except IOError as error: self.output.error('ReposConf: ConfigHandler.write(); Failed to write "'\ diff --git a/layman/tests/external.py b/layman/tests/external.py index 3505eeb..82825e2 100755 --- a/layman/tests/external.py +++ b/layman/tests/external.py @@ -99,9 +99,8 @@ class TarAddRemoveSync(unittest.TestCase): """ % { 'temp_tarball_url':urllib.pathname2url(temp_tarball_path), 'repo_name':repo_name} (fd, temp_collection_path) = tempfile.mkstemp() - f = os.fdopen(fd, 'w') - f.write(xml_text) - f.close() + with os.fdopen(fd, 'w') as f: + f.write(xml_text) # Make playground directory temp_dir_path = tempfile.mkdtemp()