hopefully fewer leaks in files : always use a context manager to open files
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Fri, 19 Jun 2015 12:20:21 +0000 (14:20 +0200)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Fri, 19 Jun 2015 12:20:21 +0000 (14:20 +0200)
15 files changed:
Makefile
account.py
bootauth.py
conf_files.py
coresched_lxc.py
coresched_vs.py
initscript.py
nodemanager.py
plugins/hostmap.py
plugins/rawdisk.py
plugins/sliverauth.py
plugins/vsys_privs.py
plugins/vsys_sysctl.py
sliver_lxc.py
tools.py

index c5c719e..190cad2 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -107,7 +107,7 @@ debian.clean:
 
 ################################################## devel-oriented
 tags:
-       (find . '(' -name '*.py' -o -name '*.c' -o -name '*.spec' ')' ; ls initscripts/*) | xargs etags 
+       git ls-files | xargs etags
 
 .PHONY: tags
 
index 3631d4d..4d74a1b 100644 (file)
@@ -155,9 +155,10 @@ class Account:
             root_ssh = "/home/{}/.ssh".format(slicename)
             sliver_ssh = "/vservers/{}/home/{}/.ssh".format(slicename, slicename)
             def is_mounted (root_ssh):
-                for mount_line in file('/proc/mounts').readlines():
-                    if mount_line.find (root_ssh) >= 0:
-                        return True
+                with open('/proc/mounts') as mountsfile:
+                    for mount_line in mountsfile.readlines():
+                        if mount_line.find (root_ssh) >= 0:
+                            return True
                 return False
             if do_mount:
                 # any of both might not exist yet
index dae075c..5c9dfa7 100755 (executable)
@@ -45,12 +45,14 @@ def main():
             config = Config(optval)
         elif opt == "-n" or opt == "--node" or opt == "--nodeid" or opt == "--node-id" or opt == "--node_id":
             if os.path.exists(optval):
-                node_id = file(optval).read().strip()
+                with open(optval) as optfile:
+                    node_id = optfile.read().strip()
             else:
                 node_id = int(optval)
         elif opt == "-k" or opt == "--key":
             if os.path.exists(optval):
-                key = file(optval).read().strip()
+                with open(optval) as optfile:
+                    key = optfile.read().strip()
             else:
                 key = optval
         else:
index 252c795..fcc3b7d 100644 (file)
@@ -107,7 +107,8 @@ if __name__ == '__main__':
 
     # Load /etc/planetlab/session
     if os.path.exists(options.session):
-        session = file(options.session).read().strip()
+        with open(options.session) as f:
+            session = f.read().strip()
     else:
         session = options.session
 
index d48e2bb..a6aef79 100644 (file)
@@ -247,7 +247,8 @@ class CoreSched:
                 if glo_coresched_simulate:
                         print "F", cgroup
                 else:
-                    file(cgroup, "w").write(freeze)
+                    with open(cgroup, "w") as f:
+                        f.write(freeze)
             except Exception as e:
                 # the cgroup probably didn't exit...
                 logger.log("CoreSched: exception while setting freeze for {} ({})".format(slicename, e))
index d064e93..774eb30 100644 (file)
@@ -242,7 +242,8 @@ class CoreSched:
             if glo_coresched_simulate:
                 print "R", "/dev/cgroup/" + cgroup + "/" + var_name, self.listToRange(cpus)
             else:
-                file("/dev/cgroup/" + cgroup + "/" + var_name, "w").write( self.listToRange(cpus) + "\n" )
+                with opwn("/dev/cgroup/{}/{}".format(cgroup, var_name), "w") as f:
+                    f.write( self.listToRange(cpus) + "\n" )
 
     def reserveDefault (self, var_name, cpus):
         if not os.path.exists("/etc/vservers/.defaults/cgroup"):
@@ -251,7 +252,8 @@ class CoreSched:
         if glo_coresched_simulate:
             print "RDEF", "/etc/vservers/.defaults/cgroup/" + var_name, self.listToRange(cpus)
         else:
-            file("/etc/vservers/.defaults/cgroup/" + var_name, "w").write( self.listToRange(cpus) + "\n" )
+            with open("/etc/vservers/.defaults/cgroup/{}".format(var_name), "w") as f:
+                f.write( self.listToRange(cpus) + "\n" )
 
     def listToRange (self, list):
         """ take a list of items [1,2,3,5,...] and return it as a range: "1-3,5"
index cf9141c..a25e06a 100644 (file)
@@ -55,7 +55,8 @@ class Initscript:
         enable_link = "/vservers/%s/etc/rc.d/rc3.d/S99vinit" % self.name
         enable_target = "../init.d/vinit"
         # install in sliver
-        code = file(vinit_source).read()
+        with open(vinit_source) as f:
+            code = f.read()
         if tools.replace_file_with_string(vinit_script, code, chmod=0755):
             logger.log("Initscript: %s: installed generic vinit rc script" % self.name)
         # create symlink for runlevel 3
@@ -94,7 +95,8 @@ class Initscript:
         enable_link = "/vservers/%s/etc/systemd/system/multi-user.target.wants/vinit.service" % self.name
         enable_target = "/usr/lib/systemd/system/vinit.service"
         # install in sliver
-        code = file(vinit_source).read()
+        with open(vinit_source) as f:
+            code = f.read()
         if tools.replace_file_with_string(vinit_unit_file, code, chmod=0755):
             logger.log("Initscript: %s: installed vinit.service unit file" % self.name)
         # create symlink for enabling this unit
index c0a5656..d397a4b 100755 (executable)
@@ -232,7 +232,8 @@ If this is not the case, please remove the pid file {}. -- exiting""".format(oth
 
             # Load /etc/planetlab/session
             if os.path.exists(self.options.session):
-                session = file(self.options.session).read().strip()
+                with open(self.options.session) as f:
+                    session = f.read().strip()
             else:
                 session = None
 
index 8bd1c08..c6829cb 100644 (file)
@@ -48,7 +48,8 @@ def GetSlivers(data, config=None, plc=None):
                 if not os.path.exists(fn):
                     continue
 
-                contents = file(fn,"r").read()
+                with open(fn) as f:
+                    contents = f.read()
 
                 hostmap = []
                 for index, entry in enumerate(tag["value"].split("\n")):
@@ -84,7 +85,8 @@ def GetSlivers(data, config=None, plc=None):
                     contents = contents + hostmap
 
                 try:
-                    file(fn, "w").write(contents)
+                    with open(fn, "w") as f:
+                        f.write(contents)
                 except:
                     logger.log_exc("hostmap (%s): failed to write %s" % (slicename, fn))
 
index f64affe..616d2c6 100644 (file)
@@ -33,19 +33,19 @@ def get_unused_devices():
         in_vg.extend(map(lambda x: x.replace("!", "/"),
                          os.listdir("/sys/block/%s/slaves" % i)))
     # Read the list of partitions
-    partitions = file("/proc/partitions", "r")
-    pat = re.compile("\s+")
-    while True:
-        buf = partitions.readline()
-        if buf == "":
-            break
-        buf = buf.strip()
-        fields = re.split(pat, buf)
-        dev = fields[-1]
-        if (not dev.startswith("dm-") and dev not in in_vg and
-            os.path.exists("/dev/%s" % dev) and
-            (os.minor(os.stat("/dev/%s" % dev).st_rdev) % 2) != 0):
-            devices.append("/dev/%s" % dev)
+    with open("/proc/partitions") as partitions:
+        pat = re.compile("\s+")
+        while True:
+            buf = partitions.readline()
+            if buf == "":
+                break
+            buf = buf.strip()
+            fields = re.split(pat, buf)
+            dev = fields[-1]
+            if (not dev.startswith("dm-") and dev not in in_vg and
+                os.path.exists("/dev/%s" % dev) and
+                (os.minor(os.stat("/dev/%s" % dev).st_rdev) % 2) != 0):
+                devices.append("/dev/%s" % dev)
     partitions.close()
     return devices
 
index 0798650..61349da 100644 (file)
@@ -112,12 +112,13 @@ def generate_sshkey (sliver):
     if not os.path.isdir (dotssh):
         os.mkdir (dotssh, 0700)
         logger.log_call ( [ 'chown', "%s:slices"%(sliver['name']), dotssh ] )
-    if not os.path.isfile (pubfile):
+    if not os.path.isfile(pubfile):
         comment="%s@%s"%(sliver['name'],socket.gethostname())
         logger.log_call( [ 'ssh-keygen', '-t', 'rsa', '-N', '', '-f', keyfile , '-C', comment] )
         os.chmod (keyfile, 0400)
         logger.log_call ( [ 'chown', "%s:slices"%(sliver['name']), keyfile, pubfile ] )
-    return file(pubfile).read().strip()
+    with open(pubfile) as f:
+        return f.read().strip()
 
 # a sliver can get created, deleted and re-created
 # the slice having the tag is not sufficient to skip key geneneration
index 01ab283..bf3b1d1 100755 (executable)
@@ -64,11 +64,12 @@ def read_privs():
                 pass
 
             for tagname in tagnames:
-                tagfile = os.path.join(slicedir,tagname)
-                values_n = file(tagfile).readlines()
-                values = map(lambda s:s.rstrip(),values_n)
-                slice = os.path.basename(slicedir)
-                cur_privs[slice][tagname]=values
+                tagfilename = os.path.join(slicedir,tagname)
+                with open(tagfilename) as tagfile:
+                    values_n = tagfile.readlines()
+                    values = [ v.rstrip() for v in values_n ]
+                    slice = os.path.basename(slicedir)
+                    cur_privs[slice][tagname] = values
 
     return cur_privs
 
index 3539ab1..abf3389 100644 (file)
@@ -46,7 +46,8 @@ def GetSlivers(data, config=None, plc=None):
                     # slice. This lets us know that we've done the sysctl.
                     try:
                         logger.log("vsys_sysctl: create file %s value %s" % (fn, value))
-                        file(fn,"w").write(value+"\n")
+                        with open(fn,"w") as f:
+                            f.write(value+"\n")
                     except:
                         logger.log("vsys_sysctl: failed to create file %s" % fn)
 
index bf5f6df..37c0912 100644 (file)
@@ -194,12 +194,14 @@ class Sliver_LXC(Sliver_Libvirt, Initscript):
 
         logger.log("creating /etc/slicename file in {}".format(os.path.join(containerDir, 'etc/slicename')))
         try:
-            file(os.path.join(containerDir, 'etc/slicename'), 'w').write(name)
+            with open(os.path.join(containerDir, 'etc/slicename'), 'w') as f:
+                f.write(name)
         except:
             logger.log_exc("exception while creating /etc/slicename")
 
         try:
-            file(os.path.join(containerDir, 'etc/slicefamily'), 'w').write(vref)
+            with open(os.path.join(containerDir, 'etc/slicefamily'), 'w') as f:
+                f.write(vref)
         except:
             logger.log_exc("exception while creating /etc/slicefamily")
 
@@ -240,7 +242,8 @@ class Sliver_LXC(Sliver_Libvirt, Initscript):
             sudoers = os.path.join(containerDir, 'etc/sudoers')
             if os.path.exists(sudoers):
                 try:
-                    file(sudoers, 'a').write("{} ALL=(ALL) NOPASSWD: ALL\n".format(name))
+                    with open(sudoers, 'a') as f:
+                        f.write("{} ALL=(ALL) NOPASSWD: ALL\n".format(name))
                 except:
                     logger.log_exc("exception while updating /etc/sudoers")
 
@@ -282,10 +285,11 @@ unset pathmunge
             if not os.path.isdir(os.path.dirname(from_root)): continue
             found = False
             try:
-                contents = file(from_root).readlines()
-                for content in contents:
-                    if content == enforced_line:
-                        found = True
+                with open(from_root) as f:
+                    contents = f.readlines()
+                    for content in contents:
+                        if content == enforced_line:
+                            found = True
             except IOError:
                 pass
             if not found:
@@ -302,7 +306,7 @@ unset pathmunge
 
         # Template for libvirt sliver configuration
         template_filename_sliceimage = os.path.join(Sliver_LXC.REF_IMG_BASE_DIR, 'lxc_template.xml')
-        if os.path.isfile (template_filename_sliceimage):
+        if os.path.isfile(template_filename_sliceimage):
             logger.verbose("Using XML template {}".format(template_filename_sliceimage))
             template_filename = template_filename_sliceimage
         else:
index 7b18c59..546288f 100644 (file)
--- a/tools.py
+++ b/tools.py
@@ -147,7 +147,8 @@ writes in a tmp file, which is then renamed (from sliverauth originally)
 returns True if a change occurred, or the file is deleted
     """
     try:
-        current = file(target).read()
+        with open(target) as f:
+            current = f.read()
     except:
         current = ""
     if current == new_contents:
@@ -178,7 +179,8 @@ def node_id():
     global _node_id
     if _node_id is None:
         try:
-            _node_id = int(file("/etc/planetlab/node_id").read())
+            with open("/etc/planetlab/node_id") as f:
+                _node_id = int(f.read())
         except:
             _node_id = ""
     return _node_id
@@ -390,7 +392,8 @@ def is_valid_ipv6(ipv6addr):
 virt_stamp = "/etc/planetlab/virt"
 def get_node_virt ():
     try:
-        return file(virt_stamp).read().strip()
+        with open(virt_stamp) as f:
+            return f.read().strip()
     except:
         pass
     logger.log("Computing virt..")