Exclude recursively

Excluding a folder and its contents didn't exclude its subfolders.

Now, excluding a folder excludes all its files and subfolders.

@Tecnativa TT20357
This commit is contained in:
Jairo Llopis 2020-02-24 14:03:03 +00:00
parent e72d1e5715
commit f2ae70a4a8
No known key found for this signature in database
GPG Key ID: 0CFC348F9B242B08
6 changed files with 18 additions and 7 deletions

View File

@ -177,7 +177,6 @@ _templates_suffix: .tmpl
_exclude:
- "*.bar"
- ".git"
- ".git/*"
# Shell-style patterns files/folders that *must be* copied, even if
# they are in the exclude list
@ -325,9 +324,6 @@ Uses the template in _src_path_ to generate a new project at _dst_path_.
A list of names or shell-style patterns matching files or folders
that must not be copied.
To exclude a folder you should use **two** entries, one for the folder and
the other for its content: `[".git", ".git/*"]`.
- **include** (list):<br>
A list of names or shell-style patterns matching files or folders that
must be included, even if its name is a match for the `exclude` list.

View File

@ -15,9 +15,7 @@ DEFAULT_EXCLUDE: Tuple[str, ...] = (
"~*",
"*.py[co]",
"__pycache__",
"__pycache__/*",
".git",
".git/*",
".DS_Store",
".svn",
)

View File

@ -153,12 +153,14 @@ def copy_local(conf: ConfigData) -> None:
folder: StrOrPath
rel_folder: StrOrPath
for folder, _, files in os.walk(conf.src_path):
for folder, sub_dirs, files in os.walk(conf.src_path):
rel_folder = str(folder).replace(str(conf.src_path), "", 1).lstrip(os.path.sep)
rel_folder = render.string(rel_folder)
rel_folder = str(rel_folder).replace("." + os.path.sep, ".", 1)
if must_filter(rel_folder):
# Folder is excluded, so stop walking it
sub_dirs[:] = []
continue
folder = Path(folder)

View File

@ -0,0 +1 @@
This file must not exist in copy.

View File

@ -0,0 +1,2 @@
_exclude:
- bad

12
tests/test_exclude.py Normal file
View File

@ -0,0 +1,12 @@
from copier.main import copy
from .helpers import PROJECT_TEMPLATE
SRC = f"{PROJECT_TEMPLATE}_exclude"
def test_recursive_exclude(tmp_path):
"""Copy is done properly when excluding recursively."""
copy(SRC, tmp_path)
assert not (tmp_path / "bad").is_dir()
assert not (tmp_path / "bad").exists()