forked from ostreedev/ostree-releng-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-string-key
executable file
·66 lines (55 loc) · 1.98 KB
/
add-string-key
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
#!/usr/bin/env python
import sys
from pprint import pprint as pp
import gi
gi.require_version('OSTree', '1.0')
from gi.repository import GLib, Gio, OSTree
import argparse
parser = argparse.ArgumentParser(prog="ostree-add-key-string")
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', required=True)
parser.add_argument("--string", help="String value of the key",
action='store', required=True)
arg = parser.parse_args()
(repo, ref, key, string) = (arg.repo, arg.ref, arg.key, arg.string)
r = OSTree.Repo.new(Gio.File.new_for_path(repo))
r.open(None)
[_,rev] = r.resolve_rev(ref, False)
_,metadata = r.read_commit_detached_metadata(rev, None)
def pp_metadata(metadata):
print "Metadata:"
n = metadata.n_children()
for i in xrange(n):
child = metadata.get_child_value(i)
print "%s" % (child.print_(True), )
pp_metadata(metadata)
new_metadata = None
key_found = False
if metadata:
n = metadata.n_children()
meta = {}
for i in xrange(n):
child = metadata.get_child_value(i)
k = child.get_child_value(0).get_string()
v = child.get_child_value(1).get_variant()
if k == key:
key_found = True
break
meta[k] = v
print "%s = %r" % (k, v)
if key_found:
print "key (%s) found for ostree: %s, ref: %s, exiting" % (key, repo, ref)
sys.exit(1)
meta[key] = GLib.Variant('s', string)
new_metadata = GLib.Variant('a{sv}', meta)
else:
new_metadata = GLib.Variant('a{sv}', {key: GLib.Variant('s', string)})
print "Adding key to ostree: %s, ref: %s" % (repo, ref)
r.write_commit_detached_metadata(rev, new_metadata, None)
[_,rev] = r.resolve_rev(ref, False)
_,metadata = r.read_commit_detached_metadata(rev, None)
pp_metadata(metadata)