summaryrefslogtreecommitdiff
path: root/buildstream/_ostree.py
diff options
context:
space:
mode:
Diffstat (limited to 'buildstream/_ostree.py')
-rw-r--r--buildstream/_ostree.py81
1 files changed, 80 insertions, 1 deletions
diff --git a/buildstream/_ostree.py b/buildstream/_ostree.py
index 6fee37dc0..217790d84 100644
--- a/buildstream/_ostree.py
+++ b/buildstream/_ostree.py
@@ -1,4 +1,3 @@
-#!/usr/bin/env python3
#
# Copyright (C) 2017 Codethink Limited
#
@@ -150,6 +149,42 @@ def exists(repo, ref):
return has_object
+# remove():
+#
+# Removes the given commit or symbolic ref from the repo.
+#
+# Args:
+# repo (OSTree.Repo): The repo
+# ref (str): A commit checksum or symbolic ref
+# defer_prune (bool): Whether to defer pruning to the caller. NOTE:
+# The space won't be freed until you manually
+# call repo.prune.
+#
+# Returns:
+# (int|None) The amount of space pruned from the repository in
+# Bytes, or None if defer_prune is True
+#
+def remove(repo, ref, *, defer_prune=False):
+
+ # Get the commit checksum, this will:
+ #
+ # o Return a commit checksum if ref is a symbolic branch
+ # o Return the same commit checksum if ref is a valid commit checksum
+ # o Return None if the ostree repo doesnt know this ref.
+ #
+ check = checksum(repo, ref)
+ if check is None:
+ raise OSTreeError("Could not find artifact for ref '{}'".format(ref))
+
+ repo.set_ref_immediate(None, ref, None)
+
+ if not defer_prune:
+ _, _, _, pruned = repo.prune(OSTree.RepoPruneFlags.REFS_ONLY, -1)
+ return pruned
+
+ return None
+
+
# checksum():
#
# Returns the commit checksum for a given symbolic ref,
@@ -275,3 +310,47 @@ 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_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))]
+
+
+# _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