Skip to content

Commit

Permalink
Merge pull request #160 from jedie/updates
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
jedie authored Dec 3, 2024
2 parents 690a4fb + fb1201a commit 7b8d152
Show file tree
Hide file tree
Showing 16 changed files with 450 additions and 465 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ default_install_hook_types:

repos:
- repo: https://github.com/jedie/cli-base-utilities
rev: v0.13.1
rev: v0.14.0
hooks:
- id: update-readme-history
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ Usage: ./dev-cli.py [OPTIONS] COMMAND [ARGS]...
[comment]: <> (✂✂✂ auto generated history start ✂✂✂)

* [v0.5.0](https://github.com/jedie/cookiecutter_templates/compare/v0.3.0...v0.5.0)
* 2024-12-03 - Use new cli_base
* 2024-12-03 - Remove tyro work-a-round
* 2024-11-21 - Replace click by tyro in uv-python
* 2024-11-14 - New template: "make-uv-python"
* 2024-11-13 - Migrate to uv
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ default_install_hook_types:

repos:
- repo: https://github.com/jedie/cli-base-utilities
rev: v0.13.1
rev: v0.14.0
hooks:
- id: update-readme-history
255 changes: 134 additions & 121 deletions generated_templates/uv-python/your_cool_package/uv.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ def version():

def main():
print_version(your_cool_package)

rich_traceback_install()

# Work-a-round for: https://github.com/brentyi/tyro/issues/205
app._subcommands = {k.replace('_', '-'): v for k, v in app._subcommands.items()}

app.cli(
prog='./cli.py',
description=constants.CLI_EPILOG,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def version():

def main():
print_version(your_cool_package)

rich_traceback_install()

if len(sys.argv) >= 2:
Expand All @@ -63,9 +62,6 @@ def main():
if real_func := command_map.get(command):
real_func(argv=sys.argv, exit_after_run=True)

# Work-a-round for: https://github.com/brentyi/tyro/issues/205
app._subcommands = {k.replace('_', '-'): v for k, v in app._subcommands.items()}

app.cli(
prog='./dev-cli.py',
description=constants.CLI_EPILOG,
Expand Down
43 changes: 3 additions & 40 deletions managetemplates/cli_dev/packaging.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import logging
from tempfile import NamedTemporaryFile

import click
from bx_py_utils.pyproject_toml import get_pyproject_config
from cli_base.cli_tools.dev_tools import run_unittest_cli
from cli_base.cli_tools.subprocess_utils import ToolsExecutor
from cli_base.cli_tools.verbosity import OPTION_KWARGS_VERBOSE, setup_logging
from cli_base.run_pip_audit import run_pip_audit
from manageprojects.utilities.publish import publish_package

import managetemplates
Expand All @@ -25,50 +24,14 @@ def install():
tools_executor.verbose_check_call('pip', 'install', '--no-deps', '-e', '.')


def run_pip_audit(verbosity: int):
tools_executor = ToolsExecutor(cwd=PACKAGE_ROOT)

with NamedTemporaryFile(prefix='temp_requirements_', suffix='.txt') as temp_file:
tools_executor.verbose_check_call(
'uv',
'export',
'--no-header',
'--frozen',
'--no-editable',
'--no-emit-project',
'-o',
temp_file.name,
)

config: dict = get_pyproject_config(
section=('tool', 'cli_base', 'pip_audit'),
base_path=PACKAGE_ROOT,
)
logger.debug('pip_audit config: %r', config)
assert isinstance(config, dict), f'Expected a dict: {config=}'

popenargs = ['pip-audit', '--strict', '--require-hashes']

if verbosity:
popenargs.append(f'-{"v" * verbosity}')

for vulnerability_id in config.get('ignore-vuln', []):
popenargs.extend(['--ignore-vuln', vulnerability_id])

popenargs.extend(['-r', temp_file.name])

logger.debug('pip_audit args: %s', popenargs)
tools_executor.verbose_check_call(*popenargs)


@cli.command()
@click.option('-v', '--verbosity', **OPTION_KWARGS_VERBOSE)
def pip_audit(verbosity: int):
"""
Run pip-audit check against current requirements files
"""
setup_logging(verbosity=verbosity)
run_pip_audit(verbosity=verbosity)
run_pip_audit(base_path=PACKAGE_ROOT, verbosity=verbosity)


@cli.command()
Expand All @@ -85,7 +48,7 @@ def update(verbosity: int):
tools_executor.verbose_check_call('pip', 'install', '-U', 'uv')
tools_executor.verbose_check_call('uv', 'lock', '--upgrade')

run_pip_audit(verbosity=verbosity)
run_pip_audit(base_path=PACKAGE_ROOT, verbosity=verbosity)

# Install new dependencies in current .venv:
tools_executor.verbose_check_call('uv', 'sync')
Expand Down
22 changes: 11 additions & 11 deletions managetemplates/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from cli_base.cli_tools.test_utils.rich_test_utils import NoColorRichClickCli
from cli_base.cli_tools.test_utils.rich_test_utils import NoColorRichClickCli, invoke
from manageprojects.tests.base import BaseTestCase

from managetemplates import __version__, constants
Expand All @@ -7,8 +7,8 @@
class DevCliTestCase(BaseTestCase):

def test_install(self):
with NoColorRichClickCli() as cm:
output = cm.invoke(cli_bin=constants.PACKAGE_ROOT / 'dev-cli.py', args=('install',))
with NoColorRichClickCli():
output = invoke(cli_bin=constants.PACKAGE_ROOT / 'dev-cli.py', args=('install',))
self.assert_in_content(
got=output,
parts=(
Expand All @@ -18,17 +18,17 @@ def test_install(self):
)

def test_pass_tox_command(self):
with NoColorRichClickCli() as cm:
output = cm.invoke(cli_bin=constants.PACKAGE_ROOT / 'dev-cli.py', args=('tox', '--help'))
with NoColorRichClickCli():
output = invoke(cli_bin=constants.PACKAGE_ROOT / 'dev-cli.py', args=('tox', '--help'))
self.assert_in_content(
got=output,
parts=(
'usage: tox [-h] ',
'list environments',
),
)
with NoColorRichClickCli() as cm:
output = cm.invoke(cli_bin=constants.PACKAGE_ROOT / 'dev-cli.py', args=('tox', 'list'))
with NoColorRichClickCli():
output = invoke(cli_bin=constants.PACKAGE_ROOT / 'dev-cli.py', args=('tox', 'list'))
self.assert_in_content(
got=output,
parts=(
Expand All @@ -39,8 +39,8 @@ def test_pass_tox_command(self):
)

def test_pass_unittest_command(self):
with NoColorRichClickCli() as cm:
output = cm.invoke(cli_bin=constants.PACKAGE_ROOT / 'dev-cli.py', args=('test', '--help'))
with NoColorRichClickCli():
output = invoke(cli_bin=constants.PACKAGE_ROOT / 'dev-cli.py', args=('test', '--help'))
self.assert_in_content(
got=output,
parts=(
Expand All @@ -53,8 +53,8 @@ def test_pass_unittest_command(self):
)

def test_coverage_help(self):
with NoColorRichClickCli() as cm:
output = cm.invoke(cli_bin=constants.PACKAGE_ROOT / 'dev-cli.py', args=('coverage', '--help'))
with NoColorRichClickCli():
output = invoke(cli_bin=constants.PACKAGE_ROOT / 'dev-cli.py', args=('coverage', '--help'))
self.assert_in_content(
got=output,
parts=(
Expand Down
18 changes: 7 additions & 11 deletions managetemplates/tests/test_project_setup.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from __future__ import annotations

import subprocess
import tempfile
from pathlib import Path
from unittest.mock import patch

from bx_py_utils.path import assert_is_dir, assert_is_file
from cli_base.cli_tools.code_style import assert_code_style
from cli_base.cli_tools.subprocess_utils import ToolsExecutor
from cli_base.cli_tools.test_utils.rich_test_utils import invoke
from manageprojects.test_utils.click_cli_utils import invoke_click
from manageprojects.test_utils.project_setup import check_editor_config, get_py_max_line_length
from manageprojects.test_utils.subprocess import SubprocessCallMock as SubprocessCallMockOrigin
Expand Down Expand Up @@ -78,16 +79,10 @@ def test_version(self):
version = Version(__version__) # Will raise InvalidVersion() if wrong formatted
self.assertEqual(str(version), __version__)

cli_bin = PACKAGE_ROOT / 'cli.py'
assert_is_file(cli_bin)

output = subprocess.check_output([cli_bin, 'version'], text=True)
output = invoke(cli_bin=PACKAGE_ROOT / 'cli.py', args=('version',))
self.assertIn(f'managetemplates v{__version__}', output)

dev_cli_bin = PACKAGE_ROOT / 'dev-cli.py'
assert_is_file(dev_cli_bin)

output = subprocess.check_output([dev_cli_bin, 'version'], text=True)
output = invoke(cli_bin=PACKAGE_ROOT / 'dev-cli.py', args=('version',))
self.assertIn(f'managetemplates v{__version__}', output)

def test_code_style(self):
Expand Down Expand Up @@ -115,7 +110,7 @@ class NamedTemporaryFileMock:
name = '/tmp/temp_requirements_MOCK.txt'

def __init__(self, **kwargs):
assert kwargs == {'prefix': 'temp_requirements_', 'suffix': '.txt'}, f'{kwargs=}'
assert kwargs == {'prefix': 'requirements', 'suffix': '.txt'}, f'{kwargs=}'

def __enter__(self):
return self
Expand All @@ -124,7 +119,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
assert exc_type is None, f'{exc_type=}'

with (
patch.object(packaging, 'NamedTemporaryFile', NamedTemporaryFileMock),
patch.object(tempfile, 'NamedTemporaryFile', NamedTemporaryFileMock),
SubprocessCallMock() as call_mock,
):
invoke_click(dev_cli, 'update')
Expand Down Expand Up @@ -158,6 +153,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
'.../bin/pip-audit',
'--strict',
'--require-hashes',
'--disable-pip',
'-r',
'/tmp/temp_requirements_MOCK.txt',
],
Expand Down
4 changes: 3 additions & 1 deletion managetemplates/tests/test_readme_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from cli_base.cli_tools.constants import GITHUB_ACTION
from cli_base.cli_tools.git_history import update_readme_history

from managetemplates.cli_dev import PACKAGE_ROOT


class ReadmeHistoryTestCase(TestCase):
@skipIf(
Expand All @@ -12,4 +14,4 @@ class ReadmeHistoryTestCase(TestCase):
reason='Skip on github actions',
)
def test_readme_history(self):
update_readme_history(raise_update_error=True)
update_readme_history(base_path=PACKAGE_ROOT, raise_update_error=True)
11 changes: 6 additions & 5 deletions uv-python/update_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ def main(verbose):

verbose_check_call(sys.executable, 'dev-cli.py', 'update', cwd=pkg_path, verbose=verbose)

# Sync the requirements back to the cookiecutter template:
verbose_copy2(
src=pkg_path / 'uv.lock',
dst=PACKAGE_ROOT / 'uv-python' / '{{ cookiecutter.package_name }}' / 'uv.lock',
)
# Sync back to the cookiecutter template:
for file_name in ('uv.lock', '.pre-commit-config.yaml'):
verbose_copy2(
src=pkg_path / file_name,
dst=PACKAGE_ROOT / 'uv-python' / '{{ cookiecutter.package_name }}' / file_name,
)


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ default_install_hook_types:

repos:
- repo: https://github.com/jedie/cli-base-utilities
rev: v0.13.1
rev: v0.14.0
hooks:
- id: update-readme-history
Loading

0 comments on commit 7b8d152

Please sign in to comment.