Coverage for src / rendercv / cli / copy_templates.py: 100%
16 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-25 13:49 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-25 13:49 +0000
1import os
2import pathlib
3import shutil
4import stat
5from typing import Literal
8def copy_templates(
9 template_type: Literal["markdown", "typst"], copy_templates_to: pathlib.Path
10) -> None:
11 """Copy built-in template directory to user location for customization.
13 Why:
14 Users creating custom themes need starting templates to modify.
16 Args:
17 template_type: Which template set to copy.
18 copy_templates_to: Destination directory path.
19 """
20 # copy the package's theme files to the current directory
21 template_directory = (
22 pathlib.Path(__file__).parent.parent
23 / "renderer"
24 / "templater"
25 / "templates"
26 / template_type
27 )
28 # copy the folder but don't include __init__.py:
29 shutil.copytree(
30 template_directory,
31 copy_templates_to,
32 ignore=shutil.ignore_patterns("__init__.py", "__pycache__"),
33 )
34 make_tree_writable(copy_templates_to)
37def make_tree_writable(path: pathlib.Path) -> None:
38 """Add user-write permission to all files and directories in a tree.
40 Why:
41 On immutable distributions like NixOS, package files are read-only.
42 shutil.copytree preserves source permissions, so copied files remain
43 read-only. Users need write access to customize templates.
45 Args:
46 path: Root directory to make writable.
47 """
48 for dirpath, _, filenames in os.walk(path):
49 dp = pathlib.Path(dirpath)
50 dp.chmod(dp.stat().st_mode | stat.S_IWUSR)
51 for filename in filenames:
52 fp = dp / filename
53 fp.chmod(fp.stat().st_mode | stat.S_IWUSR)