From 1d5e2833fe4251967fe4ee73b14fdfc49f3bfb2e Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Wed, 26 Nov 2014 19:09:00 +0000 Subject: [PATCH] Switch `open()` for `salt.utils.fopen()` --- salt/modules/zcbuildout.py | 70 +++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/salt/modules/zcbuildout.py b/salt/modules/zcbuildout.py index 37443c5bbd..a28537f8a7 100644 --- a/salt/modules/zcbuildout.py +++ b/salt/modules/zcbuildout.py @@ -28,14 +28,6 @@ You have those following methods: __virtualname__ = 'buildout' -def __virtual__(): - ''' - Only load if buildout libs are present - ''' - if True: - return __virtualname__ - return False - # Import python libs import os import re @@ -46,6 +38,7 @@ import copy import urllib2 # Import salt libs +import salt.utils from salt.exceptions import CommandExecutionError from salt._compat import string_types @@ -72,6 +65,13 @@ DEFAULT_VER = 2 _logger = logging.getLogger(__name__) +def __virtual__(): + ''' + Only load if buildout libs are present + ''' + return __virtualname__ + + def _salt_callback(func, **kwargs): LOG.clear() @@ -386,11 +386,10 @@ def _get_bootstrap_content(directory='.'): Get the current bootstrap.py script content ''' try: - fic = open( - os.path.join( - os.path.abspath(directory), 'bootstrap.py')) - oldcontent = fic.read() - fic.close() + with salt.utils.fopen(os.path.join( + os.path.abspath(directory), + 'bootstrap.py')) as fic: + oldcontent = fic.read() except (OSError, IOError): oldcontent = '' return oldcontent @@ -412,16 +411,15 @@ def _get_buildout_ver(directory='.'): try: files = _find_cfgs(directory) for f in files: - fic = open(f) - buildout1re = re.compile(r'^zc\.buildout\s*=\s*1', RE_F) - dfic = fic.read() - if ( - ('buildout.dumppick' in dfic) - or - (buildout1re.search(dfic)) - ): - buildoutver = 1 - fic.close() + with salt.utils.fopen(f) as fic: + buildout1re = re.compile(r'^zc\.buildout\s*=\s*1', RE_F) + dfic = fic.read() + if ( + ('buildout.dumppick' in dfic) + or + (buildout1re.search(dfic)) + ): + buildoutver = 1 bcontent = _get_bootstrap_content(directory) if ( '--download-base' in bcontent @@ -515,7 +513,7 @@ def upgrade_bootstrap(directory='.', if not os.path.isdir(dbuild): os.makedirs(dbuild) # only try to download once per buildout checkout - open(os.path.join( + salt.utils.fopen(os.path.join( dbuild, '{0}.updated_bootstrap'.format(buildout_ver))) except (OSError, IOError): @@ -530,20 +528,17 @@ def upgrade_bootstrap(directory='.', data = '\n'.join(ldata) if updated: comment = 'Bootstrap updated' - fic = open(b_py, 'w') - fic.write(data) - fic.close() + with salt.utils.fopen(b_py, 'w') as fic: + fic.write(data) if dled: - afic = open(os.path.join( - dbuild, '{0}.updated_bootstrap'.format(buildout_ver) - ), 'w') - afic.write('foo') - afic.close() + with salt.utils.fopen(os.path.join(dbuild, + '{0}.updated_bootstrap'.format( + buildout_ver)), 'w') as afic: + afic.write('foo') except (OSError, IOError): if oldcontent: - fic = open(b_py, 'w') - fic.write(oldcontent) - fic.close() + with salt.utils.fopen(b_py, 'w') as fic: + fic.write(oldcontent) return {'comment': comment} @@ -733,9 +728,8 @@ def bootstrap(directory='.', buildout_ver=buildout_ver) # be sure which buildout bootstrap we have b_py = os.path.join(directory, 'bootstrap.py') - fic = open(b_py) - content = fic.read() - fic.close() + with salt.utils.fopen(b_py) as fic: + content = fic.read() if ( (False != test_release) and ' --accept-buildout-test-releases' in content