forked from osbuild/rhel-for-edge-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.py
executable file
·82 lines (63 loc) · 2.13 KB
/
import.py
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
#!/usr/bin/python3
import argparse
import functools
import json
import os
import subprocess
import sys
import tempfile
def run_ostree(*args, _input=None, _check=True, _stderr=sys.stderr, **kwargs):
args = list(args) + [f'--{k}={v}' for k, v in kwargs.items()]
if _stderr is None:
_stderr = subprocess.DEVNULL
res = subprocess.run(["ostree"] + args,
encoding="utf-8",
stdout=subprocess.PIPE,
stderr=_stderr,
input=_input,
check=_check)
return res
def main(tmp):
parser = argparse.ArgumentParser(description="Import an ostree commit")
parser.add_argument("commit", metavar="COMMIT", type=os.path.abspath,
help="commit, in tar form")
parser.add_argument("--repo", metavar="DIRECTORY", type=os.path.abspath,
default="repo",
help="Name of the target repository")
args = parser.parse_args(sys.argv[1:])
repo = args.repo
print(f"repo: {repo}")
ostree = functools.partial(run_ostree, repo=repo)
if not os.path.exists(repo):
ostree("init", mode="archive-z2")
# Extract the commit
commit = os.path.join(tmpdir, "commit")
os.makedirs(commit)
command = [
"tar",
"-x",
"--auto-compress",
"-f", args.commit,
"-C", commit
]
subprocess.run(command,
stdout=sys.stderr,
check=True)
with open(os.path.join(commit, "compose.json"), "r") as fp:
info = json.load(fp)
commit_id = info["ostree-commit"]
ref = info["ref"]
parent = None
print(f"commit: {commit_id}")
print(f"ref: {ref}")
ostree("pull-local",
os.path.join(commit, "repo"),
ref)
ostree("summary", "--update")
res = ostree("rev-parse", f"{ref}^", _check=False, _stderr=None)
if res.returncode == 0:
parent = res.stdout.strip()
print(f"parent: {parent}")
if __name__ == "__main__":
with tempfile.TemporaryDirectory(dir="/var/tmp") as tmpdir:
main(tmpdir)