Skip to content

Commit

Permalink
Black formatting for yum package module python script
Browse files Browse the repository at this point in the history
Ticket: ENT-12538
Changelog: none
(cherry picked from commit ce1fac4)
  • Loading branch information
craigcomstock committed Dec 19, 2024
1 parent 6c95ef5 commit e564ce3
Showing 1 changed file with 45 additions and 20 deletions.
65 changes: 45 additions & 20 deletions modules/packages/vendored/yum.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ import subprocess
import re


rpm_cmd = os.environ.get('CFENGINE_TEST_RPM_CMD', "/bin/rpm")
rpm_cmd = os.environ.get("CFENGINE_TEST_RPM_CMD", "/bin/rpm")
rpm_quiet_option = ["--quiet"]
rpm_output_format = "Name=%{name}\nVersion=%{epoch}:%{version}-%{release}\nArchitecture=%{arch}\n"
rpm_output_format = (
"Name=%{name}\nVersion=%{epoch}:%{version}-%{release}\nArchitecture=%{arch}\n"
)

yum_cmd = os.environ.get('CFENGINE_TEST_YUM_CMD', "/usr/bin/yum")
yum_cmd = os.environ.get("CFENGINE_TEST_YUM_CMD", "/usr/bin/yum")
yum_options = ["--quiet", "-y"]

NULLFILE = open(os.devnull, 'w')
NULLFILE = open(os.devnull, "w")


redirection_is_broken_cached = -1


def redirection_is_broken():
# Older versions of Python have a bug where it is impossible to redirect
# stderr using subprocess, and any attempt at redirecting *anything*, not
Expand All @@ -41,7 +44,12 @@ def redirection_is_broken():


def subprocess_Popen(cmd, stdout=None, stderr=None):
if not redirection_is_broken() or (stdout is None and stderr is None) or stdout == subprocess.PIPE or stderr == subprocess.PIPE:
if (
not redirection_is_broken()
or (stdout is None and stderr is None)
or stdout == subprocess.PIPE
or stderr == subprocess.PIPE
):
return subprocess.Popen(cmd, stdout=stdout, stderr=stderr)

old_stdout_fd = -1
Expand Down Expand Up @@ -87,14 +95,17 @@ def get_package_data():
# Absolute file.
sys.stdout.write("PackageType=file\n")
sys.stdout.flush()
process = subprocess_Popen([rpm_cmd, "--qf", rpm_output_format, "-qp", pkg_string], stdout=subprocess.PIPE)
process = subprocess_Popen(
[rpm_cmd, "--qf", rpm_output_format, "-qp", pkg_string],
stdout=subprocess.PIPE,
)
(stdoutdata, _) = process.communicate()

if process.returncode != 0:
return process.returncode

for line in stdoutdata.decode("utf-8").splitlines():
sys.stdout.write(line.replace("(none):","") + "\n")
sys.stdout.write(line.replace("(none):", "") + "\n")

return 0
elif re.search("[:,]", pkg_string):
Expand All @@ -111,14 +122,16 @@ def list_installed():
# Ignore everything.
sys.stdin.readlines()

process = subprocess_Popen([rpm_cmd, "-qa", "--qf", rpm_output_format], stdout=subprocess.PIPE)
process = subprocess_Popen(
[rpm_cmd, "-qa", "--qf", rpm_output_format], stdout=subprocess.PIPE
)
(stdoutdata, _) = process.communicate()

if process.returncode != 0:
return process.returncode

for line in stdoutdata.decode("utf-8").splitlines():
sys.stdout.write(line.replace("(none):","") + "\n")
sys.stdout.write(line.replace("(none):", "") + "\n")

return 0

Expand All @@ -128,7 +141,7 @@ def list_updates(online):
for line in sys.stdin:
line = line.strip()
if line.startswith("options="):
option = line[len("options="):]
option = line[len("options=") :]
if option.startswith("-"):
yum_options.append(option)
elif option.startswith("enablerepo=") or option.startswith("disablerepo="):
Expand All @@ -138,7 +151,9 @@ def list_updates(online):
if not online:
online_flag = ["-C"]

process = subprocess_Popen([yum_cmd] + yum_options + online_flag + ["check-update"], stdout=subprocess.PIPE)
process = subprocess_Popen(
[yum_cmd] + yum_options + online_flag + ["check-update"], stdout=subprocess.PIPE
)
(stdoutdata, _) = process.communicate()
# analyze return code from `yum check-update`:
# 0 means no updates
Expand All @@ -147,7 +162,9 @@ def list_updates(online):
if process.returncode == 1 and not online:
# If we get an error when listing local updates, try again using the
# online method, so that the cache is generated
process = subprocess_Popen([yum_cmd] + yum_options + ["check-update"], stdout=subprocess.PIPE)
process = subprocess_Popen(
[yum_cmd] + yum_options + ["check-update"], stdout=subprocess.PIPE
)
(stdoutdata, _) = process.communicate()
if process.returncode != 100:
# either there were no updates or error happened
Expand All @@ -170,7 +187,9 @@ def list_updates(online):
continue

lastline = ""
match = re.match(r"^(?P<name>\S+)\.(?P<arch>[^.\s]+)\s+(?P<version>\S+)\s+\S+\s*$", line)
match = re.match(
r"^(?P<name>\S+)\.(?P<arch>[^.\s]+)\s+(?P<version>\S+)\s+\S+\s*$", line
)
if match is not None:
sys.stdout.write("Name=" + match.group("name") + "\n")
sys.stdout.write("Version=" + match.group("version") + "\n")
Expand All @@ -192,8 +211,9 @@ def one_package_argument(name, arch, version, is_yum_install):
archs.append(arch)

if is_yum_install:
process = subprocess_Popen([rpm_cmd, "--qf", "%{arch}\n",
"-q", name], stdout=subprocess.PIPE)
process = subprocess_Popen(
[rpm_cmd, "--qf", "%{arch}\n", "-q", name], stdout=subprocess.PIPE
)
existing_archs = [line.decode("utf-8").rstrip() for line in process.stdout]
process.wait()
if process.returncode == 0 and existing_archs:
Expand Down Expand Up @@ -236,21 +256,23 @@ def package_arguments_builder(is_yum_install):
name = ""
version = ""
arch = ""
single_cmd_args = [] # List of arguments
multi_cmd_args = [] # List of lists of arguments
single_cmd_args = [] # List of arguments
multi_cmd_args = [] # List of lists of arguments
old_name = ""
for line in sys.stdin:
line = line.strip()
if line.startswith("options="):
option = line[len("options="):]
option = line[len("options=") :]
if option.startswith("-"):
yum_options.append(option)
elif option.startswith("enablerepo=") or option.startswith("disablerepo="):
yum_options.append("--" + option)
if line.startswith("Name="):
if name:
# Each new "Name=" triggers a new entry.
single_list, multi_list = one_package_argument(name, arch, version, is_yum_install)
single_list, multi_list = one_package_argument(
name, arch, version, is_yum_install
)
single_cmd_args += single_list
if name == old_name:
# Packages that differ only by architecture should be
Expand All @@ -273,7 +295,9 @@ def package_arguments_builder(is_yum_install):
arch = line.split("=", 1)[1].rstrip()

if name:
single_list, multi_list = one_package_argument(name, arch, version, is_yum_install)
single_list, multi_list = one_package_argument(
name, arch, version, is_yum_install
)
single_cmd_args += single_list
if name == old_name:
# Packages that differ only by architecture should be
Expand Down Expand Up @@ -454,4 +478,5 @@ def main():
sys.stderr.write("Invalid operation\n")
return 2


sys.exit(main())

0 comments on commit e564ce3

Please sign in to comment.