From a4a9e8562d67a1dfd46226d7a5a6b3997f5df926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lebleu?= Date: Mon, 5 Oct 2015 14:55:55 +0200 Subject: [PATCH] [enh] Add a chmod function in utils.filesystem --- moulinette/utils/filesystem.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/moulinette/utils/filesystem.py b/moulinette/utils/filesystem.py index c473f27f..ba91e146 100644 --- a/moulinette/utils/filesystem.py +++ b/moulinette/utils/filesystem.py @@ -15,7 +15,7 @@ def mkdir(path, mode=0777, parents=False, uid=None, gid=None, force=False): Keyword arguments: - path -- The directory to create - - mode -- Directory mode to set in octal + - mode -- Numeric path mode to set - parents -- Make parent directories as needed - uid -- Numeric uid or user name - gid -- Numeric gid or group name @@ -65,6 +65,26 @@ def chown(path, uid=None, gid=None, recursive=False): os.chown(os.path.join(root, f), uid, gid) +def chmod(path, mode, fmode=None, recursive=False): + """Change the mode of a path + + Keyword arguments: + - mode -- Numeric path mode to set + - fmode -- Numeric file mode to set in case of a recursive directory + - recursive -- Operate on path recursively + + """ + os.chmod(path, mode) + if recursive and os.path.isdir(path): + if fmode is None: + fmode = mode + for root, dirs, files in os.walk(path): + for d in dirs: + os.chmod(path, mode) + for f in files: + os.chmod(path, fmode) + + def rm(path, recursive=False, force=False): """Remove a file or directory