stages: add org.osbuild.chmod

Add new stage to change the mode bits of files in the tree.
This commit is contained in:
Achilleas Koutsou 2021-06-14 17:41:20 +02:00 committed by Christian Kellner
parent a411ba2270
commit 158d6da415

67
stages/org.osbuild.chmod Executable file
View file

@ -0,0 +1,67 @@
#!/usr/bin/python3
"""
Change file mode bits
Change the file mode bits of one or more files or directories inside the tree.
"""
import os
import subprocess
import sys
import osbuild.api
from osbuild.util.path import in_tree
SCHEMA = r"""
"additionalProperties": false,
"properties": {
"items": {
"type": "object",
"additionalProperties": false,
"patternProperties": {
"^\\/(?!\\.\\.)((?!\\/\\.\\.\\/).)+$": {
"type": "object",
"required": ["mode"],
"properties": {
"mode": {
"type": "string",
"description": "Symbolic or numeric octal mode"
},
"recursive": {
"type": "boolean",
"description": "Change modes recursively",
"default": false
}
}
}
}
}
}
"""
def chmod(path: str, mode: str, recursive: bool):
arguments = [mode]
if recursive:
arguments.append("--recursive")
arguments.append("--")
arguments.append(path)
subprocess.run(["chmod", *arguments], check=True)
def main(tree, options):
for path, cmdargs in options["items"].items():
mode = cmdargs["mode"]
recursive = cmdargs.get("recursive", False)
realpath = os.path.join(tree, path.lstrip("/"))
if not in_tree(realpath, tree, must_exist=True):
raise ValueError(f"path {path} not in tree")
chmod(realpath, mode, recursive)
return 0
if __name__ == "__main__":
args = osbuild.api.arguments()
sys.exit(main(args["tree"], args["options"]))