Skip to content

Commit 915c0ce

Browse files
committed
pm: use native binding to query packages ...
* Also queries if the packages have been installed via libapt-pkg * Automatically corrects APT/dpkg issues when detected * Caches query results obtained from libapt-pkg to speed up the query process
1 parent 91ff729 commit 915c0ce

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

acbs/pm.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ def fix_pm_states(escaped: List[str]):
4747
count = 0
4848
while count < 3:
4949
try:
50+
subprocess.check_call(['dpkg', '--configure', '-a'])
5051
subprocess.check_call(['apt-get', 'install', '-yf'])
51-
command = ['apt-get', 'install', '-y']
52-
command.extend(escaped)
53-
subprocess.check_call(command)
52+
if escaped:
53+
command = ['apt-get', 'install', '-y']
54+
command.extend(escaped)
55+
subprocess.check_call(command)
5456
return
5557
except subprocess.CalledProcessError:
5658
count += 1
@@ -63,6 +65,25 @@ def check_if_installed(name: str) -> bool:
6365
cached = installed_cache.get(name)
6466
if cached is not None:
6567
return cached
68+
if use_native_bindings:
69+
logging.debug('... using libapt-pkg')
70+
result = apt_check_if_available(name)
71+
if result == 0:
72+
installed_cache[name] = True
73+
return True
74+
elif result == 1:
75+
installed_cache[name] = False
76+
available_cache[name] = True
77+
return False
78+
elif result == 2:
79+
installed_cache[name] = False
80+
available_cache[name] = False
81+
return False
82+
elif result == -4:
83+
fix_pm_states([])
84+
return check_if_installed(name)
85+
else:
86+
raise RuntimeError(f'libapt-pkg binding returned error: {result}')
6687
try:
6788
subprocess.check_output(['dpkg', '-s', name], stderr=subprocess.STDOUT)
6889
installed_cache[name] = True

src/miniapt-query.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@ int check_available(const char *name)
3939
APT::CacheSetHelper helper(true, GlobalError::NOTICE);
4040
const char *list[2] = {name, NULL};
4141
APT::PackageList pkgset = APT::PackageList::FromCommandLine(cachefile, list, helper);
42+
// returns 0: installed; 1: not installed, available; 2: not installed, not available
4243
for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
4344
{
45+
if (Pkg->CurrentVer != 0) {
46+
return 0;
47+
}
4448
if (depCache->GetCandidateVersion(Pkg)) {
4549
if (depCache->MarkInstall(Pkg, true, 0, true)) {
4650
return 1;
4751
}
4852
}
4953
}
50-
return 0;
54+
return 2;
5155
}

0 commit comments

Comments
 (0)