util/path: add path checker helper function
Checks if one path is a child of a second one. Useful for checking if paths defined in a manifest exist inside the tree. Optionally checks if the target path exists.
This commit is contained in:
parent
704d5d305a
commit
901de63fb9
1 changed files with 14 additions and 0 deletions
14
osbuild/util/path.py
Normal file
14
osbuild/util/path.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
"""Path handling utility functions"""
|
||||
import os.path
|
||||
|
||||
from .types import PathLike
|
||||
|
||||
|
||||
def in_tree(path: PathLike, tree: PathLike, must_exist=False) -> bool:
|
||||
"""Return whether the canonical location of 'path' is under 'tree'.
|
||||
If 'must_exist' is True, the file must also exist for the check to succeed.
|
||||
"""
|
||||
path = os.path.abspath(path)
|
||||
if path.startswith(tree):
|
||||
return not must_exist or os.path.exists(path)
|
||||
return False
|
||||
Loading…
Add table
Add a link
Reference in a new issue