initial import

This commit is contained in:
Fabio Valentini 2022-03-08 13:35:44 +01:00
commit 459b314606
Signed by: decathorpe
GPG key ID: 5AC5F572E5D410AF
4 changed files with 256 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/.idea/

24
README.md Normal file
View file

@ -0,0 +1,24 @@
# leafdrop
This script prints whether a package is a leaf package on i686.
If it is not, it prints the list of dependent packages.
## Requirements
- python3
- DNF
- internet connection
## Usage
`./leafdrop foo`
## TODO
- filter out source packages that already have `ExcludeArch: i686` (using
similar heuristics as in `repochecker`)
## License
Licensed under the terms of the UNLICENSE.

24
UNLICENSE Normal file
View file

@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>

207
leafdrop Executable file
View file

@ -0,0 +1,207 @@
#!/usr/bin/python3
import argparse
import shutil
import subprocess
from typing import Dict, List, Tuple
def refresh_cache():
cmd = [
"dnf", "--quiet",
"--installroot", "/tmp/dnf/",
"--forcearch", "i686",
"--releasever", "rawhide",
"--disablerepo", "*",
"--repofrompath", "kojipkgs,https://kojipkgs.fedoraproject.org/repos/rawhide/latest/$basearch/",
"--repofrompath", "kojipkgs-source,https://kojipkgs.fedoraproject.org/repos/rawhide/latest/src/",
"makecache",
"--refresh",
]
ret = subprocess.run(cmd, stdout=subprocess.PIPE)
ret.check_returncode()
def clean_cache():
shutil.rmtree("/tmp/dnf/")
def get_b2s_mapping() -> Dict[str, str]:
cmd = [
"dnf", "--quiet",
"--installroot", "/tmp/dnf/",
"--forcearch", "i686",
"--releasever", "rawhide",
"--disablerepo", "*",
"--repofrompath", "kojipkgs,https://kojipkgs.fedoraproject.org/repos/rawhide/latest/$basearch/",
"--repofrompath", "kojipkgs-source,https://kojipkgs.fedoraproject.org/repos/rawhide/latest/src/",
"repoquery",
"--arch", "i686,noarch",
"--queryformat", "%{name} %{source_name}",
]
ret = subprocess.run(cmd, stdout=subprocess.PIPE)
ret.check_returncode()
mapping = dict()
for line in ret.stdout.decode().strip().splitlines():
name, source_name = line.split(" ")
mapping[name] = source_name
return mapping
def get_s2b_mapping(b2s: Dict[str, str]) -> Dict[str, List[str]]:
# calculate reverse mapping
s2b = dict()
for binary in b2s.keys():
source = b2s[binary]
if source not in s2b.keys():
s2b[source] = list()
s2b[source].append(binary)
return s2b
def parse_nevra(nevra: str) -> Tuple[str, str, str, str, str]:
try:
nevr, a = nevra.rsplit(".", 1)
n, ev, r = nevr.rsplit("-", 2)
except ValueError:
raise ValueError(f"Invalid nevra: {nevra}")
if ":" in ev:
e, v = ev.split(":")
else:
e = "0"
v = ev
return n, e, v, r, a
def get_requires(package: str) -> Tuple[List[str], List[str]]:
cmd = [
"dnf", "--quiet",
"--installroot", "/tmp/dnf/",
"--forcearch", "i686",
"--releasever", "rawhide",
"--disablerepo", "*",
"--repofrompath", "kojipkgs,https://kojipkgs.fedoraproject.org/repos/rawhide/latest/$basearch/",
"--repofrompath", "kojipkgs-source,https://kojipkgs.fedoraproject.org/repos/rawhide/latest/src/",
"repoquery",
"--arch", "src,i686,noarch",
"--alldeps",
"--whatrequires", package
]
ret = subprocess.run(cmd, stdout=subprocess.PIPE)
ret.check_returncode()
deps: List[str] = ret.stdout.decode().strip().splitlines()
requires: List[str] = list()
build_requires: List[str] = list()
for dep in deps:
n, e, v, r, a = parse_nevra(dep)
if a == "src":
build_requires.append(n)
else:
requires.append(n)
# TODO: filter out packages that already have ExcludeArch: i686
requires.sort()
build_requires.sort()
return requires, build_requires
def get_all_requires(package: str, b2s, s2b) -> Dict[str, Tuple[List[str], List[str]]]:
binaries = s2b[package]
results = dict()
for binary in binaries:
results[binary] = get_requires(binary)
return results
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument(
"package",
help="Source name of the package to check for leaf status on i686"
)
parser.add_argument(
"--clean-cache",
action="store_true",
help="Delete DNF metadata after executing the query."
)
args = parser.parse_args()
refresh_cache()
b2s = get_b2s_mapping()
s2b = get_s2b_mapping(b2s)
if args.package not in s2b.keys():
print(f"Package {args.package} is not present in i686 repositories.")
if args.clean_cache:
clean_cache()
return 0
reqs = get_all_requires(args.package, b2s, s2b)
if all(len(value[0]) == 0 and len(value[1]) == 0 for value in reqs.values()):
print(f"YES: Package {args.package} is a leaf package on i686.")
if args.clean_cache:
clean_cache()
return 0
all_internal = True
for key, values in reqs.items():
requires, build_requires = values
if not all(name in s2b[args.package] for name in requires):
all_internal = False
if not all(name in s2b[args.package] for name in build_requires):
all_internal = False
if requires:
for name in requires:
print(f"- {key} is Required by: {name}")
if build_requires:
for name in build_requires:
print(f"- {key} is BuildRequired by: {name}")
if requires or build_requires:
print()
if all_internal:
print(f"YES: Package {args.package} is a leaf package on i686.")
else:
print(f"NO: Package {args.package} is not a leaf package on i686.")
if args.clean_cache:
clean_cache()
return 0
if __name__ == "__main__":
exit(main())