OSDN Git Service

[update] : Added paru to exclude
[alterlinux/alterlinux.git] / tools / package.py
1 #!/usr/bin/env python3
2 #
3 # SPDX-License-Identifier: GPL-3.0
4 #
5 # mk-linux419
6 # Twitter: @fascoder_4
7 # Email  : mk419@fascode.net
8 #
9 # (c) 2019-2021 Fascode Network.
10 #
11 # package.py
12 #
13
14 import sys
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
20
21 pyalpm_error = False
22 epilog = """
23 exit code:
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
28   4                    Other error
29 """
30
31
32 try:
33     from pyalpm import find_satisfier, Package
34     from pycman.config import init_with_config
35 except:
36     pyalpm_error = True
37
38
39 def msg(string: str, level: str) -> None:
40     if not args.script:
41         run([f"{script_dir}/msg.sh", "-a", "package", "-s", "8", level, string])
42
43
44 def get_from_localdb(package: str) -> Optional[Package]:
45     localdb = handle.get_localdb()
46     pkg = localdb.get_pkg(package)
47
48     if pkg:
49         return pkg
50     else:
51         for pkg in localdb.search(package):
52             if package in pkg.provides:
53                 return pkg
54
55
56 def get_from_syncdb(package: str) -> Optional[Package]:
57     for db in handle.get_syncdbs():
58         pkg = db.get_pkg(package)
59
60         if pkg:
61             return pkg
62
63
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
67
68     if not pkg_from_local:
69         msg(f"{package} is not installed", "error")
70
71         return (3, None)
72     elif not pkg_from_sync:
73         msg(f"Failed to get the latest version of {package}", "warn")
74
75         return (1, (pkg_from_local.version))
76
77     if pkg_from_local.version == pkg_from_sync.version:
78         msg(f"Latest {package} {pkg_from_local.version} is installed", "debug")
79
80         return (0, (pkg_from_local.version))
81     else:
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")
84
85         return (2, (pkg_from_local.version, pkg_from_sync.version))
86
87
88 if __name__ == "__main__":
89     script_dir = dirname(abspath(__file__))
90
91     parser = ArgumentParser(
92         usage           = f"{sys.argv[0]} [option] [package]",
93         description     = "Check the status of the specified package",
94         formatter_class = RawTextHelpFormatter,
95         epilog          = epilog
96     )
97
98     parser.add_argument(
99         "package",
100         type = str,
101         help = SUPPRESS
102     )
103
104     parser.add_argument(
105         "-c", "--conf",
106         default = Path("/etc/pacman.conf"),
107         type    = Path,
108         help    = "Path of pacman configuration file"
109     )
110
111     parser.add_argument(
112         "-s", "--script",
113         action = "store_true",
114         help   = "Enable script mode"
115     )
116
117     args = parser.parse_args()
118
119     if pyalpm_error:
120         msg("pyalpm is not installed.", "error")
121         sys.exit(4)
122
123     handle = init_with_config(str(args.conf))
124
125     exit_code, info = compare(args.package)
126
127     if args.script and info:
128         print(info)
129
130     sys.exit(exit_code)