sync archpkg.py

This commit is contained in:
lilydjwg 2023-08-26 10:32:38 +08:00
parent fa4f743045
commit 954068b000

View file

@ -1,17 +1,19 @@
from __future__ import annotations
import os import os
from collections import namedtuple from collections import namedtuple
import subprocess import subprocess
import re import re
from typing import List, Dict from typing import List, Dict
from pyalpm import vercmp import pyalpm
class PkgNameInfo(namedtuple('PkgNameInfo', 'name, version, release, arch')): class PkgNameInfo(namedtuple('PkgNameInfo', 'name, version, release, arch')):
def __lt__(self, other) -> bool: def __lt__(self, other) -> bool:
if self.name != other.name or self.arch != other.arch: if self.name != other.name or self.arch != other.arch:
return NotImplemented return NotImplemented
if self.version != other.version: if self.version != other.version:
return vercmp(self.version, other.version) < 0 return pyalpm.vercmp(self.version, other.version) < 0
return float(self.release) < float(other.release) return float(self.release) < float(other.release)
def __gt__(self, other) -> bool: def __gt__(self, other) -> bool:
@ -36,52 +38,24 @@ def get_pkgname_with_bash(PKGBUILD: str) -> List[str]:
. '%s' . '%s'
echo ${pkgname[*]}''' % PKGBUILD echo ${pkgname[*]}''' % PKGBUILD
# Python 3.4 has 'input' arg for check_output # Python 3.4 has 'input' arg for check_output
p = subprocess.Popen(['bash'], stdin=subprocess.PIPE, p = subprocess.Popen(
stdout=subprocess.PIPE) ['bwrap', '--unshare-all', '--ro-bind', '/', '/', '--tmpfs', '/home',
output = p.communicate(script.encode('latin1'))[0].decode('latin1') '--tmpfs', '/run', '--die-with-parent',
'--tmpfs', '/tmp', '--proc', '/proc', '--dev', '/dev', '/bin/bash'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
)
output = p.communicate(script.encode())[0].decode()
ret = p.wait() ret = p.wait()
if ret != 0: if ret != 0:
raise subprocess.CalledProcessError( raise subprocess.CalledProcessError(
ret, ['bash'], output) ret, ['bash'], output)
return output.split() return output.split()
def _run_bash(script: str) -> None:
p = subprocess.Popen(['bash'], stdin=subprocess.PIPE)
p.communicate(script.encode('latin1'))
ret = p.wait()
if ret != 0:
raise subprocess.CalledProcessError(
ret, ['bash'])
def get_aur_pkgbuild_with_bash(name: str) -> None:
script = '''\
. /usr/lib/yaourt/util.sh
. /usr/lib/yaourt/aur.sh
init_color
aur_get_pkgbuild '%s' ''' % name
_run_bash(script)
def get_abs_pkgbuild_with_bash(name: str) -> None:
script = '''\
. /usr/lib/yaourt/util.sh
. /usr/lib/yaourt/abs.sh
init_paths
init_color
arg=$(pacman -Sp --print-format '%%r/%%n' '%s')
RSYNCOPT="$RSYNCOPT -O"
abs_get_pkgbuild "$arg" ''' % name
_run_bash(script)
pkgfile_pat = re.compile(r'(?:^|/).+-[^-]+-[\d.]+-(?:\w+)\.pkg\.tar\.(?:xz|zst)$') pkgfile_pat = re.compile(r'(?:^|/).+-[^-]+-[\d.]+-(?:\w+)\.pkg\.tar\.(?:xz|zst)$')
def _strip_ver(s: str) -> str: def _strip_ver(s: str) -> str:
return re.sub(r'[<>=].*', '', s) return re.sub(r'[<>=].*', '', s)
def get_package_dependencies(name: str) -> List[str]:
outb = subprocess.check_output(["package-query", "-Sii", "-f", "%D", name])
out = outb.decode('latin1')
return [_strip_ver(x) for x in out.split() if x != '-']
def get_package_info(name: str, local: bool = False) -> Dict[str, str]: def get_package_info(name: str, local: bool = False) -> Dict[str, str]:
old_lang = os.environ['LANG'] old_lang = os.environ['LANG']
os.environ['LANG'] = 'C' os.environ['LANG'] = 'C'
@ -105,14 +79,3 @@ def get_package_info(name: str, local: bool = False) -> Dict[str, str]:
ret[key] += ' ' + l.strip() ret[key] += ' ' + l.strip()
return ret return ret
def get_package_repository(name: str) -> str:
try:
out = subprocess.check_output(["package-query", "-Sii", "-f", "%r", name])
repo = out.strip().decode('latin1')
except subprocess.CalledProcessError:
repo = 'local'
return repo
def is_official(name: str) -> bool:
repo = get_package_repository(name)
return repo in ('core', 'extra', 'community', 'multilib', 'testing')