From 2e14d32bf279be1c3fa360f95aaf07cdeb1832f8 Mon Sep 17 00:00:00 2001 From: Prodesire Date: Sat, 2 Dec 2017 10:32:11 +0800 Subject: [PATCH 1/4] Add platform to help judge which platform we are working on --- pydu/platform.py | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 pydu/platform.py diff --git a/pydu/platform.py b/pydu/platform.py new file mode 100644 index 0000000..4236cb2 --- /dev/null +++ b/pydu/platform.py @@ -0,0 +1,4 @@ +import os + + +WINDOWS = os.name == 'nt' From 95d366771d9f8497747762516590c39f60f614fd Mon Sep 17 00:00:00 2001 From: Prodesire Date: Sat, 2 Dec 2017 10:33:55 +0800 Subject: [PATCH 2/4] Fix #2, file.link only available on No Windows platform --- pydu/file.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/pydu/file.py b/pydu/file.py index 4c8bf9e..2c72e5b 100644 --- a/pydu/file.py +++ b/pydu/file.py @@ -1,6 +1,7 @@ import os import sys import shutil +from pydu.platform import WINDOWS # todo tests and docs @@ -80,19 +81,6 @@ def open_file(path, mode='wb+', buffer_size=-1, ignore_errors=False): return f -def link(src, dst, overwrite=False, ignore_errors=False): - try: - if os.path.exists(dst): - if overwrite: - remove(dst) - else: - return - os.link(src, dst) - except Exception: - if not ignore_errors: - raise OSError('Link {} to {} error'.format(dst, src)) - - def copy(src, dst, ignore_errors=False, follow_symlinks=True): try: if os.path.isdir(src): @@ -102,3 +90,17 @@ def copy(src, dst, ignore_errors=False, follow_symlinks=True): except Exception: if not ignore_errors: raise OSError('Copy {} to {} error'.format(src, dst)) + + +if not WINDOWS: + def link(src, dst, overwrite=False, ignore_errors=False): + try: + if os.path.exists(dst): + if overwrite: + remove(dst) + else: + return + os.link(src, dst) + except Exception: + if not ignore_errors: + raise OSError('Link {} to {} error'.format(dst, src)) From afe7fa6bdc7cb9175d80081635e067743b7b3705 Mon Sep 17 00:00:00 2001 From: Prodesire Date: Sat, 2 Dec 2017 10:37:59 +0800 Subject: [PATCH 3/4] rename Testmakedirs to TestMakeDirs --- tests/test_file.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_file.py b/tests/test_file.py index a4260a7..714ad66 100644 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -1,9 +1,9 @@ import os -from pydu.file import makedirs import pytest +from pydu.file import makedirs -class Testmakedirs(): +class TestMakeDirs: def test_makedirs(self, tmpdir): path = str(tmpdir.join('test')) makedirs(path) @@ -33,3 +33,5 @@ class Testmakedirs(): path = str(tmpdir.join('test/test')) makedirs(path) assert os.path.exists(path) + + From d60bc2e3b8bbc202b979427a343a34051afd9b11 Mon Sep 17 00:00:00 2001 From: Prodesire Date: Sat, 2 Dec 2017 11:09:05 +0800 Subject: [PATCH 4/4] add test for file.link --- pydu/file.py | 5 +++++ tests/test_file.py | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/pydu/file.py b/pydu/file.py index 2c72e5b..d648cae 100644 --- a/pydu/file.py +++ b/pydu/file.py @@ -92,6 +92,11 @@ def copy(src, dst, ignore_errors=False, follow_symlinks=True): raise OSError('Copy {} to {} error'.format(src, dst)) +def touch(path): + with open(path, 'w'): + pass + + if not WINDOWS: def link(src, dst, overwrite=False, ignore_errors=False): try: diff --git a/tests/test_file.py b/tests/test_file.py index 714ad66..4afc240 100644 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -1,6 +1,10 @@ import os import pytest -from pydu.file import makedirs +from pydu.platform import WINDOWS +from pydu.file import makedirs, remove, removes, open_file, copy, touch + +if not WINDOWS: + from pydu.file import link class TestMakeDirs: @@ -35,3 +39,17 @@ class TestMakeDirs: assert os.path.exists(path) +def test_touch(tmpdir): + path = str(tmpdir.join('test')) + touch(path) + assert os.path.isfile(path) + + +@pytest.mark.skipIf(WINDOWS) +class TestLink: + def test_link(self, tmpdir): + src = str(tmpdir.join('src')) + dst = str(tmpdir.join('dst')) + touch(src) + link(src, dst) + assert os.path.exists(dst)