summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoffrey F <joffrey@docker.com>2017-01-06 16:37:15 -0800
committerJoffrey F <joffrey@docker.com>2017-01-06 16:37:15 -0800
commit9450442c8c3e69d6ec82dc9610fe7f8ee31181f2 (patch)
tree6496b11a84af1e0a0ee2da7f1870555ef12f12bd
parent6d871990d207027bc73ab16eae539af280151016 (diff)
downloaddocker-py-9450442c8c3e69d6ec82dc9610fe7f8ee31181f2.tar.gz
Accept / as a path separator in dockerignore patterns on all platformscompose-4302-dockerignore-windows
Signed-off-by: Joffrey F <joffrey@docker.com>
-rw-r--r--docker/utils/utils.py26
-rw-r--r--tests/unit/utils_test.py18
2 files changed, 35 insertions, 9 deletions
diff --git a/docker/utils/utils.py b/docker/utils/utils.py
index 4e5f454..e12fcf0 100644
--- a/docker/utils/utils.py
+++ b/docker/utils/utils.py
@@ -175,11 +175,17 @@ def should_check_directory(directory_path, exclude_patterns, include_patterns):
# docker logic (2016-10-27):
# https://github.com/docker/docker/blob/bc52939b0455116ab8e0da67869ec81c1a1c3e2c/pkg/archive/archive.go#L640-L671
- path_with_slash = directory_path + os.sep
- possible_child_patterns = [pattern for pattern in include_patterns if
- (pattern + os.sep).startswith(path_with_slash)]
- directory_included = should_include(directory_path, exclude_patterns,
- include_patterns)
+ def normalize_path(path):
+ return path.replace(os.path.sep, '/')
+
+ path_with_slash = normalize_path(directory_path) + '/'
+ possible_child_patterns = [
+ pattern for pattern in map(normalize_path, include_patterns)
+ if (pattern + '/').startswith(path_with_slash)
+ ]
+ directory_included = should_include(
+ directory_path, exclude_patterns, include_patterns
+ )
return directory_included or len(possible_child_patterns) > 0
@@ -195,9 +201,11 @@ def get_paths(root, exclude_patterns, include_patterns, has_exceptions=False):
# by mutating the dirs we're iterating over.
# This looks strange, but is considered the correct way to skip
# traversal. See https://docs.python.org/2/library/os.html#os.walk
- dirs[:] = [d for d in dirs if
- should_check_directory(os.path.join(parent, d),
- exclude_patterns, include_patterns)]
+ dirs[:] = [
+ d for d in dirs if should_check_directory(
+ os.path.join(parent, d), exclude_patterns, include_patterns
+ )
+ ]
for path in dirs:
if should_include(os.path.join(parent, path),
@@ -213,7 +221,7 @@ def get_paths(root, exclude_patterns, include_patterns, has_exceptions=False):
def match_path(path, pattern):
- pattern = pattern.rstrip('/')
+ pattern = pattern.rstrip('/' + os.path.sep)
if pattern:
pattern = os.path.relpath(pattern)
diff --git a/tests/unit/utils_test.py b/tests/unit/utils_test.py
index 743d076..cf00616 100644
--- a/tests/unit/utils_test.py
+++ b/tests/unit/utils_test.py
@@ -780,6 +780,16 @@ class ExcludePathsTest(unittest.TestCase):
])
)
+ @pytest.mark.skipif(
+ not IS_WINDOWS_PLATFORM, reason='Backslash patterns only on Windows'
+ )
+ def test_directory_with_subdir_exception_win32_pathsep(self):
+ assert self.exclude(['foo', '!foo\\bar']) == convert_paths(
+ self.all_paths - set([
+ 'foo/a.py', 'foo/b.py', 'foo', 'foo/Dockerfile3'
+ ])
+ )
+
def test_directory_with_wildcard_exception(self):
assert self.exclude(['foo', '!foo/*.py']) == convert_paths(
self.all_paths - set([
@@ -792,6 +802,14 @@ class ExcludePathsTest(unittest.TestCase):
self.all_paths - set(['foo/bar', 'foo/bar/a.py'])
)
+ @pytest.mark.skipif(
+ not IS_WINDOWS_PLATFORM, reason='Backslash patterns only on Windows'
+ )
+ def test_subdirectory_win32_pathsep(self):
+ assert self.exclude(['foo\\bar']) == convert_paths(
+ self.all_paths - set(['foo/bar', 'foo/bar/a.py'])
+ )
+
class TarTest(unittest.TestCase):
def test_tar_with_excludes(self):