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.
This commit is contained in:
Christian Kellner 2021-06-09 19:15:59 +00:00
parent 368d0a5c18
commit dc201d45fd

View file

@ -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