Skip to content

Instantly share code, notes, and snippets.

@cosimoc
Created October 7, 2017 15:41
Show Gist options
  • Save cosimoc/216623d83698e2f4d747fcd9879785bd to your computer and use it in GitHub Desktop.
Save cosimoc/216623d83698e2f4d747fcd9879785bd to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
# ostree-delete-refs - Delete refs from ostree repositories
# Copyright (C) 2016 Dan Nicholson <nicholson@endlessm.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
from argparse import ArgumentParser
import eosostree
import fnmatch
import gi
gi.require_version('OSTree', '1.0')
from gi.repository import Gio, OSTree
# On public server by default
REPO_BASE = eosostree.CONFIG['public-repo-base']
parser = ArgumentParser(description='Delete commit references in ostree repositories')
parser.add_argument('repo', metavar='REPO',
help='name of repository to use')
parser.add_argument('stage', metavar='STAGE',
help='repository stage to use')
parser.add_argument('refs', metavar='REF', nargs='+',
help='refs to delete, can use globbing')
parser.add_argument('-n', '--dry-run', action='store_true',
help='show commands that would be run')
parser.add_argument('--base', default=REPO_BASE,
help='base path for repos (default: %s)' % REPO_BASE)
parser.add_argument('--release', action='store_true',
help='remove corresponding release refs')
args = parser.parse_args()
path = eosostree.staged_repo_path(args.repo, base=args.base,
stage=args.stage)
repo_file = Gio.File.new_for_path(path)
repo = eosostree.EosOSTreeRepo(path=repo_file)
repo.open(None)
# Find all the refs that need to be deleted
_, all_refs_info = repo.list_refs(None, None)
all_refs = sorted(all_refs_info.keys())
delete_refs = set()
for pattern in args.refs:
delete_refs.update(fnmatch.filter(all_refs, pattern))
# Delete the associated release refs if wanted
if args.release:
for refspec in delete_refs.copy():
_, remote, ref = OSTree.parse_refspec(refspec)
info = eosostree.ref_info(ref)
if info['release']:
continue
if not info['tag_prefix']:
# Not a releasable ref
continue
# Construct a pattern that finds all the releases for this
# series
rel_pattern = ''
if remote is not None:
rel_pattern += remote + ':'
rel_pattern += '%s/%s.*' %(info['tag_prefix'], info['major'])
delete_refs.update(fnmatch.filter(all_refs, rel_pattern))
if len(delete_refs) == 0:
print('No refs found to delete')
exit(0)
for refspec in sorted(delete_refs):
print('Deleting ref', refspec)
_, remote, ref = OSTree.parse_refspec(refspec)
if not args.dry_run:
repo.set_ref_immediate(remote, ref, None, None)
eosostree.update_repo(repo, dry_run=args.dry_run)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment