You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by tv...@apache.org on 2021/02/04 07:12:03 UTC

[buildstream] 05/15: _ostree.py: Introduce list_all_refs() and list_artifacts()

This is an automated email from the ASF dual-hosted git repository.

tvb pushed a commit to branch jennis/136-clean-remote-cache
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit 82c7848a41218e51765d7cf0aa379d5ae3b2194f
Author: Tristan Maat <tm...@tlater.net>
AuthorDate: Sat Mar 24 17:56:56 2018 +0000

    _ostree.py: Introduce list_all_refs() and list_artifacts()
---
 buildstream/_ostree.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)

diff --git a/buildstream/_ostree.py b/buildstream/_ostree.py
index 246b54f..080447b 100644
--- a/buildstream/_ostree.py
+++ b/buildstream/_ostree.py
@@ -552,3 +552,67 @@ def configure_remote(repo, remote, url, key_url=None):
             repo.remote_gpg_import(remote, stream, None, 0, None)
         except GLib.GError as e:
             raise OSTreeError("Failed to add gpg key from url '{}': {}".format(key_url, e.message)) from e
+
+
+# list_all_refs():
+#
+# Create a list of all refs.
+#
+# Args:
+#    repo (OSTree.Repo): The repo
+#
+# Returns:
+#    (dict): A dict of refs to checksums.
+#
+def list_all_refs(repo):
+    try:
+        _, refs = repo.list_refs(None)
+        return refs
+    except GLib.GError as e:
+        raise OSTreeError(message=e.message) from e
+
+
+# list_remote_refs():
+#
+# Fetch list of refs from a remote.
+#
+# Args:
+#    repo (OSTree.Repo): The repo
+#    remote (str): An optional remote name, defaults to 'origin'
+#
+# Returns:
+#    (dict): A dict of refs to checksums.
+#
+def list_remote_refs(repo, remote="origin"):
+    try:
+        _, refs = repo.remote_list_refs(remote)
+        return refs
+    except GLib.GError as e:
+        (_, remote_url) = repo.remote_get_url(remote)
+        raise OSTreeError(message="{} when attempting to fetch from {}".format(e.message, remote_url)) from e
+
+
+# list_artifacts():
+#
+# List cached artifacts in Least Recently Modified (LRM) order.
+#
+# Returns:
+#     (list) - A list of refs in LRM order
+#
+def list_artifacts(repo):
+    # string of: /path/to/repo/refs/heads
+    ref_heads = os.path.join(repo.get_path().get_path(), 'refs', 'heads')
+
+    # obtain list of <project>/<element>/<key>
+    refs = list_all_refs(repo).keys()
+
+    mtimes = []
+    for ref in refs:
+        ref_path = os.path.join(ref_heads, ref)
+        if os.path.exists(ref_path):
+            # Obtain the mtime (the time a file was last modified)
+            mtimes.append(os.path.getmtime(ref_path))
+
+    # NOTE: Sorted will sort from earliest to latest, thus the
+    # first element of this list will be the file modified earliest.
+    return [ref for _, ref in sorted(zip(mtimes, refs))]