[libvirt PATCH 3/4] ci: list-images: Split some generic logic to a util module

Erik Skultety posted 4 patches 4 years, 12 months ago
[libvirt PATCH 3/4] ci: list-images: Split some generic logic to a util module
Posted by Erik Skultety 4 years, 12 months ago
Parts of the code can be reused by another registry checker script
introduced in the next patch.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
---
 ci/containers/util.py | 25 +++++++++++++++++++++++++
 ci/list-images.py     | 43 +++++++++++++++++++++++--------------------
 2 files changed, 48 insertions(+), 20 deletions(-)
 create mode 100644 ci/containers/util.py

diff --git a/ci/containers/util.py b/ci/containers/util.py
new file mode 100644
index 0000000000..1ce326bba2
--- /dev/null
+++ b/ci/containers/util.py
@@ -0,0 +1,25 @@
+import json
+import urllib.request as urllib
+
+
+PROJECT_ID = 192693
+
+
+def get_registry_uri(project_id,
+                     base_uri="https://gitlab.com",
+                     api_version=4):
+    uri = f"{base_uri}/api/v{str(api_version)}/projects/{project_id}/registry/repositories"
+    return uri
+
+
+def list_images(uri):
+    """
+    Returns all container images as currently available for the given GitLab
+    project.
+
+    ret: list of container image names
+    """
+    r = urllib.urlopen(uri)
+
+    # read the HTTP response and load the JSON part of it
+    return json.loads(r.read().decode())
diff --git a/ci/list-images.py b/ci/list-images.py
index 6862ac347f..1a1d87c10b 100644
--- a/ci/list-images.py
+++ b/ci/list-images.py
@@ -1,29 +1,32 @@
 #!/usr/bin/env python3
 
-import json
-import urllib.request as urllib
+import containers.util as util
 
-PROJECT_ID = 192693
-DOMAIN = "https://gitlab.com"
-API = "api/v4"
 
-uri = f"{DOMAIN}/{API}/projects/{PROJECT_ID}/registry/repositories"
-r = urllib.urlopen(uri + "?per_page=100")
+def list_image_names(uri):
+    images = util.list_images(uri)
 
-# read the HTTP response and load the JSON part of it
-data = json.loads(r.read().decode())
+    # skip the "ci-" prefix each of our container images' name has
+    names = [i["name"][3:] for i in images]
+    names.sort()
 
-# skip the "ci-" prefix each of our container images' name has
-names = [i["name"][3:] for i in data]
-names.sort()
+    return names
 
-names_native = [name for name in names if "cross" not in name]
-names_cross = [name for name in names if "cross" in name]
 
-print("Available x86 container images:\n")
-print("\t" + "\n\t".join(names_native))
+def main():
+    names = list_image_names(util.get_registry_uri(util.PROJECT_ID) + "?per_page=100")
 
-if names_cross:
-    print()
-    print("Available cross-compiler container images:\n")
-    print("\t" + "\n\t".join(names_cross))
+    names_native = [name for name in names if "cross" not in name]
+    names_cross = [name for name in names if "cross" in name]
+
+    print("Available x86 container images:\n")
+    print("\t" + "\n\t".join(names_native))
+
+    if names_cross:
+        print()
+        print("Available cross-compiler container images:\n")
+        print("\t" + "\n\t".join(names_cross))
+
+
+if __name__ == "__main__":
+    main()
-- 
2.29.2

Re: [libvirt PATCH 3/4] ci: list-images: Split some generic logic to a util module
Posted by Andrea Bolognani 4 years, 11 months ago
On Wed, 2021-02-10 at 18:24 +0100, Erik Skultety wrote:
> +++ b/ci/list-images.py
> @@ -1,29 +1,32 @@
>  #!/usr/bin/env python3
>  
> -import json
> -import urllib.request as urllib
> +import containers.util as util

As we add more Python code here, I think it makes sense to have a
single util module in the top-level ci/ directory instead of one for
the ci/containers/ directory and potentially another one for the
ci/cirrus directory.

-- 
Andrea Bolognani / Red Hat / Virtualization