71 lines
1.5 KiB
Python
Executable file
71 lines
1.5 KiB
Python
Executable file
#! /usr/bin/env python3
|
|
# encoding: utf-8
|
|
|
|
import glob
|
|
import re
|
|
import sys
|
|
|
|
def is_a_version(token):
|
|
"""
|
|
Returns True iff token is a portage version spec (i.e. contains only numbers or ".").
|
|
"""
|
|
|
|
for A in token:
|
|
a = ord(A)
|
|
if not (0x30 <= a <= 0x39 or a == 0x2e):
|
|
return False
|
|
|
|
return True
|
|
|
|
def strip_version(atom):
|
|
"""
|
|
Removes the =version-123 spec from a portage atom.
|
|
|
|
Works like 99% of the time. Good enough for the girls I go out with.
|
|
"""
|
|
|
|
parts = atom[1:].split("-")
|
|
wanted = []
|
|
|
|
for part in parts:
|
|
if is_a_version(part):
|
|
break
|
|
|
|
wanted.append(part)
|
|
|
|
return "-".join(wanted)
|
|
|
|
def grep(exp, filename):
|
|
with open(filename) as f:
|
|
matches = re.findall(exp, f.read())
|
|
|
|
return matches
|
|
|
|
def find_reverse_deps(deps, version=True):
|
|
all_dep_files = glob.glob("/var/db/pkg/*/*/*DEPEND")
|
|
|
|
regexp = "(%s)[^:]*:[^/]+/[^=]+=" %("|".join(deps))
|
|
matching_files = set(f for f in all_dep_files if grep(regexp, f))
|
|
atoms = {"=" + f[12:].rsplit("/", 1)[0] for f in matching_files}
|
|
|
|
if not version:
|
|
atoms = {strip_version(a) for a in atoms}
|
|
|
|
return atoms
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) > 1:
|
|
if sys.argv[1] == "-v":
|
|
version = False
|
|
targets = sys.argv[2:]
|
|
else:
|
|
version = True
|
|
targets = sys.argv[1:]
|
|
|
|
atoms = find_reverse_deps(targets, version)
|
|
print(" ".join(sorted(atoms)))
|
|
else:
|
|
print("Usage: %s DEPENDENCY [more DEPENDENCIES...]" %(sys.argv[0]))
|
|
print("Finds all reverse deps of DEPENDENCY")
|
|
|
|
sys.exit(1)
|