mirror of
https://github.com/copier-org/copier.git
synced 2025-05-05 15:32:54 +00:00
style: use pathlib
This commit is contained in:
parent
44e67289c8
commit
1c2f8d8b08
@ -189,7 +189,7 @@ class _Subcommand(cli.Application): # type: ignore[misc]
|
|||||||
Arguments:
|
Arguments:
|
||||||
path: The path to the YAML file to load.
|
path: The path to the YAML file to load.
|
||||||
"""
|
"""
|
||||||
with open(path) as f:
|
with Path(path).open() as f:
|
||||||
file_updates: AnyByStrDict = yaml.safe_load(f)
|
file_updates: AnyByStrDict = yaml.safe_load(f)
|
||||||
|
|
||||||
updates_without_cli_overrides = {
|
updates_without_cli_overrides = {
|
||||||
|
@ -204,7 +204,7 @@ class Worker:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
src_path: str | None = None
|
src_path: str | None = None
|
||||||
dst_path: Path = Path(".")
|
dst_path: Path = Path()
|
||||||
answers_file: RelativePath | None = None
|
answers_file: RelativePath | None = None
|
||||||
vcs_ref: str | None = None
|
vcs_ref: str | None = None
|
||||||
data: AnyByStrDict = field(default_factory=dict)
|
data: AnyByStrDict = field(default_factory=dict)
|
||||||
@ -1235,7 +1235,7 @@ class Worker:
|
|||||||
# The 3-way merge might have resolved conflicts automatically,
|
# The 3-way merge might have resolved conflicts automatically,
|
||||||
# so we need to check if the file contains conflict markers
|
# so we need to check if the file contains conflict markers
|
||||||
# before storing the file name for marking it as unmerged after the loop.
|
# before storing the file name for marking it as unmerged after the loop.
|
||||||
with open(fname) as conflicts_candidate:
|
with Path(fname).open() as conflicts_candidate:
|
||||||
if any(
|
if any(
|
||||||
line.rstrip()
|
line.rstrip()
|
||||||
in {"<<<<<<< before updating", ">>>>>>> after updating"}
|
in {"<<<<<<< before updating", ">>>>>>> after updating"}
|
||||||
|
@ -57,5 +57,5 @@ class Settings(BaseModel):
|
|||||||
def normalize(self, url: str) -> str:
|
def normalize(self, url: str) -> str:
|
||||||
"""Normalize an URL using user settings."""
|
"""Normalize an URL using user settings."""
|
||||||
if url.startswith("~"): # Only expand on str to avoid messing with URLs
|
if url.startswith("~"): # Only expand on str to avoid messing with URLs
|
||||||
url = expanduser(url)
|
url = expanduser(url) # noqa: PTH111
|
||||||
return url
|
return url
|
||||||
|
@ -173,7 +173,7 @@ class Task:
|
|||||||
cmd: str | Sequence[str]
|
cmd: str | Sequence[str]
|
||||||
extra_vars: dict[str, Any] = field(default_factory=dict)
|
extra_vars: dict[str, Any] = field(default_factory=dict)
|
||||||
condition: str | bool = True
|
condition: str | bool = True
|
||||||
working_directory: Path = Path(".")
|
working_directory: Path = Path()
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -325,7 +325,7 @@ class Template:
|
|||||||
return tuple(
|
return tuple(
|
||||||
self.config_data.get(
|
self.config_data.get(
|
||||||
"exclude",
|
"exclude",
|
||||||
DEFAULT_EXCLUDE if Path(self.subdirectory) == Path(".") else [],
|
DEFAULT_EXCLUDE if Path(self.subdirectory) == Path() else [],
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -169,7 +169,7 @@ def handle_remove_readonly(
|
|||||||
excvalue = cast(OSError, exc if isinstance(exc, BaseException) else exc[1])
|
excvalue = cast(OSError, exc if isinstance(exc, BaseException) else exc[1])
|
||||||
|
|
||||||
if func in (os.rmdir, os.remove, os.unlink) and excvalue.errno == errno.EACCES:
|
if func in (os.rmdir, os.remove, os.unlink) and excvalue.errno == errno.EACCES:
|
||||||
os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) # 0777
|
Path(path).chmod(stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) # 0777
|
||||||
func(path)
|
func(path)
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
@ -242,7 +242,7 @@ def get_git_objects_dir(path: Path) -> Path:
|
|||||||
).absolute()
|
).absolute()
|
||||||
|
|
||||||
|
|
||||||
def set_git_alternates(*repos: Path, path: Path = Path(".")) -> None:
|
def set_git_alternates(*repos: Path, path: Path = Path()) -> None:
|
||||||
"""Set Git alternates to borrow Git objects from other repositories.
|
"""Set Git alternates to borrow Git objects from other repositories.
|
||||||
|
|
||||||
Alternates are paths of other repositories' object directories written to
|
Alternates are paths of other repositories' object directories written to
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
@ -190,12 +189,12 @@ def clone(url: str, ref: str | None = None) -> str:
|
|||||||
_clone = _clone["--filter=blob:none"]
|
_clone = _clone["--filter=blob:none"]
|
||||||
_clone()
|
_clone()
|
||||||
# Include dirty changes if checking out a local HEAD
|
# Include dirty changes if checking out a local HEAD
|
||||||
if ref in {None, "HEAD"} and os.path.exists(url) and Path(url).is_dir():
|
url_abspath = Path(url).absolute()
|
||||||
|
if ref in {None, "HEAD"} and url_abspath.is_dir():
|
||||||
is_dirty = False
|
is_dirty = False
|
||||||
with local.cwd(url):
|
with local.cwd(url):
|
||||||
is_dirty = bool(git("status", "--porcelain").strip())
|
is_dirty = bool(git("status", "--porcelain").strip())
|
||||||
if is_dirty:
|
if is_dirty:
|
||||||
url_abspath = Path(url).absolute()
|
|
||||||
with local.cwd(location):
|
with local.cwd(location):
|
||||||
git("--git-dir=.git", f"--work-tree={url_abspath}", "add", "-A")
|
git("--git-dir=.git", f"--work-tree={url_abspath}", "add", "-A")
|
||||||
git(
|
git(
|
||||||
|
@ -118,6 +118,7 @@ extend-select = [
|
|||||||
"I",
|
"I",
|
||||||
"PERF",
|
"PERF",
|
||||||
"PGH",
|
"PGH",
|
||||||
|
"PTH",
|
||||||
"UP",
|
"UP",
|
||||||
]
|
]
|
||||||
extend-ignore = ['B028', "B904", "D105", "D107", "E501"]
|
extend-ignore = ['B028', "B904", "D105", "D107", "E501"]
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
NAMES = (
|
NAMES = (
|
||||||
"{VERSION_FROM}-{VERSION_CURRENT}-{VERSION_TO}-{STAGE}.json",
|
"{VERSION_FROM}-{VERSION_CURRENT}-{VERSION_TO}-{STAGE}.json",
|
||||||
@ -9,5 +10,5 @@ NAMES = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
for name in NAMES:
|
for name in NAMES:
|
||||||
with open(name.format(**os.environ), "w") as fd:
|
with Path(name.format(**os.environ)).open("w") as fd:
|
||||||
json.dump(sys.argv, fd)
|
json.dump(sys.argv, fd)
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import os
|
import os
|
||||||
import os.path
|
|
||||||
import sys
|
import sys
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
with open("created-with-tasks.txt", "a", newline="\n") as cwt:
|
with Path("created-with-tasks.txt").open("a", newline="\n") as cwt:
|
||||||
cwt.write(" ".join([os.environ["STAGE"]] + sys.argv[1:]) + "\n")
|
cwt.write(" ".join([os.environ["STAGE"]] + sys.argv[1:]) + "\n")
|
||||||
with suppress(FileNotFoundError):
|
with suppress(FileNotFoundError):
|
||||||
os.unlink("delete-in-tasks.txt")
|
Path("delete-in-tasks.txt").unlink()
|
||||||
|
@ -329,7 +329,7 @@ def test_flags_extra_fails() -> None:
|
|||||||
with pytest.raises(ValidationError):
|
with pytest.raises(ValidationError):
|
||||||
copier.Worker( # type: ignore[call-arg]
|
copier.Worker( # type: ignore[call-arg]
|
||||||
src_path="..",
|
src_path="..",
|
||||||
dst_path=Path("."),
|
dst_path=Path(),
|
||||||
i_am_not_a_member="and_i_do_not_belong_here",
|
i_am_not_a_member="and_i_do_not_belong_here",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import filecmp
|
import filecmp
|
||||||
import os
|
|
||||||
import platform
|
import platform
|
||||||
import stat
|
import stat
|
||||||
import sys
|
import sys
|
||||||
@ -302,10 +301,10 @@ def test_skip_if_exists(tmp_path: Path) -> None:
|
|||||||
|
|
||||||
def test_timestamp_identical(tmp_path: Path) -> None:
|
def test_timestamp_identical(tmp_path: Path) -> None:
|
||||||
copier.run_copy(str(Path("tests", "demo_skip_dst")), tmp_path)
|
copier.run_copy(str(Path("tests", "demo_skip_dst")), tmp_path)
|
||||||
modification_time_before = os.path.getmtime(tmp_path / "a.noeof.txt")
|
modification_time_before = (tmp_path / "a.noeof.txt").stat().st_mtime
|
||||||
sleep(2)
|
sleep(2)
|
||||||
copier.run_copy(str(Path("tests", "demo_skip_dst")), tmp_path)
|
copier.run_copy(str(Path("tests", "demo_skip_dst")), tmp_path)
|
||||||
modification_time_after = os.path.getmtime(tmp_path / "a.noeof.txt")
|
modification_time_after = (tmp_path / "a.noeof.txt").stat().st_mtime
|
||||||
|
|
||||||
assert modification_time_before == modification_time_after
|
assert modification_time_before == modification_time_after
|
||||||
|
|
||||||
|
@ -122,7 +122,7 @@ def test_update(tmp_path_factory: pytest.TempPathFactory) -> None:
|
|||||||
Path("test_file.txt").write_text("Test content")
|
Path("test_file.txt").write_text("Test content")
|
||||||
|
|
||||||
# test updating a file
|
# test updating a file
|
||||||
with open("aaaa.txt", "a") as f:
|
with Path("aaaa.txt").open("a") as f:
|
||||||
f.write("dolor sit amet")
|
f.write("dolor sit amet")
|
||||||
|
|
||||||
# test updating a symlink
|
# test updating a symlink
|
||||||
|
@ -38,9 +38,9 @@ def test_copy_symlink(tmp_path_factory: pytest.TempPathFactory) -> None:
|
|||||||
vcs_ref="HEAD",
|
vcs_ref="HEAD",
|
||||||
)
|
)
|
||||||
|
|
||||||
assert os.path.exists(dst / "target.txt")
|
assert (dst / "target.txt").exists()
|
||||||
assert os.path.exists(dst / "symlink.txt")
|
assert (dst / "symlink.txt").exists()
|
||||||
assert os.path.islink(dst / "symlink.txt")
|
assert (dst / "symlink.txt").is_symlink()
|
||||||
assert (dst / "symlink.txt").readlink() == Path("target.txt")
|
assert (dst / "symlink.txt").readlink() == Path("target.txt")
|
||||||
|
|
||||||
|
|
||||||
@ -73,9 +73,9 @@ def test_copy_symlink_templated_name(tmp_path_factory: pytest.TempPathFactory) -
|
|||||||
vcs_ref="HEAD",
|
vcs_ref="HEAD",
|
||||||
)
|
)
|
||||||
|
|
||||||
assert os.path.exists(dst / "target.txt")
|
assert (dst / "target.txt").exists()
|
||||||
assert os.path.exists(dst / "symlink.txt")
|
assert (dst / "symlink.txt").exists()
|
||||||
assert os.path.islink(dst / "symlink.txt")
|
assert (dst / "symlink.txt").is_symlink()
|
||||||
assert (dst / "symlink.txt").readlink() == Path("target.txt")
|
assert (dst / "symlink.txt").readlink() == Path("target.txt")
|
||||||
|
|
||||||
|
|
||||||
@ -111,14 +111,13 @@ def test_copy_symlink_templated_target(
|
|||||||
vcs_ref="HEAD",
|
vcs_ref="HEAD",
|
||||||
)
|
)
|
||||||
|
|
||||||
assert os.path.exists(dst / "target.txt")
|
assert (dst / "target.txt").exists()
|
||||||
|
assert (dst / "symlink1.txt").exists()
|
||||||
assert os.path.exists(dst / "symlink1.txt")
|
assert (dst / "symlink1.txt").is_symlink()
|
||||||
assert os.path.islink(dst / "symlink1.txt")
|
|
||||||
assert (dst / "symlink1.txt").readlink() == Path("target.txt")
|
assert (dst / "symlink1.txt").readlink() == Path("target.txt")
|
||||||
|
|
||||||
assert not os.path.exists(dst / "symlink2.txt")
|
assert not (dst / "symlink2.txt").exists()
|
||||||
assert os.path.islink(dst / "symlink2.txt")
|
assert (dst / "symlink2.txt").is_symlink()
|
||||||
assert (dst / "symlink2.txt").readlink() == Path("{{ target_name }}.txt")
|
assert (dst / "symlink2.txt").readlink() == Path("{{ target_name }}.txt")
|
||||||
|
|
||||||
|
|
||||||
@ -149,11 +148,11 @@ def test_copy_symlink_missing_target(tmp_path_factory: pytest.TempPathFactory) -
|
|||||||
vcs_ref="HEAD",
|
vcs_ref="HEAD",
|
||||||
)
|
)
|
||||||
|
|
||||||
assert os.path.islink(dst / "symlink.txt")
|
assert (dst / "symlink.txt").is_symlink()
|
||||||
assert (dst / "symlink.txt").readlink() == Path("target.txt")
|
assert (dst / "symlink.txt").readlink() == Path("target.txt")
|
||||||
assert not os.path.exists(
|
assert not (
|
||||||
dst / "symlink.txt"
|
dst / "symlink.txt"
|
||||||
) # exists follows symlinks, It returns False as the target doesn't exist
|
).exists() # exists follows symlinks, It returns False as the target doesn't exist
|
||||||
|
|
||||||
|
|
||||||
def test_option_preserve_symlinks_false(
|
def test_option_preserve_symlinks_false(
|
||||||
@ -186,9 +185,9 @@ def test_option_preserve_symlinks_false(
|
|||||||
vcs_ref="HEAD",
|
vcs_ref="HEAD",
|
||||||
)
|
)
|
||||||
|
|
||||||
assert os.path.exists(dst / "target.txt")
|
assert (dst / "target.txt").exists()
|
||||||
assert os.path.exists(dst / "symlink.txt")
|
assert (dst / "symlink.txt").exists()
|
||||||
assert not os.path.islink(dst / "symlink.txt")
|
assert not (dst / "symlink.txt").is_symlink()
|
||||||
|
|
||||||
|
|
||||||
def test_option_preserve_symlinks_default(
|
def test_option_preserve_symlinks_default(
|
||||||
@ -220,9 +219,9 @@ def test_option_preserve_symlinks_default(
|
|||||||
vcs_ref="HEAD",
|
vcs_ref="HEAD",
|
||||||
)
|
)
|
||||||
|
|
||||||
assert os.path.exists(dst / "target.txt")
|
assert (dst / "target.txt").exists()
|
||||||
assert os.path.exists(dst / "symlink.txt")
|
assert (dst / "symlink.txt").exists()
|
||||||
assert not os.path.islink(dst / "symlink.txt")
|
assert not (dst / "symlink.txt").is_symlink()
|
||||||
|
|
||||||
|
|
||||||
def test_update_symlink(tmp_path_factory: pytest.TempPathFactory) -> None:
|
def test_update_symlink(tmp_path_factory: pytest.TempPathFactory) -> None:
|
||||||
@ -256,7 +255,7 @@ def test_update_symlink(tmp_path_factory: pytest.TempPathFactory) -> None:
|
|||||||
|
|
||||||
with local.cwd(src):
|
with local.cwd(src):
|
||||||
# test updating a symlink
|
# test updating a symlink
|
||||||
os.remove("symlink.txt")
|
Path("symlink.txt").unlink()
|
||||||
os.symlink("bbbb.txt", "symlink.txt")
|
os.symlink("bbbb.txt", "symlink.txt")
|
||||||
|
|
||||||
# dst must be vcs-tracked to use run_update
|
# dst must be vcs-tracked to use run_update
|
||||||
@ -312,10 +311,10 @@ def test_update_file_to_symlink(tmp_path_factory: pytest.TempPathFactory) -> Non
|
|||||||
|
|
||||||
with local.cwd(src):
|
with local.cwd(src):
|
||||||
# test updating a symlink
|
# test updating a symlink
|
||||||
os.remove("bbbb.txt")
|
Path("bbbb.txt").unlink()
|
||||||
os.symlink("aaaa.txt", "bbbb.txt")
|
os.symlink("aaaa.txt", "bbbb.txt")
|
||||||
os.remove("cccc.txt")
|
Path("cccc.txt").unlink()
|
||||||
with open("cccc.txt", "w+") as f:
|
with Path("cccc.txt").open("w+") as f:
|
||||||
f.write("Lorem ipsum")
|
f.write("Lorem ipsum")
|
||||||
|
|
||||||
# dst must be vcs-tracked to use run_update
|
# dst must be vcs-tracked to use run_update
|
||||||
@ -431,9 +430,9 @@ def test_copy_symlink_none_path(tmp_path_factory: pytest.TempPathFactory) -> Non
|
|||||||
vcs_ref="HEAD",
|
vcs_ref="HEAD",
|
||||||
)
|
)
|
||||||
|
|
||||||
assert os.path.exists(dst / "target.txt")
|
assert (dst / "target.txt").exists()
|
||||||
assert not os.path.exists(dst / "symlink.txt")
|
assert not (dst / "symlink.txt").exists()
|
||||||
assert not os.path.islink(dst / "symlink.txt")
|
assert not (dst / "symlink.txt").is_symlink()
|
||||||
|
|
||||||
|
|
||||||
def test_recursive_symlink(tmp_path_factory: pytest.TempPathFactory) -> None:
|
def test_recursive_symlink(tmp_path_factory: pytest.TempPathFactory) -> None:
|
||||||
|
@ -12,7 +12,7 @@ from .helpers import git
|
|||||||
|
|
||||||
def test_types() -> None:
|
def test_types() -> None:
|
||||||
"""Ensure source code static typing."""
|
"""Ensure source code static typing."""
|
||||||
result = PoeThePoet(Path("."))(["types"])
|
result = PoeThePoet(Path())(["types"])
|
||||||
assert result == 0
|
assert result == 0
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user