Commit 0ff082c1 authored by Bryton Lacquement's avatar Bryton Lacquement 🚪

Fix most ResourceWarning

Python 3 warns about unclosed files.
parent 5decb5a7
......@@ -28,7 +28,8 @@ def isLocalTcpPortOpened(ip_address, port):
ip_addr_hex = ('%08X' * int_count) % struct.unpack('I' * int_count, socket.inet_pton(family, ip_address))
full_addr_hex = ip_addr_hex + ":%04X" % port
return any(full_addr_hex == line.split()[1] for line in open(tcp_path).readlines())
with open(tcp_path) as f:
return any(full_addr_hex == line.split()[1] for line in f.readlines())
def main():
if isLocalTcpPortOpened(sys.argv[1], int(sys.argv[2])):
......
......@@ -51,20 +51,21 @@ class RunPromise(GenericPromise):
# First, parse the log file
backup_started = False
backup_ended = False
for line in open(status, 'r'):
m = re.match(r"(.*), (.*), (.*), backup (.*)$", line)
if m:
if m.group(4) == "running":
backup_started = True
backup_start = parse(m.group(1))
elif m.group(4) == "failed":
backup_ended = True
backup_failed = True
backup_end = parse(m.group(1))
elif m.group(4) == "success":
backup_ended = True
backup_failed = False
backup_end = parse(m.group(1))
with open(status, 'r') as f:
for line in f:
m = re.match(r"(.*), (.*), (.*), backup (.*)$", line)
if m:
if m.group(4) == "running":
backup_started = True
backup_start = parse(m.group(1))
elif m.group(4) == "failed":
backup_ended = True
backup_failed = True
backup_end = parse(m.group(1))
elif m.group(4) == "success":
backup_ended = True
backup_failed = False
backup_end = parse(m.group(1))
# Then check result
if backup_ended and backup_failed:
......
......@@ -43,7 +43,8 @@ html_escape_table = {
def getBuildAndRunParams(config):
json_file = os.path.join(config['etc_dir'], 'config.json')
json_params = json.load(open(json_file))
with open(json_file) as f:
json_params = json.load(f)
return json_params
......@@ -52,7 +53,8 @@ def saveBuildAndRunParams(config, params):
Works like that because this function do not care
about how you got the parameters"""
json_file = os.path.join(config['etc_dir'], 'config.json')
open(json_file, "w").write(json.dumps(params))
with open(json_file, "w") as f:
f.write(json.dumps(params))
def html_escape(text):
......@@ -899,7 +901,8 @@ def runSlapgridUntilSuccess(config, step):
else:
return -1
counter_file = os.path.join(config['runner_workdir'], '.turn-left')
open(counter_file, 'w+').write(str(max_tries))
with open(counter_file, 'w+') as f:
f.write(str(max_tries))
counter = max_tries
slapgrid = True
# XXX-Nico runSoftwareWithLock can return 0 or False (0==False)
......@@ -909,9 +912,11 @@ def runSlapgridUntilSuccess(config, step):
# slapgrid == 0 because EXIT_SUCCESS == 0
if slapgrid == 0:
break
times_left = int(open(counter_file).read()) - 1
with open(counter_file) as f:
times_left = int(f.read()) - 1
if times_left > 0 :
open(counter_file, 'w+').write(str(times_left))
with open(counter_file, 'w+') as f:
f.write(str(times_left))
counter = times_left
else :
counter = 0
......
......@@ -101,8 +101,10 @@ echo "htpasswd $@" > %s/monitor-htpasswd
def check_config(self):
config_parameter = os.path.join(self.config_dir, 'config.parameters.json')
config_parameter_json = json.load(open(config_parameter))
config_json = json.load(open(self.config_path))
with open(config_parameter) as f:
config_parameter_json = json.load(f)
with open(self.config_path) as f:
config_json = json.load(f)
for config in config_json:
if config["key"]:
......@@ -112,20 +114,24 @@ echo "htpasswd $@" > %s/monitor-htpasswd
continue
if config["key"] == 'from-file':
self.assertTrue(os.path.exists(parameter['file']))
self.assertEqual(config["value"], open(parameter['file']).read())
with open(parameter['file']) as f:
self.assertEqual(config["value"], f.read())
elif config["key"] == 'httpd-password':
http_passwd = "%s/monitor-htpasswd" % self.base_dir
#XXX where \n bellow come from ?
command = 'htpasswd -cb %s admin %s%s' % (http_passwd, config["value"], '\n')
self.assertTrue(os.path.exists(parameter['file']))
self.assertTrue(os.path.exists(http_passwd))
self.assertEqual(config["value"], open(parameter['file']).read())
self.assertEqual(open(http_passwd).read(), command)
with open(parameter['file']) as f:
self.assertEqual(config["value"], f.read())
with open(http_passwd) as f:
self.assertEqual(f.read(), command)
elif config["key"] == 'cors-domain':
cors_file = "%s/test-httpd-cors.cfg" % self.base_dir
self.assertTrue(os.path.exists(cors_file))
cors_string = self.generate_cors_string(config["value"].split())
self.assertEqual(cors_string, open(cors_file).read())
with open(cors_file) as f:
self.assertEqual(cors_string, f.read())
def check_cfg_config(self, config_list):
cfg_output = os.path.join(self.config_dir, 'config.cfg')
......
......@@ -249,7 +249,8 @@ partition-folder = %(base_dir)s
instance_config = os.path.join(instance.config_folder, '.jio_documents', 'config.json')
self.assertTrue(os.path.exists(instance_config))
config_content = json.loads(open(instance_config).read())
with open(instance_config) as f:
config_content = json.loads(f.read())
self.assertEqual(len(config_content), 4)
key_list = ['', 'sample', 'monitor-password', 'cors-domain']
for parameter in config_content:
......
......@@ -150,7 +150,8 @@ class RunPromise(GenericPromise):
result_file = os.path.join(self.output_dir, 'my_promise.status.json')
os.system('cat %s' % result_file)
self.assertTrue(os.path.exists(result_file))
my_result = json.load(open(result_file))
with open(result_file) as f:
my_result = json.load(f)
my_result['result'].pop('date')
self.assertTrue(my_result.pop(u'execution-time'))
expected_result = {
......@@ -164,7 +165,8 @@ class RunPromise(GenericPromise):
result_file = os.path.join(self.output_dir, 'my_second_promise.status.json')
self.assertTrue(os.path.exists(result_file))
second_result = json.load(open(result_file))
with open(result_file) as f:
second_result = json.load(f)
second_result['result'].pop('date')
self.assertTrue(second_result.pop(u'execution-time'))
......@@ -185,7 +187,8 @@ class RunPromise(GenericPromise):
result_file = os.path.join(self.output_dir, 'my_promise.status.json')
self.assertTrue(os.path.exists(result_file))
my_result = json.load(open(result_file))
with open(result_file) as f:
my_result = json.load(f)
my_result['result'].pop('date')
self.assertTrue(my_result.pop(u'execution-time'))
......@@ -206,7 +209,8 @@ class RunPromise(GenericPromise):
result_file = os.path.join(self.output_dir, 'my_promise.status.json')
self.assertTrue(os.path.exists(result_file))
my_result = json.load(open(result_file))
with open(result_file) as f:
my_result = json.load(f)
my_result['result'].pop('date')
self.assertTrue(my_result.pop(u'execution-time'))
......@@ -225,7 +229,8 @@ class RunPromise(GenericPromise):
promise_runner2 = MonitorPromiseLauncher(parser)
promise_runner2.start()
my_result = json.load(open(result_file))
with open(result_file) as f:
my_result = json.load(f)
my_result['result'].pop('date')
self.assertTrue(my_result.pop(u'execution-time'))
......@@ -286,7 +291,8 @@ class RunPromise(GenericPromise):
result_file = os.path.join(self.output_dir, 'promise_1.status.json')
self.assertTrue(os.path.exists(result_file))
result1 = json.load(open(result_file))
with open(result_file) as f:
result1 = json.load(f)
start_date = datetime.strptime(result1['result'].pop('date'), '%Y-%m-%dT%H:%M:%S+0000')
expected_result = {
......@@ -301,7 +307,8 @@ class RunPromise(GenericPromise):
parser = self.getPromiseParser(force=True)
promise_runner = MonitorPromiseLauncher(parser)
promise_runner.start()
result2 = json.load(open(result_file))
with open(result_file) as f:
result2 = json.load(f)
start_date2 = datetime.strptime(result2['result'].pop('date'), '%Y-%m-%dT%H:%M:%S+0000')
self.assertTrue(result2.pop(u'execution-time'))
self.assertEqual(expected_result, result2)
......@@ -318,7 +325,8 @@ class RunPromise(GenericPromise):
result2_file = os.path.join(self.output_dir, 'promise_2.status.json')
self.assertTrue(os.path.exists(result_file))
self.assertTrue(os.path.exists(result2_file))
result1 = json.load(open(result_file))
with open(result_file) as f:
result1 = json.load(f)
start_date = datetime.strptime(result1['result'].pop('date'), '%Y-%m-%dT%H:%M:%S+0000')
self.assertTrue(result1.pop(u'execution-time'))
......@@ -352,7 +360,8 @@ class RunPromise(GenericPromise):
result_file = os.path.join(self.output_dir, 'promise_1.status.json')
self.assertTrue(os.path.exists(result_file))
result1 = json.load(open(result_file))
with open(result_file) as f:
result1 = json.load(f)
result1['result'].pop('date')
self.assertTrue(result1.pop(u'execution-time'))
expected_result = {
......@@ -367,7 +376,8 @@ class RunPromise(GenericPromise):
# second run
promise_runner = MonitorPromiseLauncher(parser)
promise_runner.start()
result2 = json.load(open(result_file))
with open(result_file) as f:
result2 = json.load(f)
result2['result'].pop('date')
self.assertTrue(result2.pop(u'execution-time'))
self.assertEqual(expected_result, result2)
......@@ -380,7 +390,8 @@ class RunPromise(GenericPromise):
result_file = os.path.join(self.output_dir, 'promise_1.status.json')
self.assertTrue(os.path.exists(result_file))
result1 = json.load(open(result_file))
with open(result_file) as f:
result1 = json.load(f)
result1['result'].pop('date')
self.assertTrue(result1.pop(u'execution-time'))
expected_result = {
......@@ -401,7 +412,8 @@ class RunPromise(GenericPromise):
promise_runner = MonitorPromiseLauncher(parser)
promise_runner.start()
result2 = json.load(open(result_file))
with open(result_file) as f:
result2 = json.load(f)
result2['result'].pop('date')
self.assertTrue(result2.pop(u'execution-time'))
self.assertEqual(expected_result, result2)
......
......@@ -159,7 +159,8 @@ class TestAutoSTemp(unittest.TestCase):
removes it when deleted.
"""
f = AutoSTemp("foo")
self.assertEqual(open(f.name, "r").read(), "foo")
with open(f.name, "r") as file:
self.assertEqual(file.read(), "foo")
fname = f.name
self.assertTrue(os.path.isfile(fname))
del f
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment