poudriere-runner: reformat with black and strip dead code
authorSofian Brabez <sbz@6dev.net>
Sun, 18 Apr 2021 17:31:27 +0000 (17:31 +0000)
committersbz <sbz@6dev.net>
Sun, 18 Apr 2021 17:31:27 +0000 (17:31 +0000)
poudriere-runner.py

index 8bcfa2354b40e9c06558718765e35cf46b17ba39..98940ac1d8ff325d941c0e47b5110eb6cbf4cde2 100755 (executable)
@@ -11,15 +11,16 @@ import shlex
 import subprocess
 import sys
 
-from typing import List, Dict
+from typing import Dict
 
 import colored
 from colored import stylize
 
-LOCALBASE="/usr/local"
-PORTSDIR="/usr/ports" or os.environ['PORTSDIR']
+LOCALBASE = "/usr/local"
+PORTSDIR = "/usr/ports" or os.environ["PORTSDIR"]
 
-default_conf="""
+default_conf = (
+    """
 [default]
 debug=True
 setup=False
@@ -47,7 +48,10 @@ version=13.0-RELEASE
 name=14amd64
 arch=amd64
 version=14.0-CURRENT
-""" % LOCALBASE
+"""
+    % LOCALBASE
+)
+
 
 class Prunner(object):
     def __init__(self):
@@ -61,13 +65,11 @@ class Prunner(object):
     def setUp(self, **params: Dict):
         if debug:
             print("Creating jail setup...")
-        params['host'] = cfg.get("default", "sets_host")
+        params["host"] = cfg.get("default", "sets_host")
         for jail in self._jails:
             cmd = "poudriere jail -c -j {0} -a {1} -v {2} {3}".format(
-                jail['name'],
-                jail['arch'],
-                jail['version'],
-                params['host'])
+                jail["name"], jail["arch"], jail["version"], params["host"]
+            )
             if debug:
                 print("exec: {0}".format(cmd))
             sudo(cmd)
@@ -76,30 +78,34 @@ class Prunner(object):
         if debug:
             print("Stoping jails teardown()...")
         for jail in self._jails:
-            cmd = "poudriere jail -k -j {0}".format(jail['name'])
+            cmd = "poudriere jail -k -j {0}".format(jail["name"])
             if debug:
                 print("exec: {0}".format(cmd))
             sudo(cmd)
 
     @property
     def jails(self):
-       return self._jails
+        return self._jails
 
     @property
     def port_tree(self):
-       return self._port_tree
+        return self._port_tree
 
     def testPort(self, origin: str, port_tree: str):
         for jail in self._jails:
             if debug:
-                print(stylize("testport o: {0} j: {1}".format(origin,
-                    jail['name']), colored.fg("magenta")))
+                print(
+                    stylize(
+                        "testport o: {0} j: {1}".format(origin, jail["name"]),
+                        colored.fg("magenta"),
+                    )
+                )
             cmd = "poudriere testport -o {0} -j {1} -p {2}".format(
-                    origin, jail['name'], port_tree)
+                origin, jail["name"], port_tree
+            )
             out, err = sudo(cmd)
             if debug:
-                print("j: {0}\nout: {1}\nerr: {2}".format(jail['name'], out,
-                    err))
+                print("j: {0}\nout: {1}\nerr: {2}".format(jail["name"], out, err))
 
     def testAll(self):
         for o in self.sbzports():
@@ -108,84 +114,88 @@ class Prunner(object):
     def bulk(self, jail):
         raise NotImplemented("not available yet")
 
-    def sbzPorts(self):
-        #TODO: don't rely on sbzports file
-        ports = open('{0}/sbzports'.format(os.path.expanduser('~%s' %
-            os.environ['USER'])), 'r').read().strip().split('\n')
-        return ports
-
     def __del__(self):
-        if hasattr(self, 'is_setup') and self.is_setup:
+        if hasattr(self, "is_setup") and self.is_setup:
             self.tearDown()
 
+
 def run(command: str, use_sudo=False) -> str:
-    cpuset_cmd = cfg.get('default', 'cpuset_cmd')
-    use_cpuset = cfg.getboolean('default', 'cpuset')
+    cpuset_cmd = cfg.get("default", "cpuset_cmd")
+    use_cpuset = cfg.getboolean("default", "cpuset")
     if use_cpuset:
         command = "%s %s" % (cpuset_cmd, command)
 
     command = "sudo %s" % command if use_sudo else command
-    p = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE,
-            universal_newlines=True)
+    p = subprocess.Popen(
+        shlex.split(command), stdout=subprocess.PIPE, universal_newlines=True
+    )
     stdout, stderr = p.communicate()
     p.wait()
 
     return stdout.strip(), stderr
 
+
 def sudo(command: str):
     return run(command, use_sudo=True)
 
+
 def loadDisk():
-    if cfg.getboolean('default', 'mdconfig') is False:
+    if cfg.getboolean("default", "mdconfig") is False:
         print("Skip mdconfig...\n")
         return
 
-    mdconfig_cmd = cfg.get('default', 'mdconfig_cmd')
-    disk_path = cfg.get('default', 'disk_path')
-    if not os.path.exists('/dev/md0'):
+    mdconfig_cmd = cfg.get("default", "mdconfig_cmd")
+    disk_path = cfg.get("default", "disk_path")
+    if not os.path.exists("/dev/md0"):
         out, err = sudo("{0} {1}".format(mdconfig_cmd, disk_path))
         if err is not None:
             print("Error: {0}".format(out))
             sys.exit(1)
 
+
 def loadJails():
     jails = []
-    existing_jails = cfg.get('default', 'jails').split(',')
+    existing_jails = cfg.get("default", "jails").split(",")
     try:
-        existing_jails.remove(cfg.get('default', 'jails_disabled'))
-    except:
+        existing_jails.remove(cfg.get("default", "jails_disabled"))
+    except Exception:
         pass
 
     for jail in existing_jails:
-        name = cfg.get(jail, 'name')
-        arch = cfg.get(jail, 'arch')
-        version = cfg.get(jail, 'version')
-        jails.append({'name': name, 'arch': arch, 'version': version})
+        name = cfg.get(jail, "name")
+        arch = cfg.get(jail, "arch")
+        version = cfg.get(jail, "version")
+        jails.append({"name": name, "arch": arch, "version": version})
 
     return jails
 
+
 def isValidOrigin(origin: str):
     abspath = "{0}/{1}".format(PORTSDIR, origin)
 
     return os.path.isdir(abspath)
 
+
 def main():
     global cfg, debug
     cfg = configparser.ConfigParser()
     cfg.read_string(default_conf)
-    debug = cfg.getboolean('default', 'debug')
+    debug = cfg.getboolean("default", "debug")
     if debug:
-        print(cfg.get('default', 'disk_path'))
+        print(cfg.get("default", "disk_path"))
 
     runner = Prunner()
     if len(sys.argv) == 1:
         cwd = os.getcwd()
-        origin = "{0}/{1}".format(cwd.split('/')[-2], cwd.split('/')[-1])
+        origin = "{0}/{1}".format(cwd.split("/")[-2], cwd.split("/")[-1])
     else:
         origin = sys.argv[1]
 
     if isValidOrigin(origin):
         runner.testPort(origin, runner.port_tree)
 
-if __name__ == '__main__':
-    main()
+    return 0
+
+
+if __name__ == "__main__":
+    sys.exit(main())