From: xiexs <xiexs@cn.fujitsu.com>
Date: Sat, 28 Nov 2015 05:53:54 +0000 (+0800)
Subject: Optimize "open" method with context manager
X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=c0d12a5d5c1b5ad3ee17ab1ca36fa89ee45083a1;p=openstack-build%2Fcinder-build.git

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
---

diff --git a/cinder/volume/targets/cxt.py b/cinder/volume/targets/cxt.py
index 9a4b4c4cf..c08974f21 100644
--- a/cinder/volume/targets/cxt.py
+++ b/cinder/volume/targets/cxt.py
@@ -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})
diff --git a/cinder/volume/targets/tgt.py b/cinder/volume/targets/tgt.py
index ad93713b0..57c02efd6 100644
--- a/cinder/volume/targets/tgt.py
+++ b/cinder/volume/targets/tgt.py
@@ -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})