From dc201d45fdf982d5a0607d21717784e570483067 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Wed, 9 Jun 2021 19:15:59 +0000 Subject: [PATCH] utils/ostree: add rev_parse helper function Add a simple helper function that wraps `ostree rev-parse` to make it easy to resolve an OSTree reference given a repository. --- osbuild/util/ostree.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/osbuild/util/ostree.py b/osbuild/util/ostree.py index 775c2743..875d9b90 100644 --- a/osbuild/util/ostree.py +++ b/osbuild/util/ostree.py @@ -1,11 +1,14 @@ import contextlib import json import os +import subprocess import tempfile import typing from typing import List +from .types import PathLike + class Param: """rpm-ostree Treefile parameter""" @@ -104,3 +107,21 @@ class Treefile: finally: if name: os.unlink(name) + + +def rev_parse(repo: PathLike, ref: str) -> str: + """Resolve an OSTree reference `ref` in the repository at `repo`""" + + repo = os.fspath(repo) + + r = subprocess.run(["ostree", "rev-parse", ref, f"--repo={repo}"], + encoding="utf-8", + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + check=False) + + msg = r.stdout.strip() + if r.returncode != 0: + raise RuntimeError(msg) + + return msg