-
Notifications
You must be signed in to change notification settings - Fork 20
/
ostree-detached-metadata
executable file
·78 lines (70 loc) · 2.65 KB
/
ostree-detached-metadata
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
#
# Print/add/remove detached metadata key
#
# Copyright 2015 Colin Walters <[email protected]>
# Licensed under the new-BSD license (http://www.opensource.org/licenses/bsd-license.php)
import gi
gi.require_version('OSTree', '1.0')
import sys
from gi.repository import GLib, Gio, OSTree
import argparse
parser = argparse.ArgumentParser(prog="ostree-edit-detached")
parser.add_argument("--repo", help="Repo path",
action='store', required=True)
parser.add_argument("--ref", help="Branch or commit",
action='store', required=True)
parser.add_argument("--key", help="Key name",
action='store')
parser.add_argument("--set-string-file", help="Set the key to have the contents of filename",
action='store')
parser.add_argument("--set-uint64", help="Set the key to have an unsigned 64 bit integer",
action='store')
parser.add_argument("--delete", help="Remove the key",
action='store_true')
arg = parser.parse_args()
r = OSTree.Repo.new(Gio.File.new_for_path(arg.repo))
r.open(None)
[_,rev] = r.resolve_rev(arg.ref, False)
if arg.ref != rev:
print "%s => %s" % (arg.ref, rev)
_,metadata = r.read_commit_detached_metadata(rev, None)
n_metadata = 0 if metadata is None else metadata.n_children()
arg = parser.parse_args()
if arg.key is None:
keys = metadata.keys()
if len(keys) == 0:
print "No metadata."
else:
for k in keys:
print k
elif arg.delete:
new_metadata = {}
for i in xrange(n_metadata):
child = metadata.get_child_value(i)
k = child.get_child_value(0).get_string()
v = child.get_child_value(1).get_variant()
if k == arg.key:
continue
new_metadata[k] = v
new_metadatav = GLib.Variant('a{sv}', new_metadata)
r.write_commit_detached_metadata(rev, new_metadatav, None)
print "Deleted key: %s" % (arg.key, )
elif (arg.set_string_file is not None or arg.set_uint64 is not None):
new_metadata = {}
for i in xrange(n_metadata):
child = metadata.get_child_value(i)
k = child.get_child_value(0).get_string()
v = child.get_child_value(1).get_variant()
new_metadata[k] = v
if arg.set_string_file is not None:
with open(arg.set_string_file) as f:
data = f.read()
new_metadata[arg.key] = GLib.Variant('s', data)
else:
new_metadata[arg.key] = GLib.Variant('t', int(arg.set_uint64))
new_metadatav = GLib.Variant('a{sv}', new_metadata)
r.write_commit_detached_metadata(rev, new_metadatav, None)
print "Wrote key: %s" % (arg.key, )
else:
print metadata[arg.key]