Skip to content

Commit 7e66a6e

Browse files
author
Korney Gedert
committed
Fix apply process for install/remove with invalid packages
1 parent 39f8b02 commit 7e66a6e

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

gpoa/apt1_runner

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# You should have received a copy of the GNU General Public License
1818
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1919

20+
import rpm
2021
from gpoa.storage import registry_factory
2122
from util.util import get_uid_by_username, string_to_literal_eval
2223
import logging
@@ -27,7 +28,24 @@ import locale
2728
import dbus
2829
import dbus.mainloop.glib
2930

30-
def remove_prefix(item):
31+
def is_rpm_installed(rpm_name):
32+
"""
33+
Check if the package named 'rpm_name' is installed
34+
"""
35+
ts = rpm.TransactionSet()
36+
pm = ts.dbMatch('name', rpm_name)
37+
if pm.count() > 0:
38+
return True
39+
40+
return False
41+
42+
def is_rpm_notinstalled(rpm_name):
43+
"""
44+
Check if the package named 'rpm_name' is not installed
45+
"""
46+
return not is_rpm_installed(rpm_name)
47+
48+
def remove_suffix(item):
3149
return '-' + str(item)
3250

3351
class Apt_applier:
@@ -58,7 +76,8 @@ class Apt_applier:
5876

5977
self.remove_packages = set(map(str.strip, string_to_literal_eval(dict_packages.get(remove_key_name,[]))))
6078
self.install_packages = map(str.strip, string_to_literal_eval(dict_packages.get(install_key_name,[])))
61-
self.install_packages = set([item for item in self.install_packages if item not in self.remove_packages])
79+
self.install_packages = set(filter(is_rpm_notinstalled, [item for item in self.install_packages if item not in self.remove_packages]))
80+
self.remove_packages = filter(is_rpm_installed, self.remove_packages)
6281

6382
def apply(self):
6483
"""
@@ -69,7 +88,17 @@ class Apt_applier:
6988
response
7089
"""
7190
# TODO: add package install/remove logging
72-
response = self.apt_iface.ApplyAsync(" ".join(self.remove_packages), " ".join(map(str, self.install_packages)) + " ".join(map(remove_prefix, self.remove_packages)))
91+
response = self.apt_iface.ApplyAsync(" ".join(self.remove_packages),
92+
" ".join(map(str, self.install_packages)) + " ".join(
93+
map(remove_suffix, self.remove_packages)))
94+
if response != 0:
95+
remove_packages = filter(is_rpm_installed, self.remove_packages)
96+
install_packages = filter(is_rpm_notinstalled, self.install_packages)
97+
for package in remove_packages:
98+
self.apt_iface.ApplyAsync(package, remove_suffix(package))
99+
for package in install_packages:
100+
self.apt_iface.ApplyAsync(package, package)
101+
73102
print(f"ApplyAsync started with response code: {response}")
74103
return response
75104

0 commit comments

Comments
 (0)