Skip to content

Commit 8f1ab8f

Browse files
committed
feat: Add _operation variable
1 parent e444514 commit 8f1ab8f

File tree

5 files changed

+176
-4
lines changed

5 files changed

+176
-4
lines changed

copier/main.py

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
import subprocess
88
import sys
99
from contextlib import suppress
10+
from contextvars import ContextVar
1011
from dataclasses import asdict, field, replace
1112
from filecmp import dircmp
12-
from functools import cached_property, partial
13+
from functools import cached_property, partial, wraps
1314
from itertools import chain
1415
from pathlib import Path
1516
from shutil import rmtree
@@ -64,13 +65,38 @@
6465
AnyByStrDict,
6566
AnyByStrMutableMapping,
6667
JSONSerializable,
68+
Operation,
69+
ParamSpec,
6770
RelativePath,
6871
StrOrPath,
6972
)
7073
from .user_data import AnswersMap, Question, load_answersfile_data
7174
from .vcs import get_git
7275

7376
_T = TypeVar("_T")
77+
_P = ParamSpec("_P")
78+
79+
_operation: ContextVar[Operation] = ContextVar("_operation")
80+
81+
82+
def as_operation(value: Operation) -> Callable[[Callable[_P, _T]], Callable[_P, _T]]:
83+
"""Decorator to set the current operation context, if not defined already.
84+
85+
This value is used to template specific configuration options.
86+
"""
87+
88+
def _decorator(func: Callable[_P, _T]) -> Callable[_P, _T]:
89+
@wraps(func)
90+
def _wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _T:
91+
token = _operation.set(_operation.get(value))
92+
try:
93+
return func(*args, **kwargs)
94+
finally:
95+
_operation.reset(token)
96+
97+
return _wrapper
98+
99+
return _decorator
74100

75101

76102
# HACK https://github.com/copier-org/copier/pull/1880#discussion_r1887491497
@@ -260,7 +286,7 @@ def _cleanup(self) -> None:
260286
for method in self._cleanup_hooks:
261287
method()
262288

263-
def _check_unsafe(self, mode: Literal["copy", "update"]) -> None:
289+
def _check_unsafe(self, mode: Operation) -> None:
264290
"""Check whether a template uses unsafe features."""
265291
if self.unsafe or self.settings.is_trusted(self.template.url):
266292
return
@@ -333,8 +359,10 @@ def _execute_tasks(self, tasks: Sequence[Task]) -> None:
333359
Arguments:
334360
tasks: The list of tasks to run.
335361
"""
362+
operation = _operation.get()
336363
for i, task in enumerate(tasks):
337364
extra_context = {f"_{k}": v for k, v in task.extra_vars.items()}
365+
extra_context["_operation"] = operation
338366

339367
if not cast_to_bool(self._render_value(task.condition, extra_context)):
340368
continue
@@ -364,7 +392,7 @@ def _execute_tasks(self, tasks: Sequence[Task]) -> None:
364392
/ Path(self._render_string(str(task.working_directory), extra_context))
365393
).absolute()
366394

367-
extra_env = {k.upper(): str(v) for k, v in task.extra_vars.items()}
395+
extra_env = {k[1:].upper(): str(v) for k, v in extra_context.items()}
368396
with local.cwd(working_directory), local.env(**extra_env):
369397
subprocess.run(task_cmd, shell=use_shell, check=True, env=local.env)
370398

@@ -632,7 +660,14 @@ def _pathjoin(
632660
@cached_property
633661
def match_exclude(self) -> Callable[[Path], bool]:
634662
"""Get a callable to match paths against all exclusions."""
635-
return self._path_matcher(self.all_exclusions)
663+
# Include the current operation in the rendering context.
664+
# Note: This method is a cached property, it needs to be regenerated
665+
# when reusing an instance in different contexts.
666+
extra_context = {"_operation": _operation.get()}
667+
return self._path_matcher(
668+
self._render_string(exclusion, extra_context=extra_context)
669+
for exclusion in self.all_exclusions
670+
)
636671

637672
@cached_property
638673
def match_skip(self) -> Callable[[Path], bool]:
@@ -935,6 +970,7 @@ def template_copy_root(self) -> Path:
935970
return self.template.local_abspath / subdir
936971

937972
# Main operations
973+
@as_operation("copy")
938974
def run_copy(self) -> None:
939975
"""Generate a subproject from zero, ignoring what was in the folder.
940976
@@ -945,6 +981,11 @@ def run_copy(self) -> None:
945981
946982
See [generating a project][generating-a-project].
947983
"""
984+
with suppress(AttributeError):
985+
# We might have switched operation context, ensure the cached property
986+
# is regenerated to re-render templates.
987+
del self.match_exclude
988+
948989
self._check_unsafe("copy")
949990
self._print_message(self.template.message_before_copy)
950991
self._ask()
@@ -971,6 +1012,7 @@ def run_copy(self) -> None:
9711012
# TODO Unify printing tools
9721013
print("") # padding space
9731014

1015+
@as_operation("copy")
9741016
def run_recopy(self) -> None:
9751017
"""Update a subproject, keeping answers but discarding evolution."""
9761018
if self.subproject.template is None:
@@ -981,6 +1023,7 @@ def run_recopy(self) -> None:
9811023
with replace(self, src_path=self.subproject.template.url) as new_worker:
9821024
new_worker.run_copy()
9831025

1026+
@as_operation("update")
9841027
def run_update(self) -> None:
9851028
"""Update a subproject that was already generated.
9861029
@@ -1028,6 +1071,11 @@ def run_update(self) -> None:
10281071
print(
10291072
f"Updating to template version {self.template.version}", file=sys.stderr
10301073
)
1074+
with suppress(AttributeError):
1075+
# We might have switched operation context, ensure the cached property
1076+
# is regenerated to re-render templates.
1077+
del self.match_exclude
1078+
10311079
self._apply_update()
10321080
self._print_message(self.template.message_after_update)
10331081

copier/types.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Complex types, annotations, validators."""
22

3+
import sys
34
from pathlib import Path
45
from typing import (
56
Annotated,
@@ -17,6 +18,11 @@
1718

1819
from pydantic import AfterValidator
1920

21+
if sys.version_info >= (3, 10):
22+
from typing import ParamSpec as ParamSpec
23+
else:
24+
from typing_extensions import ParamSpec as ParamSpec
25+
2026
# simple types
2127
StrOrPath = Union[str, Path]
2228
AnyByStrDict = Dict[str, Any]
@@ -37,6 +43,7 @@
3743
Env = Mapping[str, str]
3844
MissingType = NewType("MissingType", object)
3945
MISSING = MissingType(object())
46+
Operation = Literal["copy", "update"]
4047

4148

4249
# Validators

docs/configuring.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,18 @@ to know available options.
962962

963963
The CLI option can be passed several times to add several patterns.
964964

965+
Each pattern can be templated using Jinja.
966+
967+
!!! example
968+
969+
Templating `exclude` patterns using `_operation` allows to have files
970+
that are rendered once during `copy`, but are never updated:
971+
972+
```yaml
973+
_exclude:
974+
- "{% if _operation == 'update' -%}src/*_example.py{% endif %}"
975+
```
976+
965977
!!! info
966978

967979
When you define this parameter in `copier.yml`, it will **replace** the default
@@ -1421,6 +1433,8 @@ configuring `secret: true` in the [advanced prompt format][advanced-prompt-forma
14211433
exist, but always be present. If they do not exist in a project during an `update`
14221434
operation, they will be recreated.
14231435

1436+
Each pattern can be templated using Jinja.
1437+
14241438
!!! example
14251439

14261440
For example, it can be used if your project generates a password the 1st time and
@@ -1571,6 +1585,9 @@ other items not present.
15711585
- [invoke, end-process, "--full-conf={{ _copier_conf|to_json }}"]
15721586
# Your script can be run by the same Python environment used to run Copier
15731587
- ["{{ _copier_python }}", task.py]
1588+
# Run a command during the initial copy operation only, excluding updates
1589+
- command: ["{{ _copier_python }}", task.py]
1590+
when: "{{ _operation == 'copy' }}"
15741591
# OS-specific task (supported values are "linux", "macos", "windows" and `None`)
15751592
- command: rm {{ name_of_the_project }}/README.md
15761593
when: "{{ _copier_conf.os in ['linux', 'macos'] }}"

docs/creating.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ variable:
136136

137137
The name of the project root directory.
138138

139+
## Variables (context-dependent)
140+
141+
Some variables are only available in select contexts:
142+
143+
### `_operation`
144+
145+
The current operation, either `"copy"` or `"update"`.
146+
147+
Availability: [`exclude`](configuring.md#exclude), [`tasks`](configuring.md#tasks)
148+
139149
## Variables (context-specific)
140150

141151
Some rendering contexts provide variables unique to them:

tests/test_context.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import json
2+
from pathlib import Path
3+
4+
import pytest
5+
from plumbum import local
6+
7+
import copier
8+
9+
from .helpers import build_file_tree, git_save
10+
11+
12+
def test_exclude_templating_with_operation(
13+
tmp_path_factory: pytest.TempPathFactory,
14+
) -> None:
15+
"""
16+
Ensure it's possible to create one-off boilerplate files that are not
17+
managed during updates via `_exclude` using the `_operation` context variable.
18+
"""
19+
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
20+
21+
template = "{% if _operation == 'update' %}copy-only{% endif %}"
22+
with local.cwd(src):
23+
build_file_tree(
24+
{
25+
"copier.yml": f'_exclude:\n - "{template}"',
26+
"{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_yaml }}",
27+
"copy-only": "foo",
28+
"copy-and-update": "foo",
29+
}
30+
)
31+
git_save(tag="1.0.0")
32+
build_file_tree(
33+
{
34+
"copy-only": "bar",
35+
"copy-and-update": "bar",
36+
}
37+
)
38+
git_save(tag="2.0.0")
39+
copy_only = dst / "copy-only"
40+
copy_and_update = dst / "copy-and-update"
41+
42+
copier.run_copy(str(src), dst, defaults=True, overwrite=True, vcs_ref="1.0.0")
43+
for file in (copy_only, copy_and_update):
44+
assert file.exists()
45+
assert file.read_text() == "foo"
46+
47+
with local.cwd(dst):
48+
git_save()
49+
50+
copier.run_update(str(dst), overwrite=True)
51+
assert copy_only.read_text() == "foo"
52+
assert copy_and_update.read_text() == "bar"
53+
54+
55+
def test_task_templating_with_operation(
56+
tmp_path_factory: pytest.TempPathFactory, tmp_path: Path
57+
) -> None:
58+
"""
59+
Ensure that it is possible to define tasks that are only executed when copying.
60+
"""
61+
src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
62+
# Use a file outside the Copier working directories to ensure accurate tracking
63+
task_counter = tmp_path / "task_calls.txt"
64+
with local.cwd(src):
65+
build_file_tree(
66+
{
67+
"copier.yml": (
68+
f"""\
69+
_tasks:
70+
- command: echo {{{{ _operation }}}} >> {json.dumps(str(task_counter))}
71+
when: "{{{{ _operation == 'copy' }}}}"
72+
"""
73+
),
74+
"{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_yaml }}",
75+
}
76+
)
77+
git_save(tag="1.0.0")
78+
79+
copier.run_copy(str(src), dst, defaults=True, overwrite=True, unsafe=True)
80+
assert task_counter.exists()
81+
assert len(task_counter.read_text().splitlines()) == 1
82+
83+
with local.cwd(dst):
84+
git_save()
85+
86+
copier.run_recopy(dst, defaults=True, overwrite=True, unsafe=True)
87+
assert len(task_counter.read_text().splitlines()) == 2
88+
89+
copier.run_update(dst, defaults=True, overwrite=True, unsafe=True)
90+
assert len(task_counter.read_text().splitlines()) == 2

0 commit comments

Comments
 (0)