Optimize "open" method with context manager

Replace the classic open() with opening context manager to open
files which is in the create_iscsi_target methods of volume targets.

Change-Id: I12e19801a21b1279308983fd0257dae9c9653641
This commit is contained in:
xiexs 2015-11-28 13:53:54 +08:00
parent 65a6c40b6e
commit c0d12a5d5c
2 changed files with 4 additions and 6 deletions

View File

@ -130,9 +130,8 @@ class CxtAdm(iscsi.ISCSITarget):
if os.path.exists(volume_path): if os.path.exists(volume_path):
LOG.warning(_LW('Persistence file already exists for volume, ' LOG.warning(_LW('Persistence file already exists for volume, '
'found file at: %s'), volume_path) 'found file at: %s'), volume_path)
f = open(volume_path, 'w+') with open(volume_path, 'w+') as f:
f.write(volume_conf) f.write(volume_conf)
f.close()
LOG.debug('Created volume path %(vp)s,\n' LOG.debug('Created volume path %(vp)s,\n'
'content: %(vc)s', 'content: %(vc)s',
{'vp': volume_path, 'vc': volume_conf}) {'vp': volume_path, 'vc': volume_conf})

View File

@ -165,9 +165,8 @@ class TgtAdm(iscsi.ISCSITarget):
if os.path.exists(volume_path): if os.path.exists(volume_path):
LOG.warning(_LW('Persistence file already exists for volume, ' LOG.warning(_LW('Persistence file already exists for volume, '
'found file at: %s'), volume_path) 'found file at: %s'), volume_path)
f = open(volume_path, 'w+') with open(volume_path, 'w+') as f:
f.write(volume_conf) f.write(volume_conf)
f.close()
LOG.debug(('Created volume path %(vp)s,\n' LOG.debug(('Created volume path %(vp)s,\n'
'content: %(vc)s'), 'content: %(vc)s'),
{'vp': volume_path, 'vc': volume_conf}) {'vp': volume_path, 'vc': volume_conf})