Skip to content

Commit

Permalink
Adopt dependency groups
Browse files Browse the repository at this point in the history
  • Loading branch information
layday committed Oct 15, 2024
1 parent 354f1c0 commit d303c19
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
43 changes: 27 additions & 16 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@
nox.options.error_on_external_run = True


_DEPENDENCY_GROUPS = {
'format_or_lint': ['ruff'],
'publish': ['twine'],
'report_coverage': ['coverage[toml]'],
}
_root = Path(__file__).parent


def _parse_dependency_group(item: str | dict[str, str]):
match item:
case str():
yield item
case {'include-group': group_ref}:
yield from iter(_dependency_groups[group_ref])
case _:
raise ValueError(f'Invalid dependency group item: {item}')

_ROOT = Path(__file__).parent

_dependency_groups = nox.project.load_toml(_root / 'pyproject.toml')['dependency-groups']
_dependency_groups = {
n.replace('_', '-'): {u for i in g for u in _parse_dependency_group(i)}
for n, g in _dependency_groups.items()
}


def _install_coverage_hook(session: nox.Session):
Expand All @@ -40,7 +51,7 @@ def _install_coverage_hook(session: nox.Session):

def _locate_or_build_packages(session: nox.Session):
wheels_metadata_json = {
d: _ROOT / 'dist' / d / '.wheel-metadata.json' for d in ['instawow', 'instawow-gui']
d: _root / 'dist' / d / '.wheel-metadata.json' for d in ['instawow', 'instawow-gui']
}
if not all(j.exists() for j in wheels_metadata_json.values()):
if os.environ.get('CI'):
Expand All @@ -62,7 +73,7 @@ def dev_env(session: nox.Session):
def format_code(session: nox.Session):
"Format source code."

session.install(*_DEPENDENCY_GROUPS['format_or_lint'])
session.install(*_dependency_groups['format'])

check = '--check' in session.posargs
skip_prettier = '--skip-prettier' in session.posargs
Expand All @@ -80,7 +91,7 @@ def format_code(session: nox.Session):
def lint(session: nox.Session):
"Lint source code."

session.install(*_DEPENDENCY_GROUPS['format_or_lint'])
session.install(*_dependency_groups['lint'])
session.run('ruff', 'check', '--output-format', 'full', *session.posargs, '.')
session.notify('format', ['--check'])

Expand All @@ -91,7 +102,7 @@ def test(session: nox.Session, minimum_versions: bool):
"Run the test suite."

if minimum_versions and session.venv_backend != 'uv':
session.error('`minimum_versions` only supported with uv')
session.error('`minimum_versions` requires uv')

if not os.environ.get('CI'):
session.create_tmp()
Expand Down Expand Up @@ -127,10 +138,10 @@ def test(session: nox.Session, minimum_versions: bool):


@nox.session
def produce_coverage_report(session: nox.Session):
def report_coverage(session: nox.Session):
"Produce coverage report."

session.install(*_DEPENDENCY_GROUPS['report_coverage'])
session.install(*_dependency_groups['report-coverage'])
session.run('coverage', 'combine')
session.run('coverage', 'html', '--skip-empty')
session.run('coverage', 'report', '-m')
Expand Down Expand Up @@ -171,7 +182,7 @@ def build_dists(session: nox.Session):
bundle_frontend(session)

out_dir = Path('dist', name)
session.run('pyproject-build', '--installer', 'uv', '--outdir', str(out_dir), source_dir)
session.run('uv', 'build', '--out-dir', str(out_dir), source_dir)

wheel_path = next(f.path for f in os.scandir(out_dir) if f.name.endswith('.whl'))
(wheel_metadata,) = Distribution.discover(name=name, path=[wheel_path])
Expand All @@ -190,7 +201,7 @@ def build_dists(session: nox.Session):
def publish_dists(session: nox.Session):
"Validate and upload dists to PyPI."

session.install(*_DEPENDENCY_GROUPS['publish'])
session.install(*_dependency_groups['publish-dists'])
session.run('twine', 'check', '--strict', 'dist/instawow/*')
session.run('twine', 'upload', '--verbose', 'dist/instawow/*')

Expand Down Expand Up @@ -264,7 +275,7 @@ def freeze_gui(session: nox.Session):

packages = _locate_or_build_packages(session)

spec_path = _ROOT / 'instawow-gui' / 'pyproject.toml'
spec_path = _root / 'instawow-gui' / 'pyproject.toml'
spec = spec_path.read_text(encoding='utf-8')
spec = spec.replace(
'"instawow-gui[full]"',
Expand All @@ -289,7 +300,7 @@ def freeze_gui(session: nox.Session):
build_opts = []
package_opts = []

session.install('briefcase')
session.install(*_dependency_groups['freeze-gui'])

with session.chdir('instawow-gui'):
session.run('briefcase', 'build', *build_opts)
Expand Down
17 changes: 17 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ packages = [
[tool.hatch.version]
source = "vcs"

[dependency-groups]
format = [
"ruff",
]
freeze-gui = [
"briefcase",
]
lint = [
{ include-group = "format" },
]
publish-dists = [
"twine",
]
report-coverage = [
"coverage[toml]",
]

[tool.ruff]
line-length = 99

Expand Down

0 comments on commit d303c19

Please sign in to comment.