From c64a85dd7524546864603b955f601bf64c9a4bcf Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Thu, 15 Aug 2019 12:50:22 +0200 Subject: Add patterns for the existing CacheFilter::Matcher classes This implements the basic logic patterns: ?and ?false ?not ?or ?true and the basic package patterns: ?architecture ?name ?x-name-fnmatch --- apt-pkg/cachefilter-patterns.cc | 30 ++++++++++++++++++ doc/apt-patterns.7.xml | 38 +++++++++++++++++++++-- test/integration/test-apt-patterns | 62 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 2 deletions(-) diff --git a/apt-pkg/cachefilter-patterns.cc b/apt-pkg/cachefilter-patterns.cc index ea4cf3cd8..dd5d741d3 100644 --- a/apt-pkg/cachefilter-patterns.cc +++ b/apt-pkg/cachefilter-patterns.cc @@ -208,6 +208,36 @@ std::unique_ptr PatternParser::aPattern(std::unique_p if (node == nullptr) nodeP->error("Expected a pattern"); + if (node->matches("?architecture", 1, 1)) + return std::make_unique(aWord(node->arguments[0])); + if (node->matches("?false", 0, 0)) + return std::make_unique(); + if (node->matches("?name", 1, 1)) + return std::make_unique(aWord(node->arguments[0])); + if (node->matches("?not", 1, 1)) + return std::make_unique(aPattern(node->arguments[0]).release()); + if (node->matches("?true", 0, 0)) + return std::make_unique(); + if (node->matches("?x-name-fnmatch", 1, 1)) + return std::make_unique(aWord(node->arguments[0])); + + // Variable argument patterns + if (node->matches("?and", 0, -1)) + { + auto pattern = std::make_unique(); + for (auto &arg : node->arguments) + pattern->AND(aPattern(arg).release()); + return pattern; + } + if (node->matches("?or", 0, -1)) + { + auto pattern = std::make_unique(); + + for (auto &arg : node->arguments) + pattern->OR(aPattern(arg).release()); + return pattern; + } + node->error(rstrprintf("Unrecognized pattern '%s'", node->term.to_string().c_str())); return nullptr; diff --git a/doc/apt-patterns.7.xml b/doc/apt-patterns.7.xml index 079c493e4..5aa352f03 100644 --- a/doc/apt-patterns.7.xml +++ b/doc/apt-patterns.7.xml @@ -36,12 +36,46 @@ - Syntax + Logic patterns + + These patterns provide the basic means to combine other patterns into + more complex expressions, as well as ?true and ?false + patterns. + + ?and(PATTERN, PATTERN, ...) + Selects objects where all specified patterns match. + + ?false + Selects nothing. + + ?not(PATTERN) + Selects objects where PATTERN does not match. + + ?or(PATTERN, PATTERN, ...) + Selects objects where at least one of the specified patterns match. + + ?true + Selects all objects. + + + + + Package patterns + + These patterns select specific packages. + + + ?architecture(WILDCARD) + Selects packages matching the specified architecture, which may contain wildcards using any. + + ?name(REGEX) + Selects packages where the name matches the given regular expression. + - + Examples diff --git a/test/integration/test-apt-patterns b/test/integration/test-apt-patterns index c33fa1bf8..0d7b1540d 100755 --- a/test/integration/test-apt-patterns +++ b/test/integration/test-apt-patterns @@ -59,3 +59,65 @@ E: Unable to locate package ?not-a-pattern E: Couldn't find any package by glob '?not-a-pattern' E: Regex compilation error - Invalid preceding regular expression E: Couldn't find any package by regex '?not-a-pattern'" apt install -s '?not-a-pattern' + + +msgmsg "Ensure that argument lists are present where needed, and absent elsewhere" + +testfailureequal "Listing... +E: input:0-7: error: ?true does not expect an argument list + ?true() + ^^^^^^^" apt list '?true()' +testfailureequal "Listing... +E: input:0-4: error: ?and expects an argument list + ?and + ^^^^" apt list '?and' +testfailureequal "Listing... +E: input:0-3: error: ?or expects an argument list + ?or + ^^^" apt list '?or' + + +msgmsg "Basic logic: true, false, not, ?or, ?and" +for pattern in '?true' '?not(?false)'; do +testsuccessequal "Listing... +automatic1/now 1.0 i386 [installed,local] +automatic2/now 1.0 i386 [installed,local] +available/unstable 1.0 all +broken/now 1.0 i386 [installed,local] +conf-only/now 1.0 i386 [residual-config] +dpkg/now 1.16.2+fake all [installed,local] +essential/now 1.0 i386 [installed,local] +foreign/unstable 2.0 amd64 +manual1/now 1.0 i386 [installed,local] +manual2/now 1.0 i386 [installed,local] +not-obsolete/unstable 2.0 i386 [upgradable from: 1.0]" apt list "$pattern" +done +testsuccessequal "Listing..." apt list '?false' +testsuccessequal "Listing..." apt list '?not(?true)' +testsuccessequal "Listing... +automatic1/now 1.0 i386 [installed,local] +automatic2/now 1.0 i386 [installed,local] +manual1/now 1.0 i386 [installed,local] +manual2/now 1.0 i386 [installed,local]" apt list '?or(?name(^automatic),?name(^manual))' +testsuccessequal "Listing... +automatic1/now 1.0 i386 [installed,local]" apt list '?and(?name(^automatic),?name(1$))' + + +msgmsg "Package patterns" + +testsuccessequal "Listing... +foreign/unstable 2.0 amd64" apt list '?architecture(amd64)' + +# XXX FIXME We should have support for foreign and native +testsuccessequal "Listing..." apt list '?architecture(foreign)' +testsuccessequal "Listing..." apt list '?architecture(native)' + + +testsuccessequal "Listing... +automatic1/now 1.0 i386 [installed,local] +automatic2/now 1.0 i386 [installed,local]" apt list '?name(^automatic)' + +testsuccessequal "Listing..." apt list '?x-name-fnmatch(1)' +testsuccessequal "Listing... +automatic1/now 1.0 i386 [installed,local] +manual1/now 1.0 i386 [installed,local]" apt list '?x-name-fnmatch(*1)' -- cgit v1.2.3-70-g09d2