Optimize "open" method with context manager
Use opening context manager to open a file. Change-Id: I07ea2f163b101e9501ee54b1f5de924895b35e64
This commit is contained in:
parent
20663fbfbe
commit
66039a8c21
@ -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()
|
||||
|
@ -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()
|
||||
|
@ -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__)),
|
||||
|
Loading…
Reference in New Issue
Block a user