[node-patches] Change in ovirt-node[master]: Use fs.File in more places
fabiand at fedoraproject.org
fabiand at fedoraproject.org
Mon Jun 10 12:29:13 UTC 2013
Fabian Deutsch has uploaded a new change for review.
Change subject: Use fs.File in more places
......................................................................
Use fs.File in more places
Change-Id: I33e057c4caefaba5fa3cc97bd26773a2c8ba09f8
Signed-off-by: Fabian Deutsch <fabiand at fedoraproject.org>
---
M src/ovirt/node/config/network.py
M src/ovirt/node/utils/network.py
M src/ovirt/node/utils/security.py
3 files changed, 32 insertions(+), 37 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/ovirt-node refs/changes/11/15511/1
diff --git a/src/ovirt/node/config/network.py b/src/ovirt/node/config/network.py
index 5a26f63..dc1c4a1 100644
--- a/src/ovirt/node/config/network.py
+++ b/src/ovirt/node/config/network.py
@@ -135,22 +135,18 @@
ShellVarFile.write(self, data, True)
- if not self._is_fileobj() and self.exists():
- pcfg = fs.Config()
- if pcfg.is_enabled():
- pcfg.persist(self.filename)
+ pcfg = fs.Config()
+ if pcfg.is_enabled():
+ pcfg.persist(self.filename)
return data
- def delete(self, ifname):
- if not self._is_fileobj() and self.exists():
- pcfg = fs.Config()
- if pcfg.is_enabled():
- pcfg.unpersist(self.filename)
+ def delete(self):
+ pcfg = fs.Config()
+ if pcfg.is_enabled():
+ pcfg.unpersist(self.filename)
- os.remove(self.filename)
- elif is_fileobj(self.filename):
- self.filename.truncate(0)
+ self._fileobj.delete()
def _aug_get_or_set(augpath, new_servers=None):
diff --git a/src/ovirt/node/utils/network.py b/src/ovirt/node/utils/network.py
index a7a12a1..164ab21 100644
--- a/src/ovirt/node/utils/network.py
+++ b/src/ovirt/node/utils/network.py
@@ -20,6 +20,7 @@
# also available at http://www.gnu.org/copyleft/gpl.html.
from ovirt.node import base, utils
from ovirt.node.config.network import NicConfig
+from ovirt.node.utils.fs import File
import glob
import gudev
import logging
@@ -232,9 +233,8 @@
# Fallback
has_carrier = False
try:
- with open("/sys/class/net/%s/carrier" % self.ifname) as c:
- content = c.read()
- has_carrier = "1" in content
+ content = File("/sys/class/net/%s/carrier" % self.ifname).read()
+ has_carrier = "1" in content
except:
LOGGER.debug("Carrier down for %s" % self.ifname)
return has_carrier
@@ -635,27 +635,27 @@
class Vlans(base.Base):
"""A class to offer a convenience api to the vconfig file
"""
- cfg = "/proc/net/vlan/config"
+ cfgfilename = "/proc/net/vlan/config"
def parse_cfg(self):
- if not os.path.exists(self.cfg):
+ if not os.path.exists(self.cfgfilename):
raise RuntimeError("vlans ain't enabled.")
vlans = {}
try:
- with open(self.cfg) as f:
- data_block = False
- for line in f:
- if line.startswith("Name-Type"):
- data_block = True
- continue
- if not data_block:
- continue
- vdev, _, hdev = [field.strip()
- for field in line.split("|")]
- if not hdev in vlans:
- vlans[hdev] = []
- vlans[hdev].append(vdev)
+ data = File(self.cfgfilename)
+ data_block = False
+ for line in data:
+ if line.startswith("Name-Type"):
+ data_block = True
+ continue
+ if not data_block:
+ continue
+ vdev, _, hdev = [field.strip()
+ for field in line.split("|")]
+ if not hdev in vlans:
+ vlans[hdev] = []
+ vlans[hdev].append(vdev)
except IOError as e:
self.logger.warning("Could not read vlan config: %s" %
e.message)
diff --git a/src/ovirt/node/utils/security.py b/src/ovirt/node/utils/security.py
index 22ef934..032e3ad 100644
--- a/src/ovirt/node/utils/security.py
+++ b/src/ovirt/node/utils/security.py
@@ -19,9 +19,10 @@
# MA 02110-1301, USA. A copy of the GNU General Public License is
# also available at http://www.gnu.org/copyleft/gpl.html.
from ovirt.node import base, valid, utils
-import process
-import os.path
+from ovirt.node.utils.fs import File
import PAM as _PAM # @UnresolvedImport
+import os.path
+import process
"""
Some convenience functions related to security
@@ -54,9 +55,8 @@
if additional_lines:
self.logger.debug("Updating /etc/profile")
- with open("/etc/profile", "a") as f:
- lines = "\n" + "\n".join(additional_lines)
- f.write(lines)
+ lines = "\n" + "\n".join(additional_lines)
+ File("/etc/profile").write(lines, "a")
ofunc.ovirt_store_config("/etc/profile")
self.restart()
@@ -111,8 +111,7 @@
if not os.path.exists(fn_hostkey):
raise Exception("SSH hostkey does not yet exist.")
- with open(fn_hostkey) as hkf:
- hostkey = hkf.read()
+ hostkey = File(fn_hostkey).read()
hostkey_fp_cmd = "ssh-keygen -l -f '%s'" % fn_hostkey
fingerprint = process.pipe(hostkey_fp_cmd).strip().split(" ")[1]
--
To view, visit http://gerrit.ovirt.org/15511
To unsubscribe, visit http://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I33e057c4caefaba5fa3cc97bd26773a2c8ba09f8
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-node
Gerrit-Branch: master
Gerrit-Owner: Fabian Deutsch <fabiand at fedoraproject.org>
More information about the node-patches
mailing list