Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from pkgcore.restrictions import packages, values
from pkgcore.test import (
silence_logging, TestRestriction, TestCase, malleable_obj, callback_logger)
class AlwaysSelfIntersect(values.base):
def intersect(self, other):
return self
__hash__ = object.__hash__
class native_PackageRestrictionTest(TestRestriction):
if packages.native_PackageRestriction is packages.PackageRestriction_base:
kls = packages.PackageRestriction
else:
class kls(packages.native_PackageRestriction,
packages.PackageRestriction_mixin):
__slots__ = ()
__inst_caching__ = packages.PackageRestriction.__inst_caching__
kls = staticmethod(kls)
@silence_logging(log.logging.root)
def test_matching(self):
strexact = values.StrExactMatch
args = malleable_obj(category="foon", package="dar")
self.assertMatches(self.kls("category", strexact("foon")), args)
self.assertMatches(self.kls("package", strexact("dar")), args)
self.assertNotMatches(self.kls("package", strexact("dar"), negate=True),
if '*' in subslot:
if r is not None:
restrictions.append(packages.PackageRestriction("subslot", r))
else:
restrictions.append(restricts.SubSlotDep(subslot))
tsplit = text.rsplit("/", 1)
if len(tsplit) == 1:
ops, text = collect_ops(text)
if not ops:
if "*" in text:
r = convert_glob(text)
if r is None:
restrictions.append(packages.AlwaysTrue)
else:
restrictions.append(packages.PackageRestriction("package", r))
if len(restrictions) == 1:
return restrictions[0]
return packages.AndRestriction(*restrictions)
elif text.startswith("*"):
raise ParseError(
f"cannot do prefix glob matches with version ops: {orig_text}")
# ok... fake category. whee.
try:
r = list(collect_package_restrictions(
atom.atom(f"{ops}category/{text}").restrictions,
attrs=("category",), invert=True))
except errors.MalformedAtom as e:
e.atom = orig_text
raise ParseError(str(e)) from e
if not restrictions and len(r) == 1:
return r[0]
slot, _sep, subslot = slot.partition('/')
if slot:
restrictions.append(restricts.SlotDep(slot))
if subslot:
restrictions.append(restricts.SubSlotDep(subslot))
tsplit = text.rsplit("/", 1)
if len(tsplit) == 1:
ops, text = collect_ops(text)
if not ops:
if "*" in text:
r = convert_glob(text)
if r is None:
restrictions.append(packages.AlwaysTrue)
else:
restrictions.append(packages.PackageRestriction("package", r))
if len(restrictions) == 1:
return restrictions[0]
return packages.AndRestriction(*restrictions)
elif text.startswith("*"):
raise ParseError(
"cannot do prefix glob matches with version ops: %s" % (
orig_text,))
# ok... fake category. whee.
try:
r = list(collect_package_restrictions(
atom.atom("%scategory/%s" % (ops, text)).restrictions,
attrs=("category",), invert=True))
except errors.MalformedAtom as e:
e.atom = orig_text
raise_from(ParseError(str(e)))
if not restrictions and len(r) == 1:
raise_from(ParseError(str(e)))
if not restrictions and len(r) == 1:
return r[0]
restrictions.extend(r)
return packages.AndRestriction(*restrictions)
elif text[0] in "=<>~" or "*" not in text:
try:
return atom.atom(orig_text)
except errors.MalformedAtom as e:
raise_from(ParseError(str(e)))
r = map(convert_glob, tsplit)
if not r[0] and not r[1]:
restrictions.append(packages.AlwaysTrue)
elif not r[0]:
restrictions.append(packages.PackageRestriction("package", r[1]))
elif not r[1]:
restrictions.append(packages.PackageRestriction("category", r[0]))
else:
restrictions.extend((
packages.PackageRestriction("category", r[0]),
packages.PackageRestriction("package", r[1]),
))
if len(restrictions) == 1:
return restrictions[0]
return packages.AndRestriction(*restrictions)
def revdep_pkgs_finalize(sequence, namespace):
if not sequence:
return []
l = []
for atom_inst in sequence:
for repo in namespace.repos:
l.extend(repo.itermatch(atom_inst))
# have our pkgs; now build the restrict.
any_restrict = values.AnyMatch(
values.FunctionRestriction(partial(_revdep_pkgs_match, tuple(l))))
r = values.FlatteningRestriction(atom.atom, any_restrict)
return list(packages.PackageRestriction(dep, r)
for dep in ('bdepend', 'depend', 'rdepend', 'pdepend'))
except errors.MalformedAtom as e:
if '*' not in text:
raise ParseError(str(e)) from e
# support globbed targets with version restrictions
return packages.AndRestriction(*parse_globbed_version(text, orig_text))
r = list(map(convert_glob, tsplit))
if not r[0] and not r[1]:
restrictions.append(packages.AlwaysTrue)
elif not r[0]:
restrictions.append(packages.PackageRestriction("package", r[1]))
elif not r[1]:
restrictions.append(packages.PackageRestriction("category", r[0]))
else:
restrictions.extend((
packages.PackageRestriction("category", r[0]),
packages.PackageRestriction("package", r[1]),
))
if len(restrictions) == 1:
return restrictions[0]
return packages.AndRestriction(*restrictions)
def parse_eapi(value):
"""Value is matched against package EAPI versions."""
return packages.PackageRestriction(
'eapi',
values.StrExactMatch(value))
return packages.AndRestriction(*restrictions)
elif text[0] in atom.valid_ops or '*' not in text:
# possibly a valid atom object
try:
return atom.atom(orig_text)
except errors.MalformedAtom as e:
if '*' not in text:
raise ParseError(str(e)) from e
# support globbed targets with version restrictions
return packages.AndRestriction(*parse_globbed_version(text, orig_text))
r = list(map(convert_glob, tsplit))
if not r[0] and not r[1]:
restrictions.append(packages.AlwaysTrue)
elif not r[0]:
restrictions.append(packages.PackageRestriction("package", r[1]))
elif not r[1]:
restrictions.append(packages.PackageRestriction("category", r[0]))
else:
restrictions.extend((
packages.PackageRestriction("category", r[0]),
packages.PackageRestriction("package", r[1]),
))
if len(restrictions) == 1:
return restrictions[0]
return packages.AndRestriction(*restrictions)
def parse_owns(value):
return packages.PackageRestriction(
'contents',
values.AnyMatch(values.GetAttrRestriction(
'location', values.StrExactMatch(value))))
def generate_mangled_blocker(self, choices, blocker):
"""converts a blocker into a "cannot block ourself" block"""
# note the second Or clause is a bit loose; allows any version to
# slip through instead of blocking everything that isn't the
# parent pkg
if blocker.category != 'virtual':
return blocker
return packages.AndRestriction(blocker,
packages.PackageRestriction("provider.key",
values.StrExactMatch(choices.current_pkg.key),
negate=True, ignore_missing=True))