diff options
author | Julian Andres Klode <julian.klode@canonical.com> | 2019-08-15 12:50:22 +0200 |
---|---|---|
committer | Julian Andres Klode <julian.klode@canonical.com> | 2019-08-15 20:21:34 +0200 |
commit | c64a85dd7524546864603b955f601bf64c9a4bcf (patch) | |
tree | 1e97211ac7d8673715266f3702f4535d1cbb194d | |
parent | 08b61197f418883ea20563e2251fb60779c0ba87 (diff) | |
download | apt-c64a85dd7524546864603b955f601bf64c9a4bcf.tar.gz |
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
-rw-r--r-- | apt-pkg/cachefilter-patterns.cc | 30 | ||||
-rw-r--r-- | doc/apt-patterns.7.xml | 38 | ||||
-rwxr-xr-x | 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<APT::CacheFilter::Matcher> PatternParser::aPattern(std::unique_p if (node == nullptr) nodeP->error("Expected a pattern"); + if (node->matches("?architecture", 1, 1)) + return std::make_unique<APT::CacheFilter::PackageArchitectureMatchesSpecification>(aWord(node->arguments[0])); + if (node->matches("?false", 0, 0)) + return std::make_unique<APT::CacheFilter::FalseMatcher>(); + if (node->matches("?name", 1, 1)) + return std::make_unique<APT::CacheFilter::PackageNameMatchesRegEx>(aWord(node->arguments[0])); + if (node->matches("?not", 1, 1)) + return std::make_unique<APT::CacheFilter::NOTMatcher>(aPattern(node->arguments[0]).release()); + if (node->matches("?true", 0, 0)) + return std::make_unique<APT::CacheFilter::TrueMatcher>(); + if (node->matches("?x-name-fnmatch", 1, 1)) + return std::make_unique<APT::CacheFilter::PackageNameMatchesFnmatch>(aWord(node->arguments[0])); + + // Variable argument patterns + if (node->matches("?and", 0, -1)) + { + auto pattern = std::make_unique<APT::CacheFilter::ANDMatcher>(); + for (auto &arg : node->arguments) + pattern->AND(aPattern(arg).release()); + return pattern; + } + if (node->matches("?or", 0, -1)) + { + auto pattern = std::make_unique<APT::CacheFilter::ORMatcher>(); + + 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 @@ </refsect1> <refsect1> - <title>Syntax</title> + <title>Logic patterns</title> + <para> + These patterns provide the basic means to combine other patterns into + more complex expressions, as well as <code>?true</code> and <code>?false</code> + patterns. + </para> <variablelist> + <varlistentry><term><code>?and(PATTERN, PATTERN, ...)</code></term> + <listitem><para>Selects objects where all specified patterns match.</para></listitem> + </varlistentry> + <varlistentry><term><code>?false</code></term> + <listitem><para>Selects nothing.</para></listitem> + </varlistentry> + <varlistentry><term><code>?not(PATTERN)</code></term> + <listitem><para>Selects objects where PATTERN does not match.</para></listitem> + </varlistentry> + <varlistentry><term><code>?or(PATTERN, PATTERN, ...)</code></term> + <listitem><para>Selects objects where at least one of the specified patterns match.</para></listitem> + </varlistentry> + <varlistentry><term><code>?true</code></term> + <listitem><para>Selects all objects.</para></listitem> + </varlistentry> + </variablelist> + </refsect1> + <refsect1> + <title>Package patterns</title> + <para> + These patterns select specific packages. + </para> + <variablelist> + <varlistentry><term><code>?architecture(WILDCARD)</code></term> + <listitem><para>Selects packages matching the specified architecture, which may contain wildcards using any.</para></listitem> + </varlistentry> + <varlistentry><term><code>?name(REGEX)</code></term> + <listitem><para>Selects packages where the name matches the given regular expression.</para></listitem> + </varlistentry> </variablelist> - </refsect1> + <refsect1><title>Examples</title> </refsect1> 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)' |