stages/mkdir: add exist_ok option to not fail if directory exists
Add a new optional stage option to not fail if the specified directory already exists. This will make it easier to support creation of custom repositories via customizations in osbuild-composer. The reason is that if a specified directory exists in an image, because it was created by an RPM, then creating it would fail. However, the user may have specified different mode for the directory, than it already has. Since there is no way to know for sure if the directory already exists on the image, without building the image itself, it is desired to handle this case gracefully as valid in specific use cases. The default behavior stays the same - specifying an existing directory path will lead to an error. Signed-off-by: Tomáš Hozza <thozza@redhat.com>
This commit is contained in:
parent
a988aacf99
commit
b8e1450a35
6 changed files with 39 additions and 5 deletions
|
|
@ -36,8 +36,12 @@ SCHEMA_2 = r"""
|
|||
"type": "boolean",
|
||||
"description": "Create intermediate directories",
|
||||
"default": false
|
||||
},
|
||||
"exist_ok": {
|
||||
"type": "boolean",
|
||||
"description": "Do not fail if the directory already exists",
|
||||
"default": false
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -50,17 +54,21 @@ def main(tree, options):
|
|||
for item in options["paths"]:
|
||||
path = item["path"]
|
||||
mode = item.get("mode", 0o777)
|
||||
|
||||
parents = item.get("parents", False)
|
||||
exist_ok = item.get("exist_ok", False)
|
||||
|
||||
target = os.path.join(tree, path.lstrip("/"))
|
||||
if not in_tree(target, tree):
|
||||
raise ValueError(f"path {path} not in tree")
|
||||
|
||||
if parents:
|
||||
os.makedirs(target, mode=mode)
|
||||
os.makedirs(target, mode=mode, exist_ok=exist_ok)
|
||||
else:
|
||||
os.mkdir(target, mode)
|
||||
try:
|
||||
os.mkdir(target, mode)
|
||||
except FileExistsError:
|
||||
if not exist_ok:
|
||||
raise
|
||||
|
||||
# Documentation for os.mkdir() says that the mode is
|
||||
# ignored on some systems. Also umask value may affect
|
||||
|
|
|
|||
|
|
@ -362,6 +362,10 @@
|
|||
"paths": [
|
||||
{
|
||||
"path": "a"
|
||||
},
|
||||
{
|
||||
"path": "c/d",
|
||||
"parents": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,10 @@
|
|||
"paths": [
|
||||
{
|
||||
"path": "a"
|
||||
},
|
||||
{
|
||||
"path": "c/d",
|
||||
"parents": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -363,6 +363,10 @@
|
|||
{
|
||||
"path": "a"
|
||||
},
|
||||
{
|
||||
"path": "c/d",
|
||||
"parents": true
|
||||
},
|
||||
{
|
||||
"path": "a/b",
|
||||
"mode": 448
|
||||
|
|
@ -370,6 +374,11 @@
|
|||
{
|
||||
"path": "b/c/d",
|
||||
"parents": true
|
||||
},
|
||||
{
|
||||
"path": "c",
|
||||
"mode": 448,
|
||||
"exist_ok": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,10 @@
|
|||
{
|
||||
"path": "a"
|
||||
},
|
||||
{
|
||||
"path": "c/d",
|
||||
"parents": true
|
||||
},
|
||||
{
|
||||
"path": "a/b",
|
||||
"mode": 448
|
||||
|
|
@ -26,6 +30,11 @@
|
|||
{
|
||||
"path": "b/c/d",
|
||||
"parents": true
|
||||
},
|
||||
{
|
||||
"path": "c",
|
||||
"mode": 448,
|
||||
"exist_ok": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,5 +6,5 @@
|
|||
"/b/c/d"
|
||||
],
|
||||
"deleted_files": [],
|
||||
"differences": {}
|
||||
"differences": {"/c": {"mode": [16877, 16832]}}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue