summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Andres Klode <julian.klode@canonical.com>2021-04-22 10:45:45 +0200
committerJulian Andres Klode <julian.klode@canonical.com>2021-04-23 12:26:46 +0200
commit4b5215e8e2e31637cb0998ecb80d3c3146760579 (patch)
tree0bf71b1dc2a60c88fca02c7fad1a567e1a15f218
parent949f3821268943149ddc26d4eaee3bfbaa1255a9 (diff)
json: Add origins fields to version
Provide access to the origins of a package, such that tools can display information about them; for example, you can write a hook counting security upgrades.
-rw-r--r--apt-private/private-json-hooks.cc35
-rw-r--r--doc/json-hooks-protocol.md10
-rwxr-xr-xtest/integration/test-apt-cli-json-hooks16
3 files changed, 55 insertions, 6 deletions
diff --git a/apt-private/private-json-hooks.cc b/apt-private/private-json-hooks.cc
index 6bf70b1c6..652a59812 100644
--- a/apt-private/private-json-hooks.cc
+++ b/apt-private/private-json-hooks.cc
@@ -199,6 +199,33 @@ class APT_HIDDEN JsonWriter
};
/**
+ * @brief Write a VerFileIterator to a JsonWriter
+ */
+static void verFiletoJson(JsonWriter &writer, CacheFile &, pkgCache::VerFileIterator const &vf)
+{
+ auto pf = vf.File(); // Packages file
+ auto rf = pf.ReleaseFile(); // release file
+
+ writer.beginObject();
+ if (not rf.end()) {
+ if (rf->Archive != 0)
+ writer.name("archive").value(rf.Archive());
+ if (rf->Codename != 0)
+ writer.name("codename").value(rf.Codename());
+ if (rf->Version != 0)
+ writer.name("version").value(rf.Version());
+ if (rf->Origin != 0)
+ writer.name("origin").value(rf.Origin());
+ if (rf->Label != 0)
+ writer.name("label").value(rf.Label());
+ if (rf->Site != 0)
+ writer.name("site").value(rf.Site());
+ }
+
+ writer.endObject();
+}
+
+/**
* @brief Write a VerIterator to a JsonWriter
*/
static void verIterToJson(JsonWriter &writer, CacheFile &Cache, pkgCache::VerIterator const &Ver)
@@ -208,6 +235,14 @@ static void verIterToJson(JsonWriter &writer, CacheFile &Cache, pkgCache::VerIte
writer.name("version").value(Ver.VerStr());
writer.name("architecture").value(Ver.Arch());
writer.name("pin").value(Cache->GetPolicy().GetPriority(Ver));
+
+ writer.name("origins");
+ writer.beginArray();
+ for (auto vf = Ver.FileList(); !vf.end(); vf++)
+ if ((vf.File()->Flags & pkgCache::Flag::NotSource) == 0)
+ verFiletoJson(writer, Cache, vf);
+ writer.endArray();
+
writer.endObject();
}
diff --git a/doc/json-hooks-protocol.md b/doc/json-hooks-protocol.md
index 09633e71d..489b04843 100644
--- a/doc/json-hooks-protocol.md
+++ b/doc/json-hooks-protocol.md
@@ -106,6 +106,16 @@ install. Each package has the following attributes:
- *version*: The version as a string
- *architecture*: Architecture of the version
- *pin*: The pin priority (optional)
+ - *origins*: Sources from which the package is retrieved (optional, not in older versions)
+
+ Each origin is represented as an object with the following fields:
+
+ - *archive*: string (optional)
+ - *codename*: string (optional)
+ - *version*: string (optional)
+ - *origin*: string (optional)
+ - *label*: string (optional)
+ - *site*: string, empty for local repositories or when using mirror+file:/ method (optional)
#### Example
diff --git a/test/integration/test-apt-cli-json-hooks b/test/integration/test-apt-cli-json-hooks
index 298fae072..7169a04cb 100755
--- a/test/integration/test-apt-cli-json-hooks
+++ b/test/integration/test-apt-cli-json-hooks
@@ -4,6 +4,10 @@ set -e
TESTDIR="$(readlink -f "$(dirname "$0")")"
. "$TESTDIR/framework"
+getoriginfromsuite() { echo 'Oranges'; }
+getlabelfromsuite() { echo 'Lemons'; }
+getversionfromsuite() { echo 'Volkamer'; }
+
setupenvironment
configarchitecture "i386"
@@ -107,18 +111,18 @@ Building dependency tree...
HOOK: HELLO
HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}}
HOOK: empty
-HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.pre-prompt","params":{"command":"install","search-terms":["foo"],"unknown-packages":[],"packages":[{"id":1,"name":"foo","architecture":"i386","mode":"install","automatic":false,"versions":{"candidate":{"id":1,"version":"1.0","architecture":"all","pin":500},"install":{"id":1,"version":"1.0","architecture":"all","pin":500}}}]}}
+HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.pre-prompt","params":{"command":"install","search-terms":["foo"],"unknown-packages":[],"packages":[{"id":1,"name":"foo","architecture":"i386","mode":"install","automatic":false,"versions":{"candidate":{"id":1,"version":"1.0","architecture":"all","pin":500,"origins":[{"archive":"unstable","codename":"sid","origin":"Oranges","label":"Lemons","site":""}]},"install":{"id":1,"version":"1.0","architecture":"all","pin":500,"origins":[{"archive":"unstable","codename":"sid","origin":"Oranges","label":"Lemons","site":""}]}}}]}}
HOOK: empty
HOOK: BYE
The following NEW packages will be installed:
foo
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
-Inst foo (1.0 unstable [all])
-Conf foo (1.0 unstable [all])
+Inst foo (1.0 Lemons:unstable [all])
+Conf foo (1.0 Lemons:unstable [all])
HOOK: HELLO
HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.hello","id":0,"params":{"versions":["0.1"]}}
HOOK: empty
-HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.post","params":{"command":"install","search-terms":["foo"],"unknown-packages":[],"packages":[{"id":1,"name":"foo","architecture":"i386","mode":"install","automatic":false,"versions":{"candidate":{"id":1,"version":"1.0","architecture":"all","pin":500},"install":{"id":1,"version":"1.0","architecture":"all","pin":500}}}]}}
+HOOK: request {"jsonrpc":"2.0","method":"org.debian.apt.hooks.install.post","params":{"command":"install","search-terms":["foo"],"unknown-packages":[],"packages":[{"id":1,"name":"foo","architecture":"i386","mode":"install","automatic":false,"versions":{"candidate":{"id":1,"version":"1.0","architecture":"all","pin":500,"origins":[{"archive":"unstable","codename":"sid","origin":"Oranges","label":"Lemons","site":""}]},"install":{"id":1,"version":"1.0","architecture":"all","pin":500,"origins":[{"archive":"unstable","codename":"sid","origin":"Oranges","label":"Lemons","site":""}]}}}]}}
HOOK: empty
HOOK: BYE' apt install foo -s
@@ -203,8 +207,8 @@ Building dependency tree...
The following NEW packages will be installed:
foo
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
-Inst foo (1.0 unstable [all])
-Conf foo (1.0 unstable [all])' apt install foo -s
+Inst foo (1.0 Lemons:unstable [all])
+Conf foo (1.0 Lemons:unstable [all])' apt install foo -s