[libvirt PATCH 12/13] scripts: factor repeated path joins from selinux helper

Daniel P. Berrangé berrange at redhat.com
Fri Aug 6 17:48:09 UTC 2021


The os.path.join() function is called repeatedly for the same base
file. Factoring this out into named variables makes the code easier
to understand.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 scripts/selinux-compile-policy.py | 34 ++++++++++++++++---------------
 1 file changed, 18 insertions(+), 16 deletions(-)

diff --git a/scripts/selinux-compile-policy.py b/scripts/selinux-compile-policy.py
index 4550f3e7cb..911f358ba1 100755
--- a/scripts/selinux-compile-policy.py
+++ b/scripts/selinux-compile-policy.py
@@ -80,19 +80,21 @@ for name in ["iferror.m4", "all_interfaces.conf"] + tmpfiles:
 
 # tmp/all_interfaces.conf
 # echo "ifdef(\`__if_error',\`m4exit(1)')" > $5/iferror.m4
-with open(os.path.join(tmpdir, "iferror.m4"), "w") as file:
+iferror = os.path.join(tmpdir, "iferror.m4")
+with open(iferror, "w") as file:
     file.write("ifdef(`__if_error',`m4exit(1)')\n")
 
 # echo "divert(-1)" > $5/all_interfaces.conf
-with open(os.path.join(tmpdir, "all_interfaces.conf"), "w") as int_file:
+all_interfaces = os.path.join(tmpdir, "all_interfaces.conf")
+with open(all_interfaces, "w") as int_file:
     int_file.write("divert(-1)\n")
 
 # m4 $M4SUPPORT $HEADER_INTERFACES $2 $5/iferror.m4
 #   | sed -e s/dollarsstar/\$\$\*/g >> $5/all_interfaces.conf
 m4_run = subprocess.run(r"m4 {} | sed -e s/dollarsstar/\$\$\*/g >> {}".format(
                         " ".join([*m4support, *header_interfaces, policy_if,
-                                  os.path.join(tmpdir, "iferror.m4")]),
-                        os.path.join(tmpdir, "all_interfaces.conf")),
+                                  iferror]),
+                        all_interfaces),
                         shell=True, check=True, stderr=subprocess.PIPE,
                         universal_newlines=True)
 
@@ -106,38 +108,38 @@ for line in m4_run.stderr.split('\n'):
 
 # doesn't work properly without "shell=True"
 #    m4_process = Popen(["m4", *m4support, *header_interfaces, policy_if,
-#                        os.path.join(tmpdir, "iferror.m4")],
+#                        iferror],
 #                       stdout=PIPE, stderr=PIPE)
 #    sed_process = Popen(["sed", "-e", "s/dollarsstar/\$\$\*/g"],
 #                        stdin=m4_process.stdout, stdout=int_file)
 #    outs, errs = m4_process.communicate()
 
 # echo "divert" >> $5/all_interfaces.conf
-with open(os.path.join(tmpdir, "all_interfaces.conf"), "a") as file:
+with open(all_interfaces, "a") as file:
     file.write("divert\n")
 
 # tmp/%.mod
 # m4 $M4PARAM -s $M4SUPPORT $5/all_interfaces.conf $1 > $5/$MODULE_NAME.tmp
-with open(os.path.join(tmpdir, "{}.tmp".format(module_name)),
-          "w") as tmp_file:
+module_tmp = os.path.join(tmpdir, "{}.tmp".format(module_name))
+with open(module_tmp, "w") as tmp_file:
     subprocess.run(["m4", *m4param, "-s", *m4support,
-                    os.path.join(tmpdir, "all_interfaces.conf"),
-                    policy_te], stdout=tmp_file, check=True)
+                    all_interfaces, policy_te], stdout=tmp_file, check=True)
 
 # checkmodule -M -m $5/$MODULE_NAME.tmp -o $5/$MODULE_NAME.mod
+module_mod = os.path.join(tmpdir, "{}.mod".format(module_name))
 subprocess.run([checkmod_path,
                 "-M",
                 "-m",
-                os.path.join(tmpdir, "{}.tmp".format(module_name)),
+                module_tmp,
                 "-o",
-                os.path.join(tmpdir, "{}.mod".format(module_name))],
+                module_mod],
                check=True)
 
 
 # tmp/%.mod.fc
 # m4 $M4PARAM $M4SUPPORT $3 > $5/$MODULE_NAME.mod.fc
-with open(os.path.join(tmpdir,
-                       "{}.mod.fc".format(module_name)), "w") as mod_fc_file:
+module_mod_fc = os.path.join(tmpdir, "{}.mod.fc".format(module_name))
+with open(module_mod_fc, "w") as mod_fc_file:
     subprocess.run(["m4", *m4param, *m4support, policy_fc],
                    stdout=mod_fc_file, check=True)
 
@@ -148,7 +150,7 @@ subprocess.run([semod_path,
                 "-o",
                 policy_mod,
                 "-m",
-                os.path.join(tmpdir, "{}.mod".format(module_name)),
+                module_mod,
                 "-f",
-                os.path.join(tmpdir, "{}.mod.fc".format(module_name))],
+                module_mod_fc],
                check=True)
-- 
2.31.1




More information about the libvir-list mailing list