Skip to content

Instantly share code, notes, and snippets.

@giuseppe
Created February 23, 2016 12:39
Show Gist options
  • Save giuseppe/9164115cdb9976373798 to your computer and use it in GitHub Desktop.
Save giuseppe/9164115cdb9976373798 to your computer and use it in GitHub Desktop.
cleanup dockerimg spc
import os
import sys
import stat
import gi
gi.require_version('OSTree', '1.0')
from gi.repository import Gio, GLib, OSTree
repo = OSTree.Repo.new(Gio.File.new_for_path("/ostree/repo"))
repo.open(None)
refs = {}
app_refs = []
prefix = "dockerimg-"
def _get_commit_metadata (repo, rev, key):
commit = repo.load_commit(rev)[1]
metadata = commit.get_child_value(0)
if key not in metadata.keys():
return None
return metadata[key]
for i in repo.list_refs()[1]:
if i.startswith(prefix):
if len(i) == len(prefix) + 64:
refs[i] = False
else:
app_refs.append(i)
def visit(rev):
commit = repo.resolve_rev(rev, False)[1]
layer = _get_commit_metadata (repo, commit, "docker.layer")
refs["%s%s" % (prefix, layer)] = True
parent = _get_commit_metadata (repo, commit, "docker.parent")
if parent:
visit(parent)
for app in app_refs:
visit(app)
for k, v in refs.items():
print("%s > %s" % ( k, v))
if not v:
ref = OSTree.parse_refspec(k)
print("Deleting %s", k)
repo.set_ref_immediate(ref[1], ref[2], None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment