Last active
April 1, 2022 22:09
-
-
Save SpotlightKid/c1cb5ba87eacaa97ac61a559d98ee3c6 to your computer and use it in GitHub Desktop.
Get info from a PKGBUILD file by calling makepkg --printsrcinfo and parsing the output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import json | |
import sys | |
from subprocess import CalledProcessError, run | |
ARRAY_FIELDS = ( | |
"arch", | |
"arch", | |
"b2sums", | |
"backup", | |
"checkdepends", | |
"conflicts", | |
"depends", | |
"groups", | |
"license", | |
"makedepends", | |
"md5sums", | |
"noextract", | |
"optdepends", | |
"options", | |
"provides", | |
"replaces", | |
"sha1sums", | |
"sha224sums", | |
"sha256sums", | |
"sha384sums", | |
"sha512sums", | |
"source", | |
"validpgpkeys", | |
) | |
def parse_srcinfo(srcinfo, array_fields=ARRAY_FIELDS): | |
results = {} | |
current_package = None | |
for line in srcinfo.splitlines(): | |
line = line.strip() | |
if not line or line.startswith("#"): | |
continue | |
key, value = (x.strip() for x in line.split("=", 1)) | |
key_noarch = key.split("_", 1)[0] | |
if key == "pkgbase": | |
info = {} | |
elif key == "pkgname": | |
if current_package is not None: | |
if not "packages" in results: | |
results["packages"] = {} | |
results["packages"][current_package] = info | |
else: | |
results = info | |
info = {} | |
current_package = value | |
if key in info: | |
if not isinstance(info[key], list): | |
info[key] = [info[key]] | |
info[key].append(value) | |
elif key_noarch in array_fields: | |
info[key] = [value] | |
else: | |
info[key] = value | |
if current_package is not None: | |
if not "packages" in results: | |
results["packages"] = {} | |
results["packages"][current_package] = info | |
return results | |
def get_srcinfo(): | |
proc = run(["makepkg", "--printsrcinfo"],capture_output=True, env={"LC_ALL": "C"}) | |
if proc.returncode != 0: | |
raise IOError("makepkg returned non-zero exit code: {}".format(proc.stderr.decode("utf-8"))) | |
else: | |
return proc.stdout.decode("utf-8") | |
def main(): | |
try: | |
srcinfo = get_srcinfo() | |
except OSError as exc: | |
return str(exc) | |
else: | |
data = parse_srcinfo(srcinfo) | |
print(json.dumps(data, indent=2)) | |
if __name__ == '__main__': | |
sys.exit(main() or 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment