Skip to content

Commit

Permalink
Enhance output and use EncloseRuleContext or Rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr. Test committed Sep 5, 2024
1 parent 24910b0 commit 858153b
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 209 deletions.
2 changes: 0 additions & 2 deletions managetemplates/cli_app/manage_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,4 @@ def reverse(template_name: str | None):
if template_name and template_name != pkg_name:
continue

print('_' * 100)
print(pkg_name)
reverse_test_project(pkg_name=pkg_name)
119 changes: 61 additions & 58 deletions managetemplates/utilities/reverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

from bx_py_utils.path import assert_is_file
from cli_base.cli_tools.git import Git
from cli_base.cli_tools.rich_utils import EncloseRuleContext
from cookiecutter.generate import generate_context
from cookiecutter.prompt import prompt_for_config
from manageprojects.cookiecutter_generator import create_cookiecutter_template
from manageprojects.utilities.temp_path import TemporaryDirectory
from rich import print # noqa
from rich.rule import Rule

from managetemplates import constants

Expand All @@ -32,61 +34,62 @@ def get_cookiecutter_context(src_path: Path) -> dict:


def reverse_test_project(pkg_name: str) -> None:
test_path = constants.TEST_PATH / pkg_name
src_path = constants.PACKAGE_ROOT / pkg_name

print(f'Reverse {test_path} to Cookiecutter template here: {src_path}')

cookiecutter_context = get_cookiecutter_context(src_path)
assert cookiecutter_context, f'No cookiecutter context found here: {src_path}'

# Move all files that are in our git into a temp directory:
git = Git(cwd=constants.PACKAGE_ROOT, detect_root=True)
file_paths = git.ls_files(verbose=True)

with TemporaryDirectory(prefix=f'{pkg_name}_', cleanup=True) as temp_dir:
destination = Path(temp_dir) / pkg_name
temp_git_path = Path(temp_dir) / 'git_temp'

# create_cookiecutter_template() needs a git repo, so:
# 1. Copy all files that are under version control into temp dir:
for test_file_path in test_path.rglob('*'):
if test_file_path in file_paths:
# It's a file under version control -> copy it into temp dir
rel_test_file_path = test_file_path.relative_to(test_path)
dst_path = temp_git_path / rel_test_file_path
dst_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(test_file_path, dst_path)

# 2. Add all files to a new git repo:
git = Git(cwd=temp_git_path, detect_root=False)
git.init()
git.add('.')
git.commit(comment='Initial commit')

print('_' * 100)
print('Reverse Cookiecutter template into temp directory:')

create_cookiecutter_template(
source_path=temp_git_path, # Use the files from temp git repo ;)
destination=destination,
cookiecutter_context=cookiecutter_context,
)

print('_' * 100)
print('Copy files from temp to final destination:')

for item in destination.rglob('*'):
if item.is_dir():
continue
elif item.is_file():
dst_path = src_path / item.relative_to(destination)
print(item, '->', dst_path)
dst_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(item, dst_path)
else:
print(f'Ignore non-file: {item}')

print('-' * 100)

print('\ndone.')
with EncloseRuleContext(f'reverse: {pkg_name!r}'):
test_path = constants.TEST_PATH / pkg_name
src_path = constants.PACKAGE_ROOT / pkg_name

print(f'Reverse {test_path} to Cookiecutter template here: {src_path}')

cookiecutter_context = get_cookiecutter_context(src_path)
assert cookiecutter_context, f'No cookiecutter context found here: {src_path}'

# Move all files that are in our git into a temp directory:
git = Git(cwd=constants.PACKAGE_ROOT, detect_root=True)
file_paths = git.ls_files(verbose=True)

with TemporaryDirectory(prefix=f'{pkg_name}_', cleanup=True) as temp_dir:
destination = Path(temp_dir) / pkg_name
temp_git_path = Path(temp_dir) / 'git_temp'

# create_cookiecutter_template() needs a git repo, so:
# 1. Copy all files that are under version control into temp dir:
for test_file_path in test_path.rglob('*'):
if test_file_path in file_paths:
# It's a file under version control -> copy it into temp dir
rel_test_file_path = test_file_path.relative_to(test_path)
dst_path = temp_git_path / rel_test_file_path
dst_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(test_file_path, dst_path)

# 2. Add all files to a new git repo:
git = Git(cwd=temp_git_path, detect_root=False)
git.init()
git.add('.')
git.commit(comment='Initial commit')

print(Rule(characters='_'))
print('Reverse Cookiecutter template into temp directory:')

create_cookiecutter_template(
source_path=temp_git_path, # Use the files from temp git repo ;)
destination=destination,
cookiecutter_context=cookiecutter_context,
)

print(Rule(characters='_'))
print('Copy files from temp to final destination:')

for item in destination.rglob('*'):
if item.is_dir():
continue
elif item.is_file():
dst_path = src_path / item.relative_to(destination)
print(item, '->', dst_path)
dst_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(item, dst_path)
else:
print(f'Ignore non-file: {item}')

print(Rule(characters='-'))

print('\ndone.')
51 changes: 25 additions & 26 deletions managetemplates/utilities/sync_cookiecutter_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import django_example
from bx_py_utils.path import assert_is_dir
from cli_base.cli_tools.rich_utils import EncloseRuleContext
from cli_base.cli_tools.subprocess_utils import verbose_check_call
from rich import print
from rich.rule import Rule

from managetemplates.constants import ALL_TEMPLATES, PACKAGE_ROOT, UPDATE_TEMPLATE_REQ_FILENAME
from managetemplates.utilities.cookiecutter_utils import run_cookiecutter
Expand All @@ -18,25 +18,24 @@ def cookiecutter_templates2generated(*, force_recreate: bool, only_template: str
template_names = sorted(ALL_TEMPLATES)

for only_template in template_names:
print(Rule(title=f'templates2generated: {only_template!r}'))

extra_context = {}
if only_template == 'yunohost_django_package':
extra_context = dict(
# Some projects test depends on the current upstream version
# So we have to set these version correct here:
upstream_version=django_example.__version__,
with EncloseRuleContext(f'templates2generated: {only_template!r}'):
extra_context = {}
if only_template == 'yunohost_django_package':
extra_context = dict(
# Some projects test depends on the current upstream version
# So we have to set these version correct here:
upstream_version=django_example.__version__,
)

pkg_path: Path = run_cookiecutter(
template_name=only_template,
force_recreate=force_recreate,
extra_context=extra_context,
)
print(pkg_path)

pkg_path: Path = run_cookiecutter(
template_name=only_template,
force_recreate=force_recreate,
extra_context=extra_context,
)
print(pkg_path)

if len(template_names) == 1:
return pkg_path
if len(template_names) == 1:
return pkg_path


def update_cookiecutter_templates_requirements(*, verbose: bool = False, only_template: str | None):
Expand All @@ -46,13 +45,13 @@ def update_cookiecutter_templates_requirements(*, verbose: bool = False, only_te
template_names = sorted(ALL_TEMPLATES)

for template_name in template_names:
print(Rule(title=f'Update requirements of {template_name!r}'))
with EncloseRuleContext(f'Update requirements of {template_name!r}'):

template_path = PACKAGE_ROOT / template_name
assert_is_dir(template_path)
template_path = PACKAGE_ROOT / template_name
assert_is_dir(template_path)

update_req_path = template_path / UPDATE_TEMPLATE_REQ_FILENAME
if not update_req_path.is_file():
print(f'[red]ERROR: file not found: {update_req_path.relative_to(PACKAGE_ROOT)}')
else:
verbose_check_call(sys.executable, UPDATE_TEMPLATE_REQ_FILENAME, cwd=template_path, verbose=verbose)
update_req_path = template_path / UPDATE_TEMPLATE_REQ_FILENAME
if not update_req_path.is_file():
print(f'[red]ERROR: file not found: {update_req_path.relative_to(PACKAGE_ROOT)}')
else:
verbose_check_call(sys.executable, UPDATE_TEMPLATE_REQ_FILENAME, cwd=template_path, verbose=verbose)
6 changes: 2 additions & 4 deletions managetemplates/utilities/verbose_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
from pathlib import Path

from bx_py_utils.path import assert_is_dir, assert_is_file
from cli_base.cli_tools.rich_utils import EncloseRuleContext
from rich import print
from rich.rule import Rule

from managetemplates.constants import PACKAGE_ROOT


@EncloseRuleContext('Copy file+meta:')
def verbose_copy2(*, src: Path, dst: Path):
print(Rule(title='Copy file+meta:'))

assert_is_file(src)
assert_is_dir(dst.parent)

Expand All @@ -21,4 +20,3 @@ def verbose_copy2(*, src: Path, dst: Path):
src=src,
dst=dst,
)
print(Rule())
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ readme = "README.md"
authors = [
{name = 'Jens Diemer', email = '[email protected]'}
]
requires-python = ">=3.10,<4"
requires-python = ">=3.11"
dependencies = [
"cli-base-utilities>=0.8.0", # https://github.com/jedie/cli-base-utilities
"cli-base-utilities>=0.11.0", # https://github.com/jedie/cli-base-utilities
"manageprojects>=0.16.0", # https://github.com/jedie/manageprojects
"bx_py_utils", # https://github.com/boxine/bx_py_utils
"click", # https://github.com/pallets/click/
Expand Down
Loading

0 comments on commit 858153b

Please sign in to comment.