srcinfo-cache: include extra metadata extracted from the PKGBUILDs

Bash variables starting with msys2_ or mingw_ in the PKGBUILD will
now be included in the srcinfo cache under "extra". This supports
both strings and arrays of strings.
This commit is contained in:
Christoph Reiter committed 2023-09-17 17:35:24 +02:00
1 parent e9c4c4228b
commit 5be6f1b262
4 files changed
+120 -1

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env python3
from msys2_devtools import srcinfo_cache
srcinfo_cache.run()
+27
View File
@@ -0,0 +1,27 @@
from typing import Collection, Dict, Union
import json
import subprocess
import os
PKGBUILD2JSON = os.path.join(os.path.dirname(__file__), "pkgbuild2json.sh")
def get_extra_meta_for_pkgbuild(msys2_root: str, pkgbuild_path: str) -> \
Dict[str, Union[str, Collection[str], Dict[str, Union[str, None]]]]:
"""Returns a dict with the MSYS2 specific metadata from the PKGBUILD file"""
executable = os.path.join(msys2_root, 'usr', 'bin', 'bash.exe')
prefixes = ["mingw_", "msys2_"]
out = subprocess.check_output(
[executable, PKGBUILD2JSON, pkgbuild_path] + prefixes,
text=True, encoding="utf-8")
data = json.loads(out)
meta = {}
for key, value in data.items():
if key == "mingw_arch":
key = "msys2_environments"
if key.startswith("msys2_"):
key = key.split("_", 1)[-1]
meta[key] = value
return meta
+84
View File
@@ -0,0 +1,84 @@
#!/bin/bash
# Sources a PKGBUILD and outputs a json representation of all variables in it.
# Usage: ./src2json.sh PKGBUILD [prefix1 prefix2...]
# Example Output:
# {
# "prefix1_name1": "value1",
# "prefix2_name2": ["element1", "element2", ...]
# }
set -e
escape_json_string() {
local json_string
json_string="${1//\\/\\\\}"
json_string="${json_string//\"/\\\"}"
json_string="${json_string//$'\n'/\\n}"
json_string="${json_string//$'\t'/\\t}"
json_string="${json_string//$'\r'/\\r}"
echo -n "$json_string"
}
# Given a variable name returns a json value representation of it
get_variable_as_json() {
local var_name="$1"
if [[ "$(declare -p "$var_name" 2>/dev/null)" =~ "declare -a" ]]; then
local -n var_ref="$var_name"
local elements=""
for element in "${var_ref[@]}"; do
local escaped_element
escaped_element="$(escape_json_string "$element")"
elements+="\"$escaped_element\","
done
elements="${elements%,*}"
echo "[$elements]"
else
local var_value
local escape_value
eval "var_value=\$$var_name"
escape_value="$(escape_json_string "$var_value")"
echo "\"$escape_value\""
fi
}
check_prefixes() {
local variable="$1"
shift
for prefix in "$@"; do
if [[ "$variable" == "$prefix"* ]]; then
return 0
fi
done
return 1
}
# Given a file name returns a json representation of all variables in it.
# Supports strings and arrays of strings.
get_variables_as_json() {
set +e
source "$1"
set -e
shift
local variable_names
variable_names=$(compgen -v)
local json=""
for var in $variable_names; do
# the main reason we don't just extract all is to make it faster
# with cygwin bash
if check_prefixes "$var" "$@"; then
json+="\"$(escape_json_string "$var")\": $(get_variable_as_json "$var"),"
continue
fi
done
json="{${json%,*}}"
echo -n "$json"
}
main() {
local pkgbuild="$1"
shift
get_variables_as_json "$pkgbuild" "$@"
}
main "$@"
+4 -1
View File
@@ -12,6 +12,7 @@ from concurrent.futures import ThreadPoolExecutor
from functools import partial
from typing import List, Iterator, Tuple, Dict, Optional, Union, Collection, Sequence, Any
from .pkgbuild import get_extra_meta_for_pkgbuild
CacheEntry = Dict[str, Union[str, Collection[str]]]
@@ -127,7 +128,9 @@ def get_srcinfo_for_pkgbuild(msys2_root: str, args: Tuple[str, str]) -> Optional
["git", "log", "-1", "--format=%aI", git_path],
cwd=git_cwd).decode("utf-8").strip()
meta = {"repo": repo, "path": relpath, "date": date, "srcinfo": srcinfos}
extra_meta = get_extra_meta_for_pkgbuild(msys2_root, pkgbuild_path)
meta = {"repo": repo, "path": relpath, "date": date, "srcinfo": srcinfos, "extra": extra_meta}
except subprocess.CalledProcessError as e:
print("ERROR: %s %s" % (pkgbuild_path, e.output.splitlines()))
return None