3 # SPDX-License-Identifier: GPL-3.0
7 # Email : mk419@fascode.net
9 # (c) 2019-2021 Fascode Network.
15 from argparse import ArgumentParser, RawTextHelpFormatter, SUPPRESS
16 from os.path import abspath, dirname
17 from pathlib import Path
18 from subprocess import run
19 from typing import Optional
24 0 (latest) The latest package is installed
25 1 (noversion) Failed to get the latest version of the package, but the package is installed
26 2 (nomatch) The version of the package installed in local does not match one of the latest
27 3 (failed) Package not installed
33 from pyalpm import find_satisfier, Package
34 from pycman.config import init_with_config
39 def msg(string: str, level: str) -> None:
41 run([f"{script_dir}/msg.sh", "-a", "package", "-s", "8", level, string])
44 def get_from_localdb(package: str) -> Optional[Package]:
45 localdb = handle.get_localdb()
46 pkg = localdb.get_pkg(package)
51 for pkg in localdb.search(package):
52 if package in pkg.provides:
56 def get_from_syncdb(package: str) -> Optional[Package]:
57 for db in handle.get_syncdbs():
58 pkg = db.get_pkg(package)
64 def compare(package: str) -> tuple[int,Optional[tuple[str]]]:
65 pkg_from_local = get_from_localdb(package)
66 pkg_from_sync = get_from_syncdb(pkg_from_local.name) if pkg_from_local else None
68 if not pkg_from_local:
69 msg(f"{package} is not installed", "error")
72 elif not pkg_from_sync:
73 msg(f"Failed to get the latest version of {package}", "warn")
75 return (1, (pkg_from_local.version))
77 if pkg_from_local.version == pkg_from_sync.version:
78 msg(f"Latest {package} {pkg_from_local.version} is installed", "debug")
80 return (0, (pkg_from_local.version))
82 msg(f"The version of {package} does not match one of the latest", "warn")
83 msg(f"Local: {pkg_from_local.version} Latest: {pkg_from_sync.version}", "warn")
85 return (2, (pkg_from_local.version, pkg_from_sync.version))
88 if __name__ == "__main__":
89 script_dir = dirname(abspath(__file__))
91 parser = ArgumentParser(
92 usage = f"{sys.argv[0]} [option] [package]",
93 description = "Check the status of the specified package",
94 formatter_class = RawTextHelpFormatter,
106 default = Path("/etc/pacman.conf"),
108 help = "Path of pacman configuration file"
113 action = "store_true",
114 help = "Enable script mode"
117 args = parser.parse_args()
120 msg("pyalpm is not installed.", "error")
123 handle = init_with_config(str(args.conf))
125 exit_code, info = compare(args.package)
127 if args.script and info: