mirror of
https://github.com/saltstack/salt.git
synced 2025-04-16 17:50:20 +00:00
Use with blocks (or try/finally) to ensure filehandles are closed
This fixes a few sources (or potential sources) of filehandle leaks.
This commit is contained in:
parent
8e99aeed6a
commit
8a7d56369c
5 changed files with 55 additions and 47 deletions
|
@ -9,6 +9,9 @@ import os
|
|||
import logging
|
||||
import pickle
|
||||
|
||||
# Import Salt libs
|
||||
import salt.utils.files
|
||||
|
||||
# This must be present or the Salt loader won't load this module
|
||||
__proxyenabled__ = ['dummy']
|
||||
|
||||
|
@ -35,16 +38,14 @@ def __virtual__():
|
|||
|
||||
|
||||
def _save_state(details):
|
||||
pck = open(FILENAME, 'wb') # pylint: disable=W8470
|
||||
pickle.dump(details, pck)
|
||||
pck.close()
|
||||
with salt.utils.files.fopen(FILENAME, 'wb') as pck:
|
||||
pickle.dump(details, pck)
|
||||
|
||||
|
||||
def _load_state():
|
||||
try:
|
||||
pck = open(FILENAME, 'r') # pylint: disable=W8470
|
||||
DETAILS = pickle.load(pck)
|
||||
pck.close()
|
||||
with salt.utils.files.fopen(FILENAME, 'r') as pck:
|
||||
DETAILS = pickle.load(pck)
|
||||
except IOError:
|
||||
DETAILS = {}
|
||||
DETAILS['initialized'] = False
|
||||
|
|
|
@ -55,50 +55,52 @@ def parse_gitlog(filename=None):
|
|||
else:
|
||||
fh = open(filename, 'r+')
|
||||
|
||||
commitcount = 0
|
||||
for line in fh.readlines():
|
||||
line = line.rstrip()
|
||||
if line.startswith('commit '):
|
||||
new_commit = True
|
||||
commitcount += 1
|
||||
continue
|
||||
try:
|
||||
commitcount = 0
|
||||
for line in fh.readlines():
|
||||
line = line.rstrip()
|
||||
if line.startswith('commit '):
|
||||
new_commit = True
|
||||
commitcount += 1
|
||||
continue
|
||||
|
||||
if line.startswith('Author:'):
|
||||
author = re.match(r'Author:\s+(.*)\s+<(.*)>', line)
|
||||
if author:
|
||||
email = author.group(2)
|
||||
continue
|
||||
if line.startswith('Author:'):
|
||||
author = re.match(r'Author:\s+(.*)\s+<(.*)>', line)
|
||||
if author:
|
||||
email = author.group(2)
|
||||
continue
|
||||
|
||||
if line.startswith('Date:'):
|
||||
if line.startswith('Date:'):
|
||||
|
||||
isodate = re.match(r'Date:\s+(.*)', line)
|
||||
d = parse_date(isodate.group(1))
|
||||
continue
|
||||
isodate = re.match(r'Date:\s+(.*)', line)
|
||||
d = parse_date(isodate.group(1))
|
||||
continue
|
||||
|
||||
if len(line) < 2 and new_commit:
|
||||
new_commit = False
|
||||
key = '{0}-{1}'.format(d.year, str(d.month).zfill(2))
|
||||
if len(line) < 2 and new_commit:
|
||||
new_commit = False
|
||||
key = '{0}-{1}'.format(d.year, str(d.month).zfill(2))
|
||||
|
||||
if key not in results:
|
||||
results[key] = []
|
||||
if key not in results:
|
||||
results[key] = []
|
||||
|
||||
if key not in commits:
|
||||
commits[key] = 0
|
||||
if key not in commits:
|
||||
commits[key] = 0
|
||||
|
||||
if email not in commits_by_contributor:
|
||||
commits_by_contributor[email] = {}
|
||||
if email not in commits_by_contributor:
|
||||
commits_by_contributor[email] = {}
|
||||
|
||||
if key not in commits_by_contributor[email]:
|
||||
commits_by_contributor[email][key] = 1
|
||||
else:
|
||||
commits_by_contributor[email][key] += 1
|
||||
if key not in commits_by_contributor[email]:
|
||||
commits_by_contributor[email][key] = 1
|
||||
else:
|
||||
commits_by_contributor[email][key] += 1
|
||||
|
||||
if email not in results[key]:
|
||||
results[key].append(email)
|
||||
if email not in results[key]:
|
||||
results[key].append(email)
|
||||
|
||||
commits[key] += commitcount
|
||||
commitcount = 0
|
||||
fh.close()
|
||||
commits[key] += commitcount
|
||||
commitcount = 0
|
||||
finally:
|
||||
fh.close()
|
||||
return (results, commits, commits_by_contributor)
|
||||
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ import optparse
|
|||
|
||||
# Import Salt libs
|
||||
import salt.utils.color
|
||||
import salt.utils.files
|
||||
|
||||
# Import 3rd-party libs
|
||||
import yaml
|
||||
|
@ -45,7 +46,8 @@ def run(command):
|
|||
'''
|
||||
cmd = r'salt \* {0} --yaml-out -t 500 > high'.format(command)
|
||||
subprocess.call(cmd, shell=True)
|
||||
data = yaml.load(open('high'))
|
||||
with salt.utils.files.fopen('high') as fp_:
|
||||
data = yaml.load(fp_)
|
||||
hashes = set()
|
||||
for key, val in six.iteritems(data):
|
||||
has = hashlib.md5(str(val)).hexdigest()
|
||||
|
|
|
@ -1201,7 +1201,8 @@ class StateModuleTest(ModuleCase, SaltReturnAssertsMixin):
|
|||
'''
|
||||
testfile = os.path.join(TMP, 'retry_file')
|
||||
time.sleep(30)
|
||||
open(testfile, 'a').close() # pylint: disable=resource-leakage
|
||||
with salt.utils.files.fopen(testfile, 'a'):
|
||||
pass
|
||||
|
||||
def test_retry_option_eventual_success(self):
|
||||
'''
|
||||
|
|
|
@ -23,6 +23,7 @@ import uuid
|
|||
|
||||
# Import salt libs
|
||||
import salt
|
||||
import salt.utils.files
|
||||
|
||||
# Import third party libs
|
||||
import yaml
|
||||
|
@ -255,7 +256,8 @@ class Swarm(object):
|
|||
pidfile = '{0}.pid'.format(path)
|
||||
try:
|
||||
try:
|
||||
pid = int(open(pidfile).read().strip())
|
||||
with salt.utils.files.fopen(pidfile) as fp_:
|
||||
pid = int(fp_.read().strip())
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
except ValueError:
|
||||
pass
|
||||
|
@ -298,7 +300,7 @@ class MinionSwarm(Swarm):
|
|||
data = {}
|
||||
if self.opts['config_dir']:
|
||||
spath = os.path.join(self.opts['config_dir'], 'minion')
|
||||
with open(spath) as conf:
|
||||
with salt.utils.files.fopen(spath) as conf:
|
||||
data = yaml.load(conf) or {}
|
||||
minion_id = '{0}-{1}'.format(
|
||||
self.opts['name'],
|
||||
|
@ -357,7 +359,7 @@ class MinionSwarm(Swarm):
|
|||
if self.opts['rand_uuid']:
|
||||
data['grains']['uuid'] = str(uuid.uuid4())
|
||||
|
||||
with open(path, 'w+') as fp_:
|
||||
with salt.utils.files.fopen(path, 'w+') as fp_:
|
||||
yaml.dump(data, fp_)
|
||||
self.confs.add(dpath)
|
||||
|
||||
|
@ -411,7 +413,7 @@ class MasterSwarm(Swarm):
|
|||
data = {}
|
||||
if self.opts['config_dir']:
|
||||
spath = os.path.join(self.opts['config_dir'], 'master')
|
||||
with open(spath) as conf:
|
||||
with salt.utils.files.fopen(spath) as conf:
|
||||
data = yaml.load(conf)
|
||||
data.update({
|
||||
'log_file': os.path.join(self.conf, 'master.log'),
|
||||
|
@ -421,7 +423,7 @@ class MasterSwarm(Swarm):
|
|||
os.makedirs(self.conf)
|
||||
path = os.path.join(self.conf, 'master')
|
||||
|
||||
with open(path, 'w+') as fp_:
|
||||
with salt.utils.files.fopen(path, 'w+') as fp_:
|
||||
yaml.dump(data, fp_)
|
||||
|
||||
def shutdown(self):
|
||||
|
|
Loading…
Add table
Reference in a new issue