From e8d80d03e1ad148549d67b7fea623f782dda5bf8 Mon Sep 17 00:00:00 2001 From: agnewee Date: Tue, 19 Dec 2017 23:56:57 +0800 Subject: [PATCH 1/6] add chmod method --- pydu/file.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pydu/file.py b/pydu/file.py index 26a8781..f9eafc2 100644 --- a/pydu/file.py +++ b/pydu/file.py @@ -231,3 +231,15 @@ if not WINDOWS: except: if not ignore_errors: raise OSError('Link {} to {} error'.format(dst, src)) + + + def chmod(src, mode=0o755): + if not os.path.exists(src): + raise OSError('%s is not exists' % src) + + if os.path.isfile(src): + os.chmod(src, mode) + else: + for root, _, files in os.walk(src): + for file_ in files: + os.chmod(os.path.join(os.path.abspath(root), file_), mode) From 040656427ca84b6466690e06404d0cf847c11acc Mon Sep 17 00:00:00 2001 From: agnewee Date: Wed, 20 Dec 2017 00:07:39 +0800 Subject: [PATCH 2/6] add chmod doc --- pydu/file.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/pydu/file.py b/pydu/file.py index f9eafc2..c69ff79 100644 --- a/pydu/file.py +++ b/pydu/file.py @@ -233,13 +233,20 @@ if not WINDOWS: raise OSError('Link {} to {} error'.format(dst, src)) - def chmod(src, mode=0o755): - if not os.path.exists(src): - raise OSError('%s is not exists' % src) + def chmod(path, mode=0o755): + """ + Change the access permissions of a file or directory - if os.path.isfile(src): - os.chmod(src, mode) + If path is not exists, throw OSError + """ + if not os.path.exists(path): + raise OSError('%s is not exists' % path) + + if os.path.isfile(path): + os.chmod(path, mode) else: - for root, _, files in os.walk(src): + for root, _, files in os.walk(path): + root_path = os.path.abspath(root) + os.chmod(root_path, mode) for file_ in files: - os.chmod(os.path.join(os.path.abspath(root), file_), mode) + os.chmod(os.path.join(root_path, file_), mode) From 354b2af6800b04ce666fba15feb0405e86f0f868 Mon Sep 17 00:00:00 2001 From: agnewee Date: Sat, 23 Dec 2017 17:29:53 +0800 Subject: [PATCH 3/6] translate mode param --- pydu/system.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pydu/system.py b/pydu/system.py index c14b90f..b907589 100644 --- a/pydu/system.py +++ b/pydu/system.py @@ -257,12 +257,16 @@ else: raise OSError('Link {} to {} error'.format(dst, src)) - def chmod(path, mode=0o755): + def chmod(path, mode): """ Change the access permissions of a file or directory If path is not exists, throw OSError + >>> chmod('/opt/sometest', 755) + >>> oct(os.stat('/opt/sometest').st_mode)[-3:] + 755 """ + mode = int('0o%d' % mode, 8) if not os.path.exists(path): raise OSError('%s is not exists' % path) From e6e7be1579a9513145c284da83dcec3c097dc426 Mon Sep 17 00:00:00 2001 From: agnewee Date: Sat, 23 Dec 2017 17:30:04 +0800 Subject: [PATCH 4/6] add test case --- tests/test_system.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/test_system.py b/tests/test_system.py index 043f4b3..c934851 100644 --- a/tests/test_system.py +++ b/tests/test_system.py @@ -1,12 +1,15 @@ import os import stat import time +import tempfile + import pytest + from pydu.platform import WINDOWS from pydu.system import makedirs, remove, removes, open_file, copy, touch, which if not WINDOWS: - from pydu.system import link, symlink + from pydu.system import link, symlink, chmod class TestMakeDirs: @@ -350,3 +353,23 @@ def test_chcp(): assert str(cp) == '' finally: windll.kernel32.SetConsoleOutputCP(origin_code) + + +@pytest.mark.skipif(WINDOWS, reason='Not support on windows') +class TestChmod: + def test_chmod_file(self): + _, t_file = tempfile.mkstemp() + chmod(t_file, 755) + assert oct(os.stat(t_file).st_mode)[-3:] == '755' + + def test_chmod_dir(self): + t_dir = tempfile.mkdtemp() + for _ in range(5): + tempfile.mkstemp(dir=t_dir) + chmod(t_dir, 755) + for root, _, files in os.walk(t_dir): + root_path = os.path.abspath(root) + assert oct(os.stat(root_path).st_mode)[-3:0] == '755' + for file_ in files: + assert oct(os.stat(os.path.join(root_path, file_)).st_mode)[-3:0] == '755' + From 777fb61847f22be52995f113cd8a7fa6a612ef44 Mon Sep 17 00:00:00 2001 From: agnewee Date: Sat, 23 Dec 2017 17:37:29 +0800 Subject: [PATCH 5/6] correction test case --- tests/test_system.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_system.py b/tests/test_system.py index c934851..b20fc9e 100644 --- a/tests/test_system.py +++ b/tests/test_system.py @@ -368,8 +368,7 @@ class TestChmod: tempfile.mkstemp(dir=t_dir) chmod(t_dir, 755) for root, _, files in os.walk(t_dir): - root_path = os.path.abspath(root) - assert oct(os.stat(root_path).st_mode)[-3:0] == '755' + assert oct(os.stat(root).st_mode)[-3:] == '755' for file_ in files: - assert oct(os.stat(os.path.join(root_path, file_)).st_mode)[-3:0] == '755' + assert oct(os.stat(os.path.join(root, file_)).st_mode)[-3:] == '755' From e3c52d5c8f4bf0dcf28c5dd9fe7d8e7ae6842896 Mon Sep 17 00:00:00 2001 From: agnewee Date: Sat, 23 Dec 2017 18:11:35 +0800 Subject: [PATCH 6/6] modify chmod method --- pydu/system.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/pydu/system.py b/pydu/system.py index b907589..71ff950 100644 --- a/pydu/system.py +++ b/pydu/system.py @@ -261,20 +261,14 @@ else: """ Change the access permissions of a file or directory - If path is not exists, throw OSError - >>> chmod('/opt/sometest', 755) + >>> chmod('/opt/sometest', 0o755) >>> oct(os.stat('/opt/sometest').st_mode)[-3:] 755 """ - mode = int('0o%d' % mode, 8) - if not os.path.exists(path): - raise OSError('%s is not exists' % path) - - if os.path.isfile(path): - os.chmod(path, mode) - else: + if os.path.isdir(path): for root, _, files in os.walk(path): - root_path = os.path.abspath(root) - os.chmod(root_path, mode) + os.chmod(root, mode) for file_ in files: - os.chmod(os.path.join(root_path, file_), mode) + os.chmod(os.path.join(root, file_), mode) + else: + os.chmod(path, mode)