Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
"""
A wrapper of apt module that is actually usable.
"""
import utils as u
try:
import apt
import apt_pkg
except ModuleNotFoundError:
u.warning("apt python module not found")
exit(2)
class Apt:
cache: apt.cache.Cache
packages: list
installed_packages: list
removable_packages: list
upgradable_packages: list
def __init__(self, update: bool = False):
self.cache = self.get_cache(update)
self.packages = self.get_packages()
self.installed_packages = self.get_installed_packages()
self.removable_packages = self.get_removable_packages()
self.upgradable_packages = self.get_upgradable_packages()
def install(self, name: str) -> bool:
"""Install a package with APT.
:param name: Package name
:type name: str
:return: Wether installation is successful or not
:rtype: bool
"""
pkg = self.cache[name]
if not pkg.installed:
pkg.mark_install(auto_fix=False)
success = self.cache.commit()
self.reload()
return success
def remove(self, name: str) -> bool:
"""Remove a package with APT.
:param name: Package name
:type name: str
:return: Wether uninstallation is successful or not
:rtype: bool
"""
pkg = self.cache[name]
if pkg.installed:
pkg.mark_delete(auto_fix=False)
success = self.cache.commit()
self.reload()
return success
def reload(self):
"""Reload object."""
self.cache.clear()
self.__init__()
def get_cache(self, update: bool = False) -> apt.cache.Cache:
"""Get an eventually updated Cache object.
:param update: Wether to update cacheor not, default=False
:type update: bool
:return: An APT Cache object
:rtype: apt.cache.Cache
"""
apt_cache = apt.cache.Cache()
if update:
apt_cache.update()
apt_cache.open()
return apt_cache
def get_packages(self) -> list:
"""Get packages list.
:return: Packages list
:rtype: list
"""
packages = list(self.cache)
return packages
def get_installed_packages(self) -> list:
"""Get installed packages list.
:return: Installed packages list
:rtype: list
"""
installed_packages = [p for p in self.packages if p.is_installed]
return installed_packages
def get_removable_packages(self) -> list:
"""Get auto-removable packages list.
:return: Auto-removable packages list
:rtype: list
"""
removable_packages = [p for p in self.installed_packages if p.is_auto_removable]
return removable_packages
def get_upgradable_packages(self) -> list:
"""Get upgradable packages list.
:return: Upgradable packages list
:rtype: list
"""
upgradable_packages = [p for p in self.installed_packages if p.is_upgradable]
return upgradable_packages