@@ -9,18 +9,21 @@ import subprocess
99import re
1010
1111
12- rpm_cmd = os .environ .get (' CFENGINE_TEST_RPM_CMD' , "/bin/rpm" )
12+ rpm_cmd = os .environ .get (" CFENGINE_TEST_RPM_CMD" , "/bin/rpm" )
1313rpm_quiet_option = ["--quiet" ]
14- rpm_output_format = "Name=%{name}\n Version=%{version}-%{release}\n Architecture=%{arch}\n "
14+ rpm_output_format = (
15+ "Name=%{name}\n Version=%{epoch}:%{version}-%{release}\n Architecture=%{arch}\n "
16+ )
1517
16- yum_cmd = os .environ .get (' CFENGINE_TEST_YUM_CMD' , "/usr/bin/yum" )
18+ yum_cmd = os .environ .get (" CFENGINE_TEST_YUM_CMD" , "/usr/bin/yum" )
1719yum_options = ["--quiet" , "-y" ]
1820
19- NULLFILE = open (os .devnull , 'w' )
21+ NULLFILE = open (os .devnull , "w" )
2022
2123
2224redirection_is_broken_cached = - 1
2325
26+
2427def redirection_is_broken ():
2528 # Older versions of Python have a bug where it is impossible to redirect
2629 # stderr using subprocess, and any attempt at redirecting *anything*, not
@@ -41,7 +44,12 @@ def redirection_is_broken():
4144
4245
4346def subprocess_Popen (cmd , stdout = None , stderr = None ):
44- if not redirection_is_broken () or (stdout is None and stderr is None ) or stdout == subprocess .PIPE or stderr == subprocess .PIPE :
47+ if (
48+ not redirection_is_broken ()
49+ or (stdout is None and stderr is None )
50+ or stdout == subprocess .PIPE
51+ or stderr == subprocess .PIPE
52+ ):
4553 return subprocess .Popen (cmd , stdout = stdout , stderr = stderr )
4654
4755 old_stdout_fd = - 1
@@ -87,7 +95,19 @@ def get_package_data():
8795 # Absolute file.
8896 sys .stdout .write ("PackageType=file\n " )
8997 sys .stdout .flush ()
90- return subprocess_call ([rpm_cmd , "--qf" , rpm_output_format , "-qp" , pkg_string ])
98+ process = subprocess_Popen (
99+ [rpm_cmd , "--qf" , rpm_output_format , "-qp" , pkg_string ],
100+ stdout = subprocess .PIPE ,
101+ )
102+ (stdoutdata , _ ) = process .communicate ()
103+
104+ if process .returncode != 0 :
105+ return process .returncode
106+
107+ for line in stdoutdata .decode ("utf-8" ).splitlines ():
108+ sys .stdout .write (line .replace ("(none):" , "" ) + "\n " )
109+
110+ return 0
91111 elif re .search ("[:,]" , pkg_string ):
92112 # Contains an illegal symbol.
93113 sys .stdout .write (line + "ErrorMessage: Package string with illegal format\n " )
@@ -102,15 +122,26 @@ def list_installed():
102122 # Ignore everything.
103123 sys .stdin .readlines ()
104124
105- return subprocess_call ([rpm_cmd , "-qa" , "--qf" , rpm_output_format ])
125+ process = subprocess_Popen (
126+ [rpm_cmd , "-qa" , "--qf" , rpm_output_format ], stdout = subprocess .PIPE
127+ )
128+ (stdoutdata , _ ) = process .communicate ()
129+
130+ if process .returncode != 0 :
131+ return process .returncode
132+
133+ for line in stdoutdata .decode ("utf-8" ).splitlines ():
134+ sys .stdout .write (line .replace ("(none):" , "" ) + "\n " )
135+
136+ return 0
106137
107138
108139def list_updates (online ):
109140 global yum_options
110141 for line in sys .stdin :
111142 line = line .strip ()
112143 if line .startswith ("options=" ):
113- option = line [len ("options=" ):]
144+ option = line [len ("options=" ) :]
114145 if option .startswith ("-" ):
115146 yum_options .append (option )
116147 elif option .startswith ("enablerepo=" ) or option .startswith ("disablerepo=" ):
@@ -120,7 +151,9 @@ def list_updates(online):
120151 if not online :
121152 online_flag = ["-C" ]
122153
123- process = subprocess_Popen ([yum_cmd ] + yum_options + online_flag + ["check-update" ], stdout = subprocess .PIPE )
154+ process = subprocess_Popen (
155+ [yum_cmd ] + yum_options + online_flag + ["check-update" ], stdout = subprocess .PIPE
156+ )
124157 (stdoutdata , _ ) = process .communicate ()
125158 # analyze return code from `yum check-update`:
126159 # 0 means no updates
@@ -129,7 +162,9 @@ def list_updates(online):
129162 if process .returncode == 1 and not online :
130163 # If we get an error when listing local updates, try again using the
131164 # online method, so that the cache is generated
132- process = subprocess_Popen ([yum_cmd ] + yum_options + ["check-update" ], stdout = subprocess .PIPE )
165+ process = subprocess_Popen (
166+ [yum_cmd ] + yum_options + ["check-update" ], stdout = subprocess .PIPE
167+ )
133168 (stdoutdata , _ ) = process .communicate ()
134169 if process .returncode != 100 :
135170 # either there were no updates or error happened
@@ -152,7 +187,9 @@ def list_updates(online):
152187 continue
153188
154189 lastline = ""
155- match = re .match (r"^(?P<name>\S+)\.(?P<arch>[^.\s]+)\s+(?P<version>\S+)\s+\S+\s*$" , line )
190+ match = re .match (
191+ r"^(?P<name>\S+)\.(?P<arch>[^.\s]+)\s+(?P<version>\S+)\s+\S+\s*$" , line
192+ )
156193 if match is not None :
157194 sys .stdout .write ("Name=" + match .group ("name" ) + "\n " )
158195 sys .stdout .write ("Version=" + match .group ("version" ) + "\n " )
@@ -174,8 +211,9 @@ def one_package_argument(name, arch, version, is_yum_install):
174211 archs .append (arch )
175212
176213 if is_yum_install :
177- process = subprocess_Popen ([rpm_cmd , "--qf" , "%{arch}\n " ,
178- "-q" , name ], stdout = subprocess .PIPE )
214+ process = subprocess_Popen (
215+ [rpm_cmd , "--qf" , "%{arch}\n " , "-q" , name ], stdout = subprocess .PIPE
216+ )
179217 existing_archs = [line .decode ("utf-8" ).rstrip () for line in process .stdout ]
180218 process .wait ()
181219 if process .returncode == 0 and existing_archs :
@@ -218,21 +256,23 @@ def package_arguments_builder(is_yum_install):
218256 name = ""
219257 version = ""
220258 arch = ""
221- single_cmd_args = [] # List of arguments
222- multi_cmd_args = [] # List of lists of arguments
259+ single_cmd_args = [] # List of arguments
260+ multi_cmd_args = [] # List of lists of arguments
223261 old_name = ""
224262 for line in sys .stdin :
225263 line = line .strip ()
226264 if line .startswith ("options=" ):
227- option = line [len ("options=" ):]
265+ option = line [len ("options=" ) :]
228266 if option .startswith ("-" ):
229267 yum_options .append (option )
230268 elif option .startswith ("enablerepo=" ) or option .startswith ("disablerepo=" ):
231269 yum_options .append ("--" + option )
232270 if line .startswith ("Name=" ):
233271 if name :
234272 # Each new "Name=" triggers a new entry.
235- single_list , multi_list = one_package_argument (name , arch , version , is_yum_install )
273+ single_list , multi_list = one_package_argument (
274+ name , arch , version , is_yum_install
275+ )
236276 single_cmd_args += single_list
237277 if name == old_name :
238278 # Packages that differ only by architecture should be
@@ -255,7 +295,9 @@ def package_arguments_builder(is_yum_install):
255295 arch = line .split ("=" , 1 )[1 ].rstrip ()
256296
257297 if name :
258- single_list , multi_list = one_package_argument (name , arch , version , is_yum_install )
298+ single_list , multi_list = one_package_argument (
299+ name , arch , version , is_yum_install
300+ )
259301 single_cmd_args += single_list
260302 if name == old_name :
261303 # Packages that differ only by architecture should be
@@ -436,4 +478,5 @@ def main():
436478 sys .stderr .write ("Invalid operation\n " )
437479 return 2
438480
481+
439482sys .exit (main ())
0 commit comments