parsing: add parse_location_into_parts

New fucntion returns tuple of 'root' and relative 'file path', which could be
useful in contexts, where knowing 'root' is required, for example setting
selinux labels.
This commit is contained in:
Nikita Dubrovskii 2024-10-18 12:28:32 +02:00 committed by Michael Vogt
parent a699d05094
commit 077244e3b9
2 changed files with 34 additions and 6 deletions

View file

@ -2,7 +2,7 @@
import os
import re
from typing import Dict, Union
from typing import Dict, Tuple, Union
from urllib.parse import ParseResult, urlparse
@ -72,9 +72,9 @@ def parse_input(url: ParseResult, args: Dict) -> os.PathLike:
return root
def parse_location(location: str, args: Dict) -> str:
def parse_location_into_parts(location: str, args: Dict) -> Tuple[str, str]:
"""
Parses the location URL to derive the corresponding file path.
Parses the location URL to derive the corresponding root and url path.
Parameters:
- location (str): The location URL to be parsed.
@ -97,11 +97,24 @@ def parse_location(location: str, args: Dict) -> str:
if not url.path.startswith("/"):
raise ValueError(f"url.path from location must start with '/', got: {url.path}")
path = os.path.relpath(url.path, "/")
return root, url.path
def parse_location(location: str, args: Dict) -> str:
"""
Parses the location URL to derive the corresponding file path.
Parameters:
- location (str): The location URL to be parsed.
- args (Dict): A dictionary containing arguments including mounts and
path information as passed by osbuild.api.arguments()
"""
root, urlpath = parse_location_into_parts(location, args)
path = os.path.relpath(urlpath, "/")
path = os.path.join(root, path)
path = os.path.normpath(path)
if url.path.endswith("/"):
if urlpath.endswith("/"):
path = os.path.join(path, ".")
return path

View file

@ -48,18 +48,31 @@ def test_parse_location_mounts():
"root": {
"path": "/run/osbuild/mounts/.",
},
"boot": {
"path": "/run/osbuild/mounts/boot"
}
},
}
location = "mount://root/"
root, path = parsing.parse_location_into_parts(location, args)
assert [root, path] == ["/run/osbuild/mounts/.", "/"]
path = parsing.parse_location(location, args)
assert path == "/run/osbuild/mounts/."
location = "mount://boot/efi/EFI/Linux"
root, path = parsing.parse_location_into_parts(location, args)
assert [root, path] == ["/run/osbuild/mounts/boot", "/efi/EFI/Linux"]
path = parsing.parse_location(location, args)
assert path == "/run/osbuild/mounts/boot/efi/EFI/Linux"
def test_parse_location_tree():
args = {
"tree": "/run/osbuild/tree",
}
location = "tree:///disk.img"
root, path = parsing.parse_location_into_parts(location, args)
assert [root, path] == ["/run/osbuild/tree", "/disk.img"]
path = parsing.parse_location(location, args)
assert path == "/run/osbuild/tree/disk.img"
@ -73,5 +86,7 @@ def test_parse_location_inputs():
},
}
location = "input://tree/"
root, path = parsing.parse_location_into_parts(location, args)
assert [root, path] == ["/run/osbuild/inputs/tree", "/"]
path = parsing.parse_location(location, args)
assert path == "/run/osbuild/inputs/tree/."