Optimize "open" method with context manager

Use opening context manager to open a file.
Change-Id: Ice9be6350fd01cbf5eec686ac68d7fc8b5a4d5bd
This commit is contained in:
zhang.lei 2016-03-01 10:50:01 +08:00
parent 74afe236d3
commit a4b1cef845
6 changed files with 50 additions and 51 deletions

View File

@ -117,7 +117,6 @@ class TempestCleanup(command.Command):
if is_dry_run:
self.dry_run_data["_tenants_to_clean"] = {}
f = open(DRY_RUN_JSON, 'w+')
admin_mgr = self.admin_mgr
# Always cleanup tempest and alt tempest tenants unless
@ -146,9 +145,9 @@ class TempestCleanup(command.Command):
svc.run()
if is_dry_run:
f.write(json.dumps(self.dry_run_data, sort_keys=True,
indent=2, separators=(',', ': ')))
f.close()
with open(DRY_RUN_JSON, 'w+') as f:
f.write(json.dumps(self.dry_run_data, sort_keys=True,
indent=2, separators=(',', ': ')))
self._remove_admin_user_roles()
@ -281,16 +280,15 @@ class TempestCleanup(command.Command):
svc = service(admin_mgr, **kwargs)
svc.run()
f = open(SAVED_STATE_JSON, 'w+')
f.write(json.dumps(data,
sort_keys=True, indent=2, separators=(',', ': ')))
f.close()
with open(SAVED_STATE_JSON, 'w+') as f:
f.write(json.dumps(data,
sort_keys=True, indent=2, separators=(',', ': ')))
def _load_json(self):
try:
json_file = open(SAVED_STATE_JSON)
self.json_data = json.load(json_file)
json_file.close()
with open(SAVED_STATE_JSON) as json_file:
self.json_data = json.load(json_file)
except IOError as ex:
LOG.exception("Failed loading saved state, please be sure you"
" have first run cleanup with --init-saved-state "

View File

@ -354,8 +354,6 @@ def main(opts=None):
outfile = sys.stdout
if update:
conf_file = _get_config_file()
if opts.output:
outfile = open(opts.output, 'w+')
CONF_PARSER = moves.configparser.SafeConfigParser()
CONF_PARSER.optionxform = str
CONF_PARSER.readfp(conf_file)
@ -378,8 +376,9 @@ def main(opts=None):
display_results(results, update, replace)
if update:
conf_file.close()
CONF_PARSER.write(outfile)
outfile.close()
if opts.output:
with open(opts.output, 'w+') as outfile:
CONF_PARSER.write(outfile)
finally:
icreds.clear_creds()

View File

@ -81,21 +81,23 @@ def find_skips_in_file(path):
DEF_RE = re.compile(r'\s*def (\w+)\(')
bug_found = False
results = []
lines = open(path, 'rb').readlines()
for x, line in enumerate(lines):
if not bug_found:
res = BUG_RE.match(line)
if res:
bug_no = int(res.group(1))
debug("Found bug skip %s on line %d", bug_no, x + 1)
bug_found = True
else:
res = DEF_RE.match(line)
if res:
method = res.group(1)
debug("Found test method %s skips for bug %d", method, bug_no)
results.append((method, bug_no))
bug_found = False
with open(path, 'rb') as content:
lines = content.readlines()
for x, line in enumerate(lines):
if not bug_found:
res = BUG_RE.match(line)
if res:
bug_no = int(res.group(1))
debug("Found bug skip %s on line %d", bug_no, x + 1)
bug_found = True
else:
res = DEF_RE.match(line)
if res:
method = res.group(1)
debug("Found test method %s skips for bug %d",
method, bug_no)
results.append((method, bug_no))
bug_found = False
return results

View File

@ -393,8 +393,6 @@ class ScenarioTest(tempest.test.BaseTestCase):
if properties is None:
properties = {}
name = data_utils.rand_name('%s-' % name)
image_file = open(path, 'rb')
self.addCleanup(image_file.close)
params = {
'name': name,
'container_format': fmt,
@ -405,7 +403,8 @@ class ScenarioTest(tempest.test.BaseTestCase):
image = self.image_client.create_image(**params)['image']
self.addCleanup(self.image_client.delete_image, image['id'])
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']
def glance_image_create(self):

View File

@ -36,9 +36,8 @@ class TestTempestInit(base.TestCase):
testr_conf_file = init.TESTR_CONF % (top_level_path, discover_path)
conf_path = conf_dir.join('.testr.conf')
conf_file = open(conf_path, 'r')
self.addCleanup(conf_file.close)
self.assertEqual(conf_file.read(), testr_conf_file)
with open(conf_path, 'r') as conf_file:
self.assertEqual(conf_file.read(), testr_conf_file)
def test_generate_sample_config(self):
local_dir = self.useFixture(fixtures.TempDir())

View File

@ -73,21 +73,23 @@ def find_skips_in_file(path):
DEF_RE = re.compile(r'\s*def (\w+)\(')
bug_found = False
results = []
lines = open(path, 'rb').readlines()
for x, line in enumerate(lines):
if not bug_found:
res = BUG_RE.match(line)
if res:
bug_no = int(res.group(1))
debug("Found bug skip %s on line %d", bug_no, x + 1)
bug_found = True
else:
res = DEF_RE.match(line)
if res:
method = res.group(1)
debug("Found test method %s skips for bug %d", method, bug_no)
results.append((method, bug_no))
bug_found = False
with open(path, 'rb') as content:
lines = content.readlines()
for x, line in enumerate(lines):
if not bug_found:
res = BUG_RE.match(line)
if res:
bug_no = int(res.group(1))
debug("Found bug skip %s on line %d", bug_no, x + 1)
bug_found = True
else:
res = DEF_RE.match(line)
if res:
method = res.group(1)
debug("Found test method %s skips for bug %d",
method, bug_no)
results.append((method, bug_no))
bug_found = False
return results