Created
February 28, 2017 18:12
-
-
Save dbnicholson/2f7fec7885a4074c37f13113add70b59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- mode: Python; coding: utf-8 -*- | |
from __future__ import print_function | |
import argparse | |
from gi.repository import GLib, Gio, OSTree | |
import os | |
import sys | |
argparser = argparse.ArgumentParser(description='Copy refs from SRC_REPO') | |
argparser.add_argument('--repo', help='Path to OSTree repository') | |
argparser.add_argument('--remote', help='Add remote to refspec') | |
argparser.add_argument('--depth', type=int, default=0, | |
help='Traverse DEPTH parents (default: 0)') | |
argparser.add_argument('src', metavar='SRC_REPO', | |
help='Path to source OSTree repository') | |
argparser.add_argument('refs', metavar='REFS', nargs='*', | |
help='Refs to pull from SRC_REPO') | |
args = argparser.parse_args() | |
if args.repo is None: | |
dest_repo = OSTree.Repo.new_default() | |
else: | |
dest_repo = OSTree.Repo.new(Gio.File.new_for_path(args.repo)) | |
try: | |
dest_repo.open(None) | |
except: | |
dest_repo.create(OSTree.RepoMode.ARCHIVE_Z2, None) | |
#repo.open(None) | |
src_repo = OSTree.Repo.new(Gio.File.new_for_path(args.src)) | |
src_uri = 'file://' + os.path.abspath(args.src) | |
if len(args.refs) == 0: | |
# Pull all refs from the source repo | |
src_repo.open(None) | |
ret, refs_hash = src_repo.list_refs(None, None) | |
args.refs = refs_hash.keys() | |
for ref in args.refs: | |
ret, rev = src_repo.resolve_rev(ref, True) | |
if not rev: | |
print('Refspec', ref, 'not found in', args.src) | |
exit(1) | |
print('Pulling', ref, 'commit', rev) | |
# Build pull options variant | |
pull_args = {'flags': GLib.Variant('i', OSTree.RepoPullFlags.NONE), | |
'refs': GLib.Variant('as', args.refs), | |
'depth': GLib.Variant('i', args.depth)} | |
if args.remote is not None: | |
pull_args['override-remote-name'] = GLib.Variant('s', args.remote) | |
pull_opts = GLib.Variant('a{sv}', pull_args) | |
# Ripped from ostree_repo_pull_default_console_progress_changed, but that | |
# seems to cause a segfault when using GSConsole | |
def progress_cb(progress, data): | |
status = progress.get_status() | |
outstanding_fetches = progress.get_uint('outstanding-fetches') | |
outstanding_writes = progress.get_uint('outstanding-writes') | |
if status: | |
print('\r' + status) | |
elif outstanding_fetches: | |
fetched = progress.get_uint('fetched') | |
requested = progress.get_uint('requested') | |
metadata_fetched = progress.get_uint('metadata-fetched') | |
outstanding_metadata_fetches = \ | |
progress.get_uint('outstanding-metadata-fetches') | |
if outstanding_metadata_fetches: | |
total_metadata_fetches = metadata_fetched \ | |
+ outstanding_metadata_fetches | |
tmpl = '\rReceiving metadata objects: {}/{}' | |
print(tmpl.format(metadata_fetched, total_metadata_fetches), | |
end='') | |
else: | |
percent = float(fetched) / requested | |
tmpl = '\rReceiving objects: {:%} ({}/{})' | |
print(tmpl.format(percent, fetched, requested), end='') | |
elif outstanding_writes: | |
print('\rWriting objects: {}'.format(outstanding_writes), end='') | |
else: | |
scanned_metadata = progress.get_uint('scanned-metadata') | |
print('\rScanning metadata: {}'.format(scanned_metadata), end='') | |
print('Pulling refs', ' '.join(args.refs), 'from', args.src, 'to', args.repo) | |
progress = OSTree.AsyncProgress.new() | |
progress.connect('changed', progress_cb, None) | |
# console = GSystem.Console.get() | |
# if console: | |
# #print('got the console') | |
# console.begin_status_line('Starting pull', None) | |
# #changed = OSTree.Repo.pull_default_console_progress_changed | |
# #progress = OSTree.AsyncProgress.new_and_connect(changed, console) | |
# progress = OSTree.AsyncProgress.new() | |
# progress.connect('changed', | |
# OSTree.Repo.pull_default_console_progress_changed, | |
# console) | |
dest_repo.pull_with_options(src_uri, pull_opts, progress, None) | |
#dest_repo.pull(src_uri, args.refs, OSTree.RepoPullFlags.NONE, None, None) | |
progress.finish() | |
#console.end_status_line(None) | |
for ref in args.refs: | |
ret, rev = dest_repo.resolve_rev(ref, False) | |
compat = 'compat/' + ref | |
print('Setting', compat, 'to', rev) | |
dest_repo.set_ref_immediate(None, compat, rev, None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment