Close all open filehandle (#50544)

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde
2019-01-11 20:44:08 +05:30
committed by Brian Coca
parent 94a1d86d70
commit db8702cdb8
21 changed files with 81 additions and 47 deletions

View File

@@ -623,7 +623,8 @@ def main():
stack_params['StackName'] = module.params['stack_name']
if module.params['template'] is not None:
stack_params['TemplateBody'] = open(module.params['template'], 'r').read()
with open(module.params['template'], 'r') as template_fh:
stack_params['TemplateBody'] = template_fh.read()
elif module.params['template_body'] is not None:
stack_params['TemplateBody'] = module.params['template_body']
elif module.params['template_url'] is not None:
@@ -636,7 +637,8 @@ def main():
# can't check the policy when verifying.
if module.params['stack_policy'] is not None and not module.check_mode and not module.params['create_changeset']:
stack_params['StackPolicyBody'] = open(module.params['stack_policy'], 'r').read()
with open(module.params['stack_policy'], 'r') as stack_policy_fh:
stack_params['StackPolicyBody'] = stack_policy_fh.read()
template_parameters = module.params['template_parameters']

View File

@@ -218,11 +218,14 @@ def cert_action(module, iam, name, cpath, new_name, new_path, state,
def load_data(cert, key, cert_chain):
# if paths are provided rather than lookups read the files and return the contents
if cert and os.path.isfile(cert):
cert = open(cert, 'r').read().rstrip()
with open(cert, 'r') as cert_fh:
cert = cert_fh.read().rstrip()
if key and os.path.isfile(key):
key = open(key, 'r').read().rstrip()
with open(key, 'r') as key_fh:
key = key_fh.read().rstrip()
if cert_chain and os.path.isfile(cert_chain):
cert_chain = open(cert_chain, 'r').read()
with open(cert_chain, 'r') as cert_chain_fh:
cert_chain = cert_chain_fh.read()
return cert, key, cert_chain

View File

@@ -115,8 +115,8 @@ def main():
public_key = module.params['public_key']
if module.params['public_key_file']:
public_key = open(module.params['public_key_file']).read()
public_key = public_key.rstrip()
with open(module.params['public_key_file']) as public_key_fh:
public_key = public_key_fh.read().rstrip()
sdk, cloud = openstack_cloud_from_module(module)
try:

View File

@@ -255,8 +255,9 @@ class Pkcs(crypto_utils.OpenSSLObject):
try:
self.remove()
p12 = crypto.load_pkcs12(open(self.src, 'rb').read(),
with open(self.src, 'rb') as pkcs12_fh:
pkcs12_content = pkcs12_fh.read()
p12 = crypto.load_pkcs12(pkcs12_content,
self.passphrase)
pkey = crypto.dump_privatekey(crypto.FILETYPE_PEM,
p12.get_privatekey())

View File

@@ -170,7 +170,8 @@ class PublicKey(crypto_utils.OpenSSLObject):
if not self.check(module, perms_required=False) or self.force:
try:
if self.format == 'OpenSSH':
privatekey_content = open(self.privatekey_path, 'rb').read()
with open(self.privatekey_path, 'rb') as private_key_fh:
privatekey_content = private_key_fh.read()
key = crypto_serialization.load_pem_private_key(privatekey_content,
password=self.privatekey_passphrase,
backend=default_backend())
@@ -212,7 +213,8 @@ class PublicKey(crypto_utils.OpenSSLObject):
return False
try:
publickey_content = open(self.path, 'rb').read()
with open(self.path, 'rb') as public_key_fh:
publickey_content = public_key_fh.read()
if self.format == 'OpenSSH':
current_publickey = crypto_serialization.load_ssh_public_key(publickey_content, backend=default_backend())
publickey_content = current_publickey.public_bytes(crypto_serialization.Encoding.PEM,

View File

@@ -128,7 +128,8 @@ def assemble_from_fragments(src_path, delimiter=None, compiled_regexp=None, igno
fragment = os.path.join(src_path, f)
if not os.path.isfile(fragment) or (ignore_hidden and os.path.basename(fragment).startswith('.')):
continue
fragment_content = open(fragment, 'rb').read()
with open(fragment, 'rb') as fragment_fh:
fragment_content = fragment_fh.read()
# always put a newline between fragments if the previous fragment didn't end with a newline.
if add_newline:

View File

@@ -170,7 +170,9 @@ ZIP_FILE_MODE_RE = re.compile(r'([r-][w-][SsTtx-]){3}')
def crc32(path):
''' Return a CRC32 checksum of a file '''
return binascii.crc32(open(path, 'rb').read()) & 0xffffffff
with open(path, 'rb') as f:
file_content = f.read()
return binascii.crc32(file_content) & 0xffffffff
def shell_escape(string):

View File

@@ -79,7 +79,9 @@ def main():
if not os.access(source, os.R_OK):
module.fail_json(msg="file is not readable: %s" % source)
data = base64.b64encode(open(source, 'rb').read())
with open(source, 'rb') as source_fh:
source_content = source_fh.read()
data = base64.b64encode(source_content)
module.exit_json(content=data, source=source, encoding='base64')

View File

@@ -108,7 +108,8 @@ def get_matching_jobs(module, at_cmd, script_file):
return matching_jobs
# Read script_file into a string.
script_file_string = open(script_file).read().strip()
with open(script_file) as script_fh:
script_file_string = script_fh.read().strip()
# Loop through the jobs.
# If the script text is contained in a job add job number to list.

View File

@@ -686,12 +686,15 @@ class LinuxService(Service):
override_file_name = "%s/%s.override" % (initpath, self.name)
# Check to see if files contain the manual line in .conf and fail if True
if manreg.search(open(conf_file_name).read()):
with open(conf_file_name) as conf_file_fh:
conf_file_content = conf_file_fh.read()
if manreg.search(conf_file_content):
self.module.fail_json(msg="manual stanza not supported in a .conf file")
self.changed = False
if os.path.exists(override_file_name):
override_file_contents = open(override_file_name).read()
with open(override_file_name) as override_fh:
override_file_contents = override_fh.read()
# Remove manual stanza if present and service enabled
if self.enable and manreg.search(override_file_contents):
self.changed = True

View File

@@ -77,8 +77,8 @@ def main():
data = None
try:
data = open(log_path).read()
data = json.loads(data)
with open(log_path) as f:
data = json.loads(f.read())
except Exception:
if not data:
# file not written yet? That means it is running

View File

@@ -430,8 +430,9 @@ class JenkinsPlugin(object):
md5sum_old = None
if os.path.isfile(plugin_file):
# Make the checksum of the currently installed plugin
md5sum_old = hashlib.md5(
open(plugin_file, 'rb').read()).hexdigest()
with open(plugin_file, 'rb') as md5_plugin_fh:
md5_plugin_content = md5_plugin_fh.read()
md5sum_old = hashlib.md5(md5_plugin_content).hexdigest()
if self.params['version'] in [None, 'latest']:
# Take latest version
@@ -482,7 +483,9 @@ class JenkinsPlugin(object):
plugin_data = self._download_updates()
try:
sha1_old = hashlib.sha1(open(plugin_file, 'rb').read())
with open(plugin_file, 'rb') as sha1_plugin_fh:
sha1_plugin_content = sha1_plugin_fh.read()
sha1_old = hashlib.sha1(sha1_plugin_content)
except Exception as e:
self.module.fail_json(
msg="Cannot calculate SHA1 of the old plugin.",