]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Optimize "open" method with context manager
authorxiexs <xiexs@cn.fujitsu.com>
Sat, 28 Nov 2015 05:53:54 +0000 (13:53 +0800)
committerxiexs <xiexs@cn.fujitsu.com>
Mon, 30 Nov 2015 14:20:29 +0000 (14:20 +0000)
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

cinder/volume/targets/cxt.py
cinder/volume/targets/tgt.py

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