This repository has been archived on 2021-06-12. You can view files and clone it, but cannot push or open issues or pull requests.
alhp/utils.py
2021-06-04 17:06:07 +02:00

65 lines
2.2 KiB
Python

import logging
import os
import pathlib
import re
import subprocess
from packaging.version import LegacyVersion
from srcinfo.parse import parse_srcinfo
regex_pkgrel = re.compile(r"^pkgrel\s*=\s*(.+)$", re.MULTILINE)
def import_keys(pkgbuild: str) -> bool:
parsed = parse_pkgbuild(pkgbuild)
if "validpgpkeys" in parsed:
for k in parsed["validpgpkeys"]:
s = subprocess.run(["gpg", "--keyserver", "keyserver.ubuntu.com", "--recv-keys", k],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
logging.debug("[GPG] %s", s.stdout.decode(errors='ignore'))
if s.returncode:
logging.warning("[GPG] Import of key %s failed: %s", k, s.stdout.decode(errors="ignore"))
return False
else:
logging.info("[GPG] Imported key %s", k)
return True
def increase_pkgrel(pkgbuild_file: str, parsed: dict = None) -> None:
if not parsed:
parsed = parse_pkgbuild(pkgbuild_file)
with open(pkgbuild_file, "r+", errors='ignore') as p:
pkgbuild_str = p.read()
p.seek(0)
p.truncate(0)
pkgbuild_str = regex_pkgrel.sub("pkgrel=" + parsed["pkgrel"] + ".1", pkgbuild_str)
p.write(pkgbuild_str)
def parse_pkgbuild(pkgbuild_file: str) -> dict:
pkgbuild_path = pathlib.Path(pkgbuild_file)
os.chdir(pkgbuild_path.parent)
res = subprocess.run(["makepkg", "--printsrcinfo"], check=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if res.returncode:
logging.warning("[PKGBUILD/%s] makepkg failed: %s", pkgbuild_path.name, res.stdout.decode(errors="ignore"))
return {}
(parsed, errors) = parse_srcinfo(res.stdout.decode(errors="ignore"))
if errors:
logging.warning("[PKGBUILD/%s] Failed to parse: %s", pkgbuild_path.name, errors)
return {}
return parsed
def parse_pkgbuild_ver(pkgbuild_file: str = None, parsed: dict = None) -> LegacyVersion:
if not parsed:
parsed = parse_pkgbuild(pkgbuild_file)
if "epoch" in parsed:
return LegacyVersion("{}:{}-{}".format(parsed["epoch"], parsed["pkgver"], parsed["pkgrel"]))
return LegacyVersion("{}-{}".format(parsed["pkgver"], parsed["pkgrel"]))