[libvirt PATCH v2 4/6] ci: Introduce a util module

Erik Skultety eskultet at redhat.com
Tue Mar 16 17:32:58 UTC 2021


With the gradual rewrite of the Makefile to the 'helper' script will
require helper functions that would better live in a separate util
module.

Signed-off-by: Erik Skultety <eskultet at redhat.com>
---
 ci/util.py | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
 create mode 100644 ci/util.py

diff --git a/ci/util.py b/ci/util.py
new file mode 100644
index 0000000000..8a2d6d8f47
--- /dev/null
+++ b/ci/util.py
@@ -0,0 +1,39 @@
+import json
+import urllib.request as urllibrequest
+import urllib.parse as urllibparse
+
+from typing import Dict, List
+
+
+def get_registry_uri(namespace: str,
+                     gitlab_uri="https://gitlab.com",
+                     api_version=4) -> str:
+    """
+    :param namespace: GitLab project namespace, e.g. "libvirt/libvirt"
+    :param gitlab_uri: GitLab base URI, can be a private deployment
+    :param api_version: GitLab REST API version number
+    :return: URI pointing to a namespaced project's image registry
+    """
+
+    # this converts something like "libvirt/libvirt" to "libvirt%2Flibvirt"
+    namespace_urlenc = urllibparse.quote_plus(namespace)
+
+    apistr = str(api_version)
+    project_uri = f"{gitlab_uri}/api/v{apistr}/projects/{namespace_urlenc}"
+
+    uri = project_uri + "/registry/repositories"
+    return uri
+
+
+def get_registry_images(uri: str) -> Dict[str, str]:
+    """
+    Returns all container images as currently available for the given GitLab
+    project.
+
+    :return: list of container image names
+    """
+
+    r = urllibrequest.urlopen(uri + "?per_page=100")
+
+    # read the HTTP response and load the JSON part of it
+    return json.loads(r.read().decode())
-- 
2.29.2




More information about the libvir-list mailing list