Re: [PATCH 01/22] Add class for handling ifcfg files (#520146)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Nothing beyond what bcl already said.  Ack from me.  I do question the use of
shutil.move() because, in my experience, that function has been prone to
failure pretty easy.  Almost easier to copy and then remove.  But maybe the
function is a little safer these days.

On Tue, 27 Apr 2010, Radek Vykydal wrote:

We are communicating with nm via ifcfg files heavily (for configuring
we do it exclusively), and we started to use nm-c-e which does the same,
we can pass parameters to nm-c-e only via ifcfg files, so we need
backend class for NetworkDevice object to be able to keep it synced
with ifcfg files reasonably.
---
simpleconfig.py |   67 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 64 insertions(+), 3 deletions(-)

diff --git a/simpleconfig.py b/simpleconfig.py
index b3e6c90..1f90356 100644
--- a/simpleconfig.py
+++ b/simpleconfig.py
@@ -16,6 +16,8 @@

import string
import os
+import tempfile
+import shutil

# use our own ASCII only uppercase function to avoid locale issues
# not going to be fast but not important
@@ -80,9 +82,68 @@ class SimpleConfigFile:

    def get (self, key):
        key = uppercase_ASCII_string(key)
-        if self.info.has_key (key):
-            return self.info[key]
+        return self.info.get(key, "")
+
+
+class IfcfgFile(SimpleConfigFile):
+
+    def __init__(self, dir, iface):
+        SimpleConfigFile.__init__(self)
+        self.iface = iface
+        self.dir = dir
+
+    @property
+    def path(self):
+        return os.path.join(self.dir, "ifcfg-%s" % self.iface)
+
+    def clear(self):
+        self.info = {}
+
+    def read(self):
+        """Reads values from ifcfg file.
+
+        returns: number of values read
+        """
+        f = open(self.path, "r")
+        lines = f.readlines()
+        f.close()
+
+        for line in lines:
+            line = line.strip()
+            if line.startswith("#") or line == '':
+                continue
+            fields = line.strip().split('=', 2)
+            if len(fields) < 2:
+                continue
+            key = uppercase_ASCII_string(fields[0])
+            value = fields[1]
+            # XXX hack
+            value = value.replace('"', '')
+            value = value.replace("'", '')
+            self.info[key] = value
+
+        return len(self.info)
+
+    # This method has to write file in a particular
+    # way so that ifcfg-rh's inotify mechanism triggeres
+    # TODORV: check that it is still true.
+    def write(self, dir=None):
+        """Writes values into ifcfg file."""
+
+        if not dir:
+            path = self.path
        else:
-            return ""
+            path = os.path.join(dir, os.path.basename(self.path))
+
+        fd, newifcfg = tempfile.mkstemp(prefix="ifcfg-%s" % self.iface, text=False)
+        os.write(fd, self.__str__())
+        os.close(fd)

+        os.chmod(newifcfg, 0644)
+        try:
+            os.remove(path)
+        except OSError, e:
+            if e.errno != 2:
+                raise
+        shutil.move(newifcfg, path)



- -- David Cantrell <dcantrell@xxxxxxxxxx>
Red Hat / Honolulu, HI

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iEYEARECAAYFAkvfiC0ACgkQ5hsjjIy1Vknl0ACgpwN0l8ilvI2NrYoedsHc+vbj
FTgAoIif4BmRaYKBf9uGoIRdlBOu7NbW
=TCxz
-----END PGP SIGNATURE-----

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/anaconda-devel-list

[Index of Archives]     [Kickstart]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]
  Powered by Linux