Skip to content

Commit 609362f

Browse files
committed
Merge branch 'Issue-245' into dev
2 parents cc8de38 + 71526e2 commit 609362f

File tree

2 files changed

+83
-85
lines changed

2 files changed

+83
-85
lines changed

build.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ check_bool() {
353353
prepare_env() {
354354
# Check packages
355355
if [[ "${nodepend}" = false ]]; then
356-
local _check_failed=false _pkg _result=0 _version
356+
local _check_failed=false _pkg _result=0 _version _local _latest
357357
msg_info "Checking dependencies ..."
358358
for _pkg in ${dependence[@]}; do
359359
msg_debug -n "Checking ${_pkg} ..."
@@ -366,8 +366,10 @@ prepare_env() {
366366
echo; msg_warn "Failed to get the latest version of ${_pkg}."
367367
;;
368368
"2")
369+
_local="$(echo "${_version}" | getclm 1)"
370+
_latest="$(echo "${_version}" | getclm 2)"
369371
[[ "${debug}" = true ]] && echo -ne " ${_result[1]}\n"
370-
msg_warn "The version of ${_pkg} installed in local does not match one of the latest.\nLocal: ${_result[1]} Latest: ${_result[2]}"
372+
msg_warn "The version of ${_pkg} installed in local does not match one of the latest.\nLocal: ${_local} Latest: ${_latest}"
371373
;;
372374
"3")
373375
[[ "${debug}" = true ]] && echo

tools/package.py

Lines changed: 79 additions & 83 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 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
2320

21+
pyalpm_error = False
2422
epilog = """
2523
exit code:
2624
0 (latest) The latest package is installed
@@ -30,105 +28,103 @@
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 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.py", level, string])
42+
43+
44+
def get_from_localdb(package: str) -> Optional[Package]:
45+
localdb = handle.get_localdb()
46+
pkg = localdb.get_pkg(package)
3847

39-
def msg_error(string):
40-
subprocess.run([f"{script_dir}/msg.sh", "-a", "package.py", "error", string])
48+
if pkg:
49+
return pkg
50+
else:
51+
for pkg in localdb.search(package):
52+
if package in pkg.provides:
53+
return pkg
4154

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

45-
def get_from_syncdb(package):
46-
for db in syncdbs:
56+
def get_from_syncdb(package: str) -> Optional[Package]:
57+
for db in handle.get_syncdbs():
4758
pkg = db.get_pkg(package)
4859

49-
if pkg is not None: break
50-
51-
return pkg
60+
if pkg:
61+
return pkg
62+
5263

53-
def compare(package):
64+
def compare(package: str) -> tuple[int,Optional[tuple[str]]]:
5465
pkg_from_local = get_from_localdb(package)
55-
pkg_from_sync = get_from_syncdb(package)
66+
pkg_from_sync = get_from_syncdb(pkg_from_local.name) if pkg_from_local else None
5667

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
68+
if not pkg_from_local:
69+
msg(f"{package} is not installed.", "error")
6270

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
71+
return (3, None)
72+
elif not pkg_from_sync:
73+
msg(f"Failed to get the latest version of {package}.", "warn")
6874

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

7377
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
78+
msg(f"The latest version of {package} is installed.", "info")
79+
80+
return (0, (pkg_from_local.version))
7881
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
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 (1, (pkg_from_local.version, pkg_from_sync.version))
86+
8387

8488
if __name__ == "__main__":
85-
script_dir = os.path.dirname(os.path.abspath(__file__))
89+
script_dir = dirname(abspath(__file__))
8690

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
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
9296
)
9397

9498
parser.add_argument(
9599
"package",
96-
type=str,
97-
help=argparse.SUPPRESS
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"
98109
)
99110

100111
parser.add_argument(
101112
"-s", "--script",
102-
action="store_true",
103-
help="Enable script mode"
113+
action = "store_true",
114+
help = "Enable script mode"
104115
)
105116

106117
args = parser.parse_args()
107118

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)
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(" ".join(info))
129+
130+
sys.exit(exit_code)

0 commit comments

Comments
 (0)