Optimize "open" method with context manager
Use opening context manager to open a file. Change-Id: Ice9be6350fd01cbf5eec686ac68d7fc8b5a4d5bd
This commit is contained in:
@@ -117,7 +117,6 @@ class TempestCleanup(command.Command):
|
|||||||
|
|
||||||
if is_dry_run:
|
if is_dry_run:
|
||||||
self.dry_run_data["_tenants_to_clean"] = {}
|
self.dry_run_data["_tenants_to_clean"] = {}
|
||||||
f = open(DRY_RUN_JSON, 'w+')
|
|
||||||
|
|
||||||
admin_mgr = self.admin_mgr
|
admin_mgr = self.admin_mgr
|
||||||
# Always cleanup tempest and alt tempest tenants unless
|
# Always cleanup tempest and alt tempest tenants unless
|
||||||
@@ -146,9 +145,9 @@ class TempestCleanup(command.Command):
|
|||||||
svc.run()
|
svc.run()
|
||||||
|
|
||||||
if is_dry_run:
|
if is_dry_run:
|
||||||
f.write(json.dumps(self.dry_run_data, sort_keys=True,
|
with open(DRY_RUN_JSON, 'w+') as f:
|
||||||
indent=2, separators=(',', ': ')))
|
f.write(json.dumps(self.dry_run_data, sort_keys=True,
|
||||||
f.close()
|
indent=2, separators=(',', ': ')))
|
||||||
|
|
||||||
self._remove_admin_user_roles()
|
self._remove_admin_user_roles()
|
||||||
|
|
||||||
@@ -281,16 +280,15 @@ class TempestCleanup(command.Command):
|
|||||||
svc = service(admin_mgr, **kwargs)
|
svc = service(admin_mgr, **kwargs)
|
||||||
svc.run()
|
svc.run()
|
||||||
|
|
||||||
f = open(SAVED_STATE_JSON, 'w+')
|
with open(SAVED_STATE_JSON, 'w+') as f:
|
||||||
f.write(json.dumps(data,
|
f.write(json.dumps(data,
|
||||||
sort_keys=True, indent=2, separators=(',', ': ')))
|
sort_keys=True, indent=2, separators=(',', ': ')))
|
||||||
f.close()
|
|
||||||
|
|
||||||
def _load_json(self):
|
def _load_json(self):
|
||||||
try:
|
try:
|
||||||
json_file = open(SAVED_STATE_JSON)
|
with open(SAVED_STATE_JSON) as json_file:
|
||||||
self.json_data = json.load(json_file)
|
self.json_data = json.load(json_file)
|
||||||
json_file.close()
|
|
||||||
except IOError as ex:
|
except IOError as ex:
|
||||||
LOG.exception("Failed loading saved state, please be sure you"
|
LOG.exception("Failed loading saved state, please be sure you"
|
||||||
" have first run cleanup with --init-saved-state "
|
" have first run cleanup with --init-saved-state "
|
||||||
|
@@ -354,8 +354,6 @@ def main(opts=None):
|
|||||||
outfile = sys.stdout
|
outfile = sys.stdout
|
||||||
if update:
|
if update:
|
||||||
conf_file = _get_config_file()
|
conf_file = _get_config_file()
|
||||||
if opts.output:
|
|
||||||
outfile = open(opts.output, 'w+')
|
|
||||||
CONF_PARSER = moves.configparser.SafeConfigParser()
|
CONF_PARSER = moves.configparser.SafeConfigParser()
|
||||||
CONF_PARSER.optionxform = str
|
CONF_PARSER.optionxform = str
|
||||||
CONF_PARSER.readfp(conf_file)
|
CONF_PARSER.readfp(conf_file)
|
||||||
@@ -378,8 +376,9 @@ def main(opts=None):
|
|||||||
display_results(results, update, replace)
|
display_results(results, update, replace)
|
||||||
if update:
|
if update:
|
||||||
conf_file.close()
|
conf_file.close()
|
||||||
CONF_PARSER.write(outfile)
|
if opts.output:
|
||||||
outfile.close()
|
with open(opts.output, 'w+') as outfile:
|
||||||
|
CONF_PARSER.write(outfile)
|
||||||
finally:
|
finally:
|
||||||
icreds.clear_creds()
|
icreds.clear_creds()
|
||||||
|
|
||||||
|
@@ -81,21 +81,23 @@ def find_skips_in_file(path):
|
|||||||
DEF_RE = re.compile(r'\s*def (\w+)\(')
|
DEF_RE = re.compile(r'\s*def (\w+)\(')
|
||||||
bug_found = False
|
bug_found = False
|
||||||
results = []
|
results = []
|
||||||
lines = open(path, 'rb').readlines()
|
with open(path, 'rb') as content:
|
||||||
for x, line in enumerate(lines):
|
lines = content.readlines()
|
||||||
if not bug_found:
|
for x, line in enumerate(lines):
|
||||||
res = BUG_RE.match(line)
|
if not bug_found:
|
||||||
if res:
|
res = BUG_RE.match(line)
|
||||||
bug_no = int(res.group(1))
|
if res:
|
||||||
debug("Found bug skip %s on line %d", bug_no, x + 1)
|
bug_no = int(res.group(1))
|
||||||
bug_found = True
|
debug("Found bug skip %s on line %d", bug_no, x + 1)
|
||||||
else:
|
bug_found = True
|
||||||
res = DEF_RE.match(line)
|
else:
|
||||||
if res:
|
res = DEF_RE.match(line)
|
||||||
method = res.group(1)
|
if res:
|
||||||
debug("Found test method %s skips for bug %d", method, bug_no)
|
method = res.group(1)
|
||||||
results.append((method, bug_no))
|
debug("Found test method %s skips for bug %d",
|
||||||
bug_found = False
|
method, bug_no)
|
||||||
|
results.append((method, bug_no))
|
||||||
|
bug_found = False
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
@@ -393,8 +393,6 @@ class ScenarioTest(tempest.test.BaseTestCase):
|
|||||||
if properties is None:
|
if properties is None:
|
||||||
properties = {}
|
properties = {}
|
||||||
name = data_utils.rand_name('%s-' % name)
|
name = data_utils.rand_name('%s-' % name)
|
||||||
image_file = open(path, 'rb')
|
|
||||||
self.addCleanup(image_file.close)
|
|
||||||
params = {
|
params = {
|
||||||
'name': name,
|
'name': name,
|
||||||
'container_format': fmt,
|
'container_format': fmt,
|
||||||
@@ -405,7 +403,8 @@ class ScenarioTest(tempest.test.BaseTestCase):
|
|||||||
image = self.image_client.create_image(**params)['image']
|
image = self.image_client.create_image(**params)['image']
|
||||||
self.addCleanup(self.image_client.delete_image, image['id'])
|
self.addCleanup(self.image_client.delete_image, image['id'])
|
||||||
self.assertEqual("queued", image['status'])
|
self.assertEqual("queued", image['status'])
|
||||||
self.image_client.update_image(image['id'], data=image_file)
|
with open(path, 'rb') as image_file:
|
||||||
|
self.image_client.update_image(image['id'], data=image_file)
|
||||||
return image['id']
|
return image['id']
|
||||||
|
|
||||||
def glance_image_create(self):
|
def glance_image_create(self):
|
||||||
|
@@ -36,9 +36,8 @@ class TestTempestInit(base.TestCase):
|
|||||||
testr_conf_file = init.TESTR_CONF % (top_level_path, discover_path)
|
testr_conf_file = init.TESTR_CONF % (top_level_path, discover_path)
|
||||||
|
|
||||||
conf_path = conf_dir.join('.testr.conf')
|
conf_path = conf_dir.join('.testr.conf')
|
||||||
conf_file = open(conf_path, 'r')
|
with open(conf_path, 'r') as conf_file:
|
||||||
self.addCleanup(conf_file.close)
|
self.assertEqual(conf_file.read(), testr_conf_file)
|
||||||
self.assertEqual(conf_file.read(), testr_conf_file)
|
|
||||||
|
|
||||||
def test_generate_sample_config(self):
|
def test_generate_sample_config(self):
|
||||||
local_dir = self.useFixture(fixtures.TempDir())
|
local_dir = self.useFixture(fixtures.TempDir())
|
||||||
|
@@ -73,21 +73,23 @@ def find_skips_in_file(path):
|
|||||||
DEF_RE = re.compile(r'\s*def (\w+)\(')
|
DEF_RE = re.compile(r'\s*def (\w+)\(')
|
||||||
bug_found = False
|
bug_found = False
|
||||||
results = []
|
results = []
|
||||||
lines = open(path, 'rb').readlines()
|
with open(path, 'rb') as content:
|
||||||
for x, line in enumerate(lines):
|
lines = content.readlines()
|
||||||
if not bug_found:
|
for x, line in enumerate(lines):
|
||||||
res = BUG_RE.match(line)
|
if not bug_found:
|
||||||
if res:
|
res = BUG_RE.match(line)
|
||||||
bug_no = int(res.group(1))
|
if res:
|
||||||
debug("Found bug skip %s on line %d", bug_no, x + 1)
|
bug_no = int(res.group(1))
|
||||||
bug_found = True
|
debug("Found bug skip %s on line %d", bug_no, x + 1)
|
||||||
else:
|
bug_found = True
|
||||||
res = DEF_RE.match(line)
|
else:
|
||||||
if res:
|
res = DEF_RE.match(line)
|
||||||
method = res.group(1)
|
if res:
|
||||||
debug("Found test method %s skips for bug %d", method, bug_no)
|
method = res.group(1)
|
||||||
results.append((method, bug_no))
|
debug("Found test method %s skips for bug %d",
|
||||||
bug_found = False
|
method, bug_no)
|
||||||
|
results.append((method, bug_no))
|
||||||
|
bug_found = False
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user