Merge "Optimize "open" method with context manager"

This commit is contained in:
Jenkins 2015-11-30 08:34:26 +00:00 committed by Gerrit Code Review
commit 9e2e459def
3 changed files with 9 additions and 20 deletions

View File

@ -116,8 +116,5 @@ if __name__ == '__main__':
provision_log = os.path.join(VAR_PATH, 'provision-finished')
# touch the file so it is timestamped with when finished
pl = open(provision_log, 'a')
try:
with open(provision_log, 'a'):
os.utime(provision_log, None)
finally:
pl.close()

View File

@ -35,23 +35,14 @@ def handle_part(data, ctype, filename, payload):
if ctype == "__end__":
return
log = open('/var/log/part-handler.log', 'a')
try:
timestamp = datetime.datetime.now()
timestamp = datetime.datetime.now()
with open('/var/log/part-handler.log', 'a') as log:
log.write('%s filename:%s, ctype:%s\n' % (timestamp, filename, ctype))
finally:
log.close()
if ctype == 'text/x-cfninitdata':
f = open('/var/lib/heat-cfntools/%s' % filename, 'w')
try:
with open('/var/lib/heat-cfntools/%s' % filename, 'w') as f:
f.write(payload)
finally:
f.close()
# TODO(sdake) hopefully temporary until users move to heat-cfntools-1.3
f = open('/var/lib/cloud/data/%s' % filename, 'w')
try:
with open('/var/lib/cloud/data/%s' % filename, 'w') as f:
f.write(payload)
finally:
f.close()

View File

@ -71,11 +71,12 @@ class JsonToYamlTest(common.HeatTestCase):
for path in os.listdir(dirpath):
if not path.endswith('.template') and not path.endswith('.json'):
continue
f = open(os.path.join(dirpath, path), 'r')
json_str = f.read()
with open(os.path.join(dirpath, path), 'r') as f:
json_str = f.read()
f_name = f.name
yml_str = template_format.convert_json_to_yaml(json_str)
yield (json_str, yml_str, f.name)
yield (json_str, yml_str, f_name)
def test_integer_only_keys_get_translated_correctly(self):
path = os.path.join(os.path.dirname(os.path.realpath(__file__)),