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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
#!/usr/bin/env python3
#
# Copyright (C) 2017 Codethink Limited
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Jürg Billeter <juerg.billeter@codethink.co.uk>
# Andrew Leeming <andrew.leeming@codethink.co.uk>
# Tristan Van Berkom <tristan.vanberkom@codethink.co.uk>
#
# Code based on Jürg's artifact cache and Andrew's ostree plugin
#
import os
import subprocess
from . import _site
from . import utils
from ._exceptions import BstError
import gi
gi.require_version('OSTree', '1.0')
from gi.repository import GLib, Gio, OSTree # nopep8
from gi.repository.GLib import Variant, VariantDict # nopep8
# For users of this file, they must expect (except) it.
class OSTreeError(BstError):
pass
# ensure()
#
# Args:
# path (str): The file path to where the desired repo should be
# compress (bool): use compression or not when creating
#
# Returns: an OSTree.Repo
def ensure(path, compress):
# create also succeeds on existing repository
repo = OSTree.Repo.new(Gio.File.new_for_path(path))
mode = OSTree.RepoMode.ARCHIVE_Z2 if compress \
else OSTree.RepoMode.BARE_USER
repo.create(mode)
return repo
# checkout()
#
# Checkout the content at 'commit' from 'repo' in
# the specified 'path'
#
# Args:
# repo (OSTree.Repo): The repo
# path (str): The checkout path
# commit (str): The commit checksum to checkout
# user (boot): Whether to checkout in user mode
#
def checkout(repo, path, commit, user=False):
# Check out a full copy of an OSTree at a given ref to some directory.
#
# Note: OSTree does not like updating directories inline/sync, therefore
# make sure you checkout to a clean directory or add additional code to support
# union mode or (if it exists) file replacement/update.
#
# Returns True on success
#
# cli exmaple:
# ostree --repo=repo checkout --user-mode runtime/org.freedesktop.Sdk/x86_64/1.4 foo
os.makedirs(os.path.dirname(path), exist_ok=True)
options = OSTree.RepoCheckoutAtOptions()
# For repos which contain root owned files, we need
# to checkout with OSTree.RepoCheckoutMode.USER
#
# This will reassign uid/gid and also munge the
# permission bits a bit.
if user:
options.mode = OSTree.RepoCheckoutMode.USER
# Using AT_FDCWD value from fcntl.h
#
# This will be ignored if the passed path is an absolute path,
# if path is a relative path then it will be appended to the
# current working directory.
AT_FDCWD = -100
try:
repo.checkout_at(options, AT_FDCWD, path, commit)
except GLib.GError as e:
raise OSTreeError("Failed to checkout commit '{}': {}".format(commit, e.message)) from e
# commit():
#
# Commit built artifact to cache.
#
# Files are all recorded with uid/gid 0
#
# Args:
# repo (OSTree.Repo): The repo
# dir (str): The source directory to commit to the repo
# ref (str): A symbolic reference (tag) for the commit
# branch (str): Optional branch for the commit
#
def commit(repo, dir, ref, branch=None):
def commit_filter(repo, path, file_info):
# For now, just set everything in the repo as uid/gid 0
#
# In the future we'll want to extract virtualized file
# attributes from a fuse layer and use that.
#
file_info.set_attribute_uint32('unix::uid', 0)
file_info.set_attribute_uint32('unix::gid', 0)
return OSTree.RepoCommitFilterResult.ALLOW
commit_modifier = OSTree.RepoCommitModifier.new(
OSTree.RepoCommitModifierFlags.NONE, commit_filter)
repo.prepare_transaction()
try:
# add tree to repository
mtree = OSTree.MutableTree.new()
repo.write_directory_to_mtree(Gio.File.new_for_path(dir),
mtree, commit_modifier)
_, root = repo.write_mtree(mtree)
# create root commit object, no parent, no branch
_, rev = repo.write_commit(None, ref, None, None, root)
# create tag
repo.transaction_set_ref(None, ref, rev)
# optionally create/update branch (without parent commit for now)
if branch:
repo.transaction_set_ref(None, branch, rev)
# complete repo transaction
repo.commit_transaction(None)
except GLib.GError as e:
# Reraise any error as a buildstream error
repo.abort_transaction()
raise OSTreeError(e.message) from e
# set_ref():
#
# Set symbolic reference to specified revision.
#
# Args:
# repo (OSTree.Repo): The repo
# ref (str): A symbolic reference (tag) for the commit
# rev (str): Commit checksum
#
def set_ref(repo, ref, rev):
repo.prepare_transaction()
try:
repo.transaction_set_ref(None, ref, rev)
# complete repo transaction
repo.commit_transaction(None)
except:
repo.abort_transaction()
raise
# exists():
#
# Checks wether a given commit or symbolic ref exists and
# is locally cached in the specified repo.
#
# Args:
# repo (OSTree.Repo): The repo
# ref (str): A commit checksum or symbolic ref
#
# Returns:
# (bool): Whether 'ref' is valid in 'repo'
#
def exists(repo, ref):
# Get the commit checksum, this will:
#
# o Return a commit checksum if ref is a symbolic branch
# o Return the same commit checksum if ref is a valid commit checksum
# o Return None if the ostree repo doesnt know this ref.
#
ref = checksum(repo, ref)
if ref is None:
return False
# If we do have a ref which the ostree knows about, this does
# not mean we necessarily have the object locally (we may just
# have some metadata about it, this can happen).
#
# Use has_object() only with a resolved valid commit checksum
# to check if we actually have the object locally.
_, has_object = repo.has_object(OSTree.ObjectType.COMMIT, ref, None)
return has_object
# checksum():
#
# Returns the commit checksum for a given symbolic ref,
# which might be a branch or tag. If it is a branch,
# the latest commit checksum for the given branch is returned.
#
# Args:
# repo (OSTree.Repo): The repo
# ref (str): The symbolic ref
#
# Returns:
# (str): The commit checksum, or None if ref does not exist.
#
def checksum(repo, ref):
_, checksum = repo.resolve_rev(ref, True)
return checksum
# fetch()
#
# Fetch new objects from a remote, if configured
#
# Args:
# repo (OSTree.Repo): The repo
# remote (str): An optional remote name, defaults to 'origin'
# ref (str): An optional ref to fetch, will reduce the amount of objects fetched
# progress (callable): An optional progress callback
#
# Note that a commit checksum or a branch reference are both
# valid options for the 'ref' parameter. Using the ref parameter
# can save a lot of bandwidth but mirroring the full repo is
# still possible.
#
def fetch(repo, remote="origin", ref=None, progress=None):
# Fetch metadata of the repo from a remote
#
# cli example:
# ostree --repo=repo pull --mirror freedesktop:runtime/org.freedesktop.Sdk/x86_64/1.4
def progress_callback(info):
status = async_progress.get_status()
outstanding_fetches = async_progress.get_uint('outstanding-fetches')
bytes_transferred = async_progress.get_uint64('bytes-transferred')
fetched = async_progress.get_uint('fetched')
requested = async_progress.get_uint('requested')
if status:
progress(0.0, status)
elif outstanding_fetches > 0:
formatted_bytes = GLib.format_size_full(bytes_transferred, 0)
if requested == 0:
percent = 0.0
else:
percent = (fetched * 1.0 / requested) * 100
progress(percent,
"Receiving objects: {:d}% ({:d}/{:d}) {}".format(percent, fetched, requested, formatted_bytes))
else:
progress(100.0, "Writing Objects")
async_progress = None
if progress is not None:
async_progress = OSTree.AsyncProgress.new()
async_progress.connect('changed', progress_callback)
# FIXME: This hangs the process and ignores keyboard interrupt,
# fix this using the Gio.Cancellable
refs = None
if ref is not None:
refs = [ref]
try:
repo.pull(remote,
refs,
OSTree.RepoPullFlags.MIRROR,
async_progress,
None) # Gio.Cancellable
except GLib.GError as e:
if ref is not None:
raise OSTreeError("Failed to fetch ref '{}' from '{}': {}".format(ref, remote, e.message)) from e
else:
raise OSTreeError("Failed to fetch from '{}': {}".format(remote, e.message)) from e
# configure_remote():
#
# Ensures a remote is setup to a given url.
#
# Args:
# repo (OSTree.Repo): The repo
# remote (str): The name of the remote
# url (str): The url of the remote ostree repo
# key_url (str): The optional url of a GPG key (should be a local file)
#
def configure_remote(repo, remote, url, key_url=None):
# Add a remote OSTree repo. If no key is given, we disable gpg checking.
#
# cli exmaple:
# wget https://sdk.gnome.org/keys/gnome-sdk.gpg
# ostree --repo=repo --gpg-import=gnome-sdk.gpg remote add freedesktop https://sdk.gnome.org/repo
options = None # or GLib.Variant of type a{sv}
if key_url is None:
vd = VariantDict.new()
vd.insert_value('gpg-verify', Variant.new_boolean(False))
options = vd.end()
repo.remote_change(None, # Optional OSTree.Sysroot
OSTree.RepoRemoteChange.ADD_IF_NOT_EXISTS,
remote, # Remote name
url, # Remote url
options, # Remote options
None) # Optional Gio.Cancellable
# Remote needs to exist before adding key
if key_url is not None:
try:
gfile = Gio.File.new_for_uri(key_url)
stream = gfile.read()
repo.remote_gpg_import(remote, stream, None, 0, None)
except GLib.GError as e:
raise OSTreeError("Failed to add gpg key from url '{}': {}".format(key_url, e.message)) from e
# list_remote_refs():
#
# Fetch list of refs from a remote.
#
# Args:
# repo (OSTree.Repo): The repo
# remote (str): An optional remote name, defaults to 'origin'
#
# Returns:
# (dict): A dict of refs to checksums.
#
def list_remote_refs(repo, remote="origin"):
try:
_, refs = repo.remote_list_refs(remote)
return refs
except GLib.GError as e:
raise OSTreeError("Failed to fetch remote refs from '{}': {}".format(remote, e.message)) from e
|