hopefully fewer leaks in files : always use a context manager to open files
[nodemanager.git] / plugins / rawdisk.py
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