Skip to content

Commit 8a4445a

Browse files
committed
[fix] : Fixed #245
1 parent 7e65351 commit 8a4445a

File tree

1 file changed

+67
-82
lines changed

1 file changed

+67
-82
lines changed

tools/package.py

Lines changed: 67 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,21 @@
44
#
55
# mk-linux419
66
# Twitter: @fascoder_4
7-
7+
88
#
99
# (c) 2019-2021 Fascode Network.
1010
#
1111
# package.py
1212
#
1313

14-
import argparse, os, subprocess, sys, time
15-
16-
repo_list = [
17-
"core",
18-
"extra",
19-
"community",
20-
"multilib",
21-
"alter-stable"
22-
]
14+
import sys
15+
from argparse import SUPPRESS, ArgumentParser, RawTextHelpFormatter
16+
from os.path import abspath, dirname
17+
from pathlib import Path
18+
from subprocess import run
19+
from typing import Optional
2320

21+
pyalpm_error = False
2422
epilog = """
2523
exit code:
2624
0 (latest) The latest package is installed
@@ -30,105 +28,92 @@
3028
4 Other error
3129
"""
3230

33-
def msg_info(string):
34-
subprocess.run([f"{script_dir}/msg.sh", "-a", "package.py", "info", string])
3531

36-
def msg_warn(string):
37-
subprocess.run([f"{script_dir}/msg.sh", "-a", "package.py", "warn", string])
32+
try:
33+
from pyalpm import Package
34+
from pycman.config import init_with_config
35+
except:
36+
pyalpm_error = True
37+
3838

39-
def msg_error(string):
40-
subprocess.run([f"{script_dir}/msg.sh", "-a", "package.py", "error", string])
39+
def msg(string: str, level: str) -> None:
40+
if not args.script:
41+
run([f"{script_dir}/msg.sh", "-a", "package.py", level, string])
4142

42-
def get_from_localdb(package):
43-
return localdb.get_pkg(package)
4443

45-
def get_from_syncdb(package):
46-
for db in syncdbs:
44+
def get_from_syncdb(package: str) -> Optional[Package]:
45+
for db in handle.get_syncdbs():
4746
pkg = db.get_pkg(package)
4847

49-
if pkg is not None: break
50-
48+
if pkg: break
49+
5150
return pkg
5251

53-
def compare(package):
54-
pkg_from_local = get_from_localdb(package)
52+
53+
def compare(package: str) -> tuple[int,Optional[tuple[str]]]:
54+
pkg_from_local = handle.get_localdb().get_pkg(package)
5555
pkg_from_sync = get_from_syncdb(package)
5656

57-
if pkg_from_local is None:
58-
# failed
59-
if not args.script:
60-
msg_error(f"{package} is not installed.")
61-
return 3
57+
if not pkg_from_local:
58+
msg(f"{package} is not installed.", "error")
6259

63-
elif pkg_from_sync is None:
64-
# noversion
65-
if not args.script:
66-
msg_warn(f"Failed to get the latest version of {package}.")
67-
return 1
60+
return (3, None)
61+
elif not pkg_from_sync:
62+
msg(f"Failed to get the latest version of {package}.", "warn")
6863

69-
# スクリプトモード時にパッケージバージョンを表示
70-
if args.script:
71-
print(pkg_from_local.version)
64+
return (2, None)
7265

7366
if pkg_from_local.version == pkg_from_sync.version:
74-
# latest
75-
if not args.script:
76-
msg_info(f"The latest version of {package} {pkg_from_local.version} is installed.")
77-
return 0
67+
msg(f"The latest version of {package} is installed.", "info")
68+
69+
return (0, (pkg_from_local.version))
7870
else:
79-
# nomatch
80-
if not args.script:
81-
msg_warn(f"The version of {package} does not match one of the latest.\nLocal: {pkg_from_local.version} Latest: {pkg_from_sync.version}")
82-
return 2
71+
msg(f"The version of {package} does not match one of the latest.", "warn")
72+
msg(f"Local: {pkg_from_local.version} Latest: {pkg_from_sync.version}", "warn")
73+
74+
return (1, (pkg_from_local.version, pkg_from_sync.version))
75+
8376

8477
if __name__ == "__main__":
85-
script_dir = os.path.dirname(os.path.abspath(__file__))
78+
script_dir = dirname(abspath(__file__))
8679

87-
parser = argparse.ArgumentParser(
88-
usage=f"{sys.argv[0]} [option] [package]",
89-
description="Check the status of the specified package",
90-
formatter_class=argparse.RawTextHelpFormatter,
91-
epilog=epilog
80+
parser = ArgumentParser(
81+
usage = f"{sys.argv[0]} [option] [package]",
82+
description = "Check the status of the specified package",
83+
formatter_class = RawTextHelpFormatter,
84+
epilog = epilog
9285
)
9386

9487
parser.add_argument(
9588
"package",
96-
type=str,
97-
help=argparse.SUPPRESS
89+
type = str,
90+
help = SUPPRESS
91+
)
92+
93+
parser.add_argument(
94+
"-c", "--conf",
95+
default = Path("/etc/pacman.conf"),
96+
type = Path,
97+
help = "Path of pacman configuration file"
9898
)
9999

100100
parser.add_argument(
101101
"-s", "--script",
102-
action="store_true",
103-
help="Enable script mode"
102+
action = "store_true",
103+
help = "Enable script mode"
104104
)
105105

106106
args = parser.parse_args()
107107

108-
try:
109-
import pyalpm
110-
except:
111-
if args.script:
112-
print("error")
113-
exit()
114-
else:
115-
msg_error("pyalpm is not installed.")
116-
sys.exit(4)
117-
118-
handle = pyalpm.Handle(".", "/var/lib/pacman")
119-
120-
for repo in repo_list:
121-
handle.register_syncdb(repo, 2048)
122-
123-
localdb = handle.get_localdb()
124-
syncdbs = handle.get_syncdbs()
125-
126-
#if args.script:
127-
# result = compare(args.package)
128-
# print(" ".join(result))
129-
#else:
130-
# return_code = compare(args.package)
131-
# sys.exit(return_code)
132-
133-
return_code = compare(args.package)
134-
sys.exit(return_code)
108+
if pyalpm_error:
109+
msg("pyalpm is not installed.", "error")
110+
sys.exit(4)
111+
112+
handle = init_with_config(str(args.conf))
113+
114+
exit_code, info = compare(args.package)
115+
116+
if args.script and info:
117+
print(" ".join(info))
118+
119+
sys.exit(exit_code)

0 commit comments

Comments
 (0)