common-python-tasks 0.11.0__py3-none-any.whl → 0.11.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,7 @@ import os
3
3
  import re
4
4
  import secrets
5
5
  import shlex
6
+ from collections.abc import Iterator
6
7
  from pathlib import Path
7
8
 
8
9
  from . import utils
@@ -53,51 +54,47 @@ def collect_uv_index_credentials() -> list[dict[str, str | None]]:
53
54
  ]
54
55
 
55
56
 
57
+ def _iter_uv_index_secrets(
58
+ credentials: list[dict[str, str | None]],
59
+ ) -> Iterator[tuple[str, str]]:
60
+ for credential in credentials:
61
+ index_name = credential["index_name"].lower()
62
+ for credential_type in ("username", "password"):
63
+ environment_name = credential[f"{credential_type}_env"]
64
+ if environment_name:
65
+ yield f"uv_index_{index_name}_{credential_type}", environment_name
66
+
67
+
56
68
  def uv_index_secret_mounts(credentials: list[dict[str, str | None]]) -> list[str]:
57
- """Return Dockerfile ``--mount`` strings for UV index credentials.
69
+ """Return Dockerfile `--mount` strings for UV index credentials.
58
70
 
59
71
  Args:
60
72
  credentials: Output of `collect_uv_index_credentials`.
61
73
 
62
74
  Returns:
63
- A list of ``type=secret,...`` strings for Dockerfile ``RUN --mount=``
75
+ A list of `type=secret,...` strings for Dockerfile `RUN --mount=`
64
76
  directives.
65
77
  """
66
- mounts = []
67
- for cred in credentials:
68
- name = cred["index_name"].lower()
69
- if cred["username_env"]:
70
- mounts.append(
71
- f"type=secret,id=uv_index_{name}_username,env={cred['username_env']}"
72
- )
73
- if cred["password_env"]:
74
- mounts.append(
75
- f"type=secret,id=uv_index_{name}_password,env={cred['password_env']}"
76
- )
77
- return mounts
78
+ return [
79
+ f"type=secret,id={secret_id},env={environment_name}"
80
+ for secret_id, environment_name in _iter_uv_index_secrets(credentials)
81
+ ]
78
82
 
79
83
 
80
84
  def uv_index_secret_build_args(credentials: list[dict[str, str | None]]) -> list[str]:
81
- """Return ``docker build --secret`` arg pairs for UV index credentials.
85
+ """Return `docker build --secret` arg pairs for UV index credentials.
82
86
 
83
87
  Args:
84
88
  credentials: Output of `collect_uv_index_credentials`.
85
89
 
86
90
  Returns:
87
- A flat list of ``--secret`` flag-value pairs for the docker build command.
91
+ A flat list of `--secret` flag-value pairs for the docker build command.
88
92
  """
89
- args = []
90
- for cred in credentials:
91
- name = cred["index_name"].lower()
92
- if cred["username_env"]:
93
- args.extend(
94
- ["--secret", f"id=uv_index_{name}_username,env={cred['username_env']}"]
95
- )
96
- if cred["password_env"]:
97
- args.extend(
98
- ["--secret", f"id=uv_index_{name}_password,env={cred['password_env']}"]
99
- )
100
- return args
93
+ return [
94
+ item
95
+ for secret_id, environment_name in _iter_uv_index_secrets(credentials)
96
+ for item in ("--secret", f"id={secret_id},env={environment_name}")
97
+ ]
101
98
 
102
99
 
103
100
  def env_truthy(env_var: str) -> bool:
@@ -330,6 +327,28 @@ def resolve_container_docker_build_args(
330
327
  utils.fatal(f"Invalid CONTAINER_DOCKER_BUILD_ARGS: {error}")
331
328
 
332
329
 
330
+ def _resolve_optional_file_path(
331
+ cli_path: str | None,
332
+ environment_path: str | None,
333
+ description: str,
334
+ ) -> Path | None:
335
+ resolved_path = next(
336
+ (
337
+ Path(value.strip())
338
+ for value in (cli_path, environment_path)
339
+ if value and value.strip()
340
+ ),
341
+ None,
342
+ )
343
+ if resolved_path is None:
344
+ return None
345
+ if not resolved_path.exists():
346
+ utils.fatal(f"{description} not found: {resolved_path}")
347
+ if not resolved_path.is_file():
348
+ utils.fatal(f"{description} is not a file: {resolved_path}")
349
+ return resolved_path
350
+
351
+
333
352
  def resolve_container_dockerfile_hook_path(
334
353
  cli_hook_path: str | None, env_hook_path: str | None
335
354
  ) -> Path | None:
@@ -342,17 +361,13 @@ def resolve_container_dockerfile_hook_path(
342
361
  Returns:
343
362
  The validated hook path, or `None` when unset.
344
363
  """
345
- raw_value = (
346
- cli_hook_path if cli_hook_path and cli_hook_path.strip() else env_hook_path
364
+ hook_path = _resolve_optional_file_path(
365
+ cli_hook_path,
366
+ env_hook_path,
367
+ "Dockerfile hook script",
347
368
  )
348
- if raw_value is None or not raw_value.strip():
369
+ if hook_path is None:
349
370
  return None
350
-
351
- hook_path = Path(raw_value.strip())
352
- if not hook_path.exists():
353
- utils.fatal(f"Dockerfile hook script not found: {hook_path}")
354
- if not hook_path.is_file():
355
- utils.fatal(f"Dockerfile hook path is not a file: {hook_path}")
356
371
  if not os.access(hook_path, os.X_OK):
357
372
  utils.fatal(f"Dockerfile hook script must be executable: {hook_path}")
358
373
 
@@ -594,6 +609,28 @@ def get_container_deps_move_script() -> str | None:
594
609
  return inline_script if inline_script else None
595
610
 
596
611
 
612
+ def resolve_container_dockerfile_path(
613
+ dockerfile_path: str | None,
614
+ environment_dockerfile_path: str | None,
615
+ ) -> Path | None:
616
+ """Resolve and validate a project-owned application Dockerfile.
617
+
618
+ Args:
619
+ dockerfile_path: Dockerfile path supplied directly to a task.
620
+ environment_dockerfile_path: Dockerfile path supplied through the
621
+ environment.
622
+
623
+ Returns:
624
+ The configured Dockerfile path, or `None` when the bundled template
625
+ should be used.
626
+ """
627
+ return _resolve_optional_file_path(
628
+ dockerfile_path,
629
+ environment_dockerfile_path,
630
+ "Container Dockerfile",
631
+ )
632
+
633
+
597
634
  def get_prune_keep() -> int:
598
635
  """Return the integer value of CONTAINER_PRUNE_KEEP.
599
636
 
@@ -70,6 +70,28 @@ class _ContainerBuild(NamedTuple):
70
70
  commit_tag: str
71
71
 
72
72
 
73
+ def _prune_container_images(version_tag: str, commit_tag: str) -> None:
74
+ from .docker import prune_images_keep
75
+ from .env import get_prune_keep
76
+ from .utils import get_full_image_name, get_package_name
77
+
78
+ keep = get_prune_keep()
79
+ if keep < 0:
80
+ return
81
+
82
+ LOGGER.debug(
83
+ "Pruning old images; keeping %d and protecting tags: %s",
84
+ keep,
85
+ [version_tag, commit_tag],
86
+ )
87
+ prune_images_keep(
88
+ get_full_image_name(),
89
+ get_package_name(),
90
+ keep,
91
+ protect_tags=[version_tag, commit_tag],
92
+ )
93
+
94
+
73
95
  def _make_var_positional_task_args_optional(
74
96
  func: callable, resolved_task_name: str
75
97
  ) -> None:
@@ -367,11 +389,19 @@ def clean(dist_only: bool = False) -> None:
367
389
  return
368
390
  else:
369
391
  to_clean = [
370
- *[Path(p) for p in [".pytest_cache", "dist", ".mypy_cache"]],
392
+ *[
393
+ Path(p)
394
+ for p in [
395
+ ".pytest_cache",
396
+ "dist",
397
+ ".mypy_cache",
398
+ ".coverage",
399
+ "coverage.xml",
400
+ ".ruff_cache",
401
+ ]
402
+ ],
371
403
  *Path(".").rglob("__pycache__"),
372
404
  *Path(".").rglob("*.pyc"),
373
- Path(".coverage"),
374
- Path("coverage.xml"),
375
405
  ]
376
406
 
377
407
  for item in to_clean:
@@ -465,11 +495,12 @@ def build_image(
465
495
  no_cache: bool = False,
466
496
  plain: bool = False,
467
497
  single_arch: bool = False,
498
+ dockerfile_path: str | None = None,
468
499
  dockerfile_hook_path: str | None = None,
469
500
  container_env: list[str] | None = None,
470
501
  container_envfile: list[str] | None = None,
471
502
  ) -> _ContainerBuild:
472
- """Build the container image for this project using the Dockerfile template.
503
+ """Build the container image using a project-owned or bundled Dockerfile.
473
504
 
474
505
  Args:
475
506
  *docker_build_args: Additional arguments passed directly to `docker build`.
@@ -479,8 +510,10 @@ def build_image(
479
510
  no_cache: Do not use cache when building the image.
480
511
  plain: Do not pretty-print output.
481
512
  single_arch: Build images for a single architecture.
513
+ dockerfile_path: Optional project-owned Dockerfile. Overrides
514
+ CONTAINER_DOCKERFILE_PATH if provided.
482
515
  dockerfile_hook_path: Optional executable script path that can mutate
483
- the generated Dockerfile before build. Overrides
516
+ the selected Dockerfile before build. Overrides
484
517
  CONTAINER_DOCKERFILE_HOOK_PATH if provided.
485
518
  container_env: Builder and runtime environment declarations as repeated
486
519
  KEY=VALUE values.
@@ -494,14 +527,10 @@ def build_image(
494
527
  The generated Dockerfile text and image tags.
495
528
  """
496
529
  from .docker import build_image as _build_image
497
- from .docker import (
498
- prune_images_keep,
499
- )
500
530
  from .env import (
501
531
  collect_uv_index_credentials,
502
532
  get_cache_id_suffix,
503
533
  get_container_deps_move_script,
504
- get_prune_keep,
505
534
  get_python_variant,
506
535
  inject_auto_build_args_from_env,
507
536
  load_container_env_tokens,
@@ -511,6 +540,7 @@ def build_image(
511
540
  render_container_deps_move_script,
512
541
  resolve_container_docker_build_args,
513
542
  resolve_container_dockerfile_hook_path,
543
+ resolve_container_dockerfile_path,
514
544
  resolve_extension_content,
515
545
  uv_index_secret_build_args,
516
546
  uv_index_secret_mounts,
@@ -521,14 +551,17 @@ def build_image(
521
551
  )
522
552
  from .utils import (
523
553
  fatal,
524
- get_full_image_name,
525
- get_package_name,
526
554
  load_data_file,
527
555
  render_template_text,
528
556
  )
529
557
 
558
+ resolved_dockerfile_path = resolve_container_dockerfile_path(
559
+ dockerfile_path,
560
+ os.getenv("CONTAINER_DOCKERFILE_PATH"),
561
+ )
562
+
530
563
  # Determine extensions up-front so we can log a single, accurate message
531
- extensions = parse_container_extensions()
564
+ extensions = [] if resolved_dockerfile_path else parse_container_extensions()
532
565
  extension_ids = [desc.get("id") for desc in extensions if desc.get("id")]
533
566
  if extension_ids:
534
567
  LOGGER.info("Building image (with extensions: %s)", ", ".join(extension_ids))
@@ -556,6 +589,29 @@ def build_image(
556
589
  dockerfile_hook_path,
557
590
  os.getenv("CONTAINER_DOCKERFILE_HOOK_PATH"),
558
591
  )
592
+ top_level_build_args = inject_auto_build_args_from_env(
593
+ {"PYTHON_VARIANT": get_python_variant()}
594
+ )
595
+
596
+ if resolved_dockerfile_path:
597
+ version_tag, commit_tag = _build_image(
598
+ dockerfile_path=resolved_dockerfile_path,
599
+ context_path=Path("."),
600
+ debug=debug,
601
+ no_cache=no_cache,
602
+ plain=plain,
603
+ single_arch=single_arch,
604
+ omit_target=not debug,
605
+ extra_build_args=top_level_build_args or None,
606
+ docker_build_args=resolved_docker_build_args,
607
+ dockerfile_hook_path=resolved_dockerfile_hook_path,
608
+ )
609
+ _prune_container_images(version_tag, commit_tag)
610
+ return _ContainerBuild(
611
+ dockerfile_text=resolved_dockerfile_path.read_text(encoding="utf-8"),
612
+ version_tag=version_tag,
613
+ commit_tag=commit_tag,
614
+ )
559
615
 
560
616
  has_debug_deps = has_debug_dependency_group()
561
617
  if debug and not has_debug_deps:
@@ -579,9 +635,6 @@ def build_image(
579
635
  "Injecting builder and runtime env vars: %s",
580
636
  ", ".join(container_env_vars),
581
637
  )
582
- top_level_build_args = inject_auto_build_args_from_env(
583
- {"PYTHON_VARIANT": get_python_variant()}
584
- )
585
638
  external_deps_image = _get_container_build_setting("CONTAINER_DEPS_IMAGE")
586
639
  if external_deps_image:
587
640
  top_level_build_args["CONTAINER_DEPS_IMAGE"] = external_deps_image
@@ -691,16 +744,7 @@ def build_image(
691
744
  dockerfile_hook_path=resolved_dockerfile_hook_path,
692
745
  )
693
746
 
694
- keep = get_prune_keep()
695
- if keep >= 0:
696
- # Protect the tags created by this build
697
- protect = [t for t in (version_tag, commit_tag) if t is not None]
698
- LOGGER.debug(
699
- "Pruning old images; keeping %d and protecting tags: %s", keep, protect
700
- )
701
- prune_images_keep(
702
- get_full_image_name(), get_package_name(), keep, protect_tags=protect
703
- )
747
+ _prune_container_images(version_tag, commit_tag)
704
748
 
705
749
  return _ContainerBuild(
706
750
  dockerfile_text=dockerfile_text,
@@ -1320,6 +1364,7 @@ def _run_release_flow(
1320
1364
  no_cache: bool = False,
1321
1365
  plain: bool = False,
1322
1366
  single_arch: bool = False,
1367
+ dockerfile_path: str | None = None,
1323
1368
  container_env: list[str] | None = None,
1324
1369
  container_envfile: list[str] | None = None,
1325
1370
  assets: list[str] | None = None,
@@ -1426,6 +1471,7 @@ def _run_release_flow(
1426
1471
  no_cache=no_cache,
1427
1472
  plain=plain,
1428
1473
  single_arch=single_arch,
1474
+ dockerfile_path=dockerfile_path,
1429
1475
  container_env=container_env,
1430
1476
  container_envfile=container_envfile,
1431
1477
  )
@@ -1444,6 +1490,7 @@ def release(
1444
1490
  no_cache: bool = False,
1445
1491
  plain: bool = False,
1446
1492
  single_arch: bool = False,
1493
+ dockerfile_path: str | None = None,
1447
1494
  container_env: list[str] | None = None,
1448
1495
  container_envfile: list[str] | None = None,
1449
1496
  assets: list[str] | None = None,
@@ -1462,6 +1509,8 @@ def release(
1462
1509
  no_cache: Do not use cache when building container images.
1463
1510
  plain: Do not pretty-print container build output.
1464
1511
  single_arch: Build container image for a single architecture.
1512
+ dockerfile_path: Optional project-owned Dockerfile used for the image
1513
+ build. Overrides CONTAINER_DOCKERFILE_PATH if provided.
1465
1514
  container_env: Inline container environment variables as repeated
1466
1515
  KEY=VALUE values.
1467
1516
  container_envfile: Repeated list of container environment files.
@@ -1480,6 +1529,7 @@ def release(
1480
1529
  no_cache=no_cache,
1481
1530
  plain=plain,
1482
1531
  single_arch=single_arch,
1532
+ dockerfile_path=dockerfile_path,
1483
1533
  container_env=container_env,
1484
1534
  container_envfile=container_envfile,
1485
1535
  assets=assets,
@@ -1532,6 +1582,7 @@ def build(
1532
1582
  no_cache: bool = False,
1533
1583
  plain: bool = False,
1534
1584
  single_arch: bool = False,
1585
+ dockerfile_path: str | None = None,
1535
1586
  container_env: list[str] | None = None,
1536
1587
  container_envfile: list[str] | None = None,
1537
1588
  ) -> None:
@@ -1544,6 +1595,8 @@ def build(
1544
1595
  no_cache: Pass `--no-cache` to the Docker build command.
1545
1596
  plain: Pass `--progress plain` to the Docker build command.
1546
1597
  single_arch: Build the container for the current host architecture only.
1598
+ dockerfile_path: Optional project-owned Dockerfile used for the image
1599
+ build. Overrides CONTAINER_DOCKERFILE_PATH if provided.
1547
1600
  container_env: Inline container environment variables as repeated
1548
1601
  `KEY=VALUE` values.
1549
1602
  container_envfile: Repeated list of container environment files.
@@ -1558,6 +1611,7 @@ def build(
1558
1611
  no_cache=no_cache,
1559
1612
  plain=plain,
1560
1613
  single_arch=single_arch,
1614
+ dockerfile_path=dockerfile_path,
1561
1615
  container_env=container_env,
1562
1616
  container_envfile=container_envfile,
1563
1617
  )
@@ -1572,6 +1626,7 @@ def build_with_containers(
1572
1626
  no_cache: bool = False,
1573
1627
  plain: bool = False,
1574
1628
  single_arch: bool = False,
1629
+ dockerfile_path: str | None = None,
1575
1630
  container_env: list[str] | None = None,
1576
1631
  container_envfile: list[str] | None = None,
1577
1632
  ) -> None:
@@ -1582,6 +1637,8 @@ def build_with_containers(
1582
1637
  no_cache: Do not use cache when building the image.
1583
1638
  plain: Do not pretty-print output.
1584
1639
  single_arch: Build images for a single architecture.
1640
+ dockerfile_path: Optional project-owned Dockerfile used for the image
1641
+ build. Overrides CONTAINER_DOCKERFILE_PATH if provided.
1585
1642
  container_env: Inline container environment variables as repeated KEY=VALUE
1586
1643
  values.
1587
1644
  container_envfile: Repeated list of container environment files.
@@ -1593,6 +1650,7 @@ def build_with_containers(
1593
1650
  no_cache=no_cache,
1594
1651
  plain=plain,
1595
1652
  single_arch=single_arch,
1653
+ dockerfile_path=dockerfile_path,
1596
1654
  container_env=container_env,
1597
1655
  container_envfile=container_envfile,
1598
1656
  )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: common-python-tasks
3
- Version: 0.11.0
3
+ Version: 0.11.1
4
4
  Summary: Opinionated Poe the Poet tasks for Python package development.
5
5
  Project-URL: Homepage, http://github.com/ci-sourcerer/common-python-tasks
6
6
  Project-URL: Source, http://github.com/ci-sourcerer/common-python-tasks.git
@@ -42,7 +42,7 @@ Read the [complete documentation](https://ci-sourcerer.github.io/common-python-t
42
42
  Add the package as a development dependency.
43
43
 
44
44
  ```shell
45
- uv add --dev common-python-tasks==0.11.0
45
+ uv add --dev common-python-tasks==0.11.1
46
46
  ```
47
47
 
48
48
  Expose the default task set in `pyproject.toml`.
@@ -2,11 +2,11 @@ common_python_tasks/__init__.py,sha256=tqRLgOwmMQHDb1A84XbML9sWfzMymkSPhJjoF6aCH
2
2
  common_python_tasks/__main__.py,sha256=GpgSd_toMbEaWA_cZvgsZE7jo4XpzuI-awBVm2uhVyw,3512
3
3
  common_python_tasks/docker.py,sha256=_YW-hyHWlUK-kMCvI6QTgxSfmXQQKhYLOKRBPfUmtHA,24176
4
4
  common_python_tasks/docker_compose.py,sha256=NGgMei3oCYS0RbNaQXE-2veXFylO3XtZ7lU-PTtocik,21338
5
- common_python_tasks/env.py,sha256=1YLSxsUP9lPn51_zraM3nAiN1J7aKdUHzjzDzxXLo34,19696
5
+ common_python_tasks/env.py,sha256=KxXyvQcSe_zMy4drTWPX3-R_q7u4jlwVNMDkRaTta-M,20666
6
6
  common_python_tasks/git.py,sha256=0X472w_vHdtbrHHxCmJ85NoQjjogEEDs_0SRXfxbWi0,11719
7
7
  common_python_tasks/github.py,sha256=_LVanl-z4PsdLM94L8y7tkX-AZO2n3Qy39FRY7reDhA,21589
8
8
  common_python_tasks/project.py,sha256=PQ_GQicTrSQgVDB9cJ9E6pU0UhLPom2xou1sGFscr3E,17377
9
- common_python_tasks/tasks.py,sha256=Ajqw7mnfMwfiOdTuVGvGJQAp_DQxEnqDc9e8y3VFcT8,61636
9
+ common_python_tasks/tasks.py,sha256=U6CGlLVW_E-9Hofja21bUwiiISU05GdJbTTzI7Vwv2I,63796
10
10
  common_python_tasks/utils.py,sha256=hR7w0Cb7uQ7ILyIegTQ4znWHVoWANlCgxrOnO-Rh5oE,20798
11
11
  common_python_tasks/data/dockerfile_extensions/.gitkeep,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
12
  common_python_tasks/data/fastapi/alembic.ini.j2,sha256=dOiAmZngzX6eyVXeM6BhzgQmes2zwUEdKwOgm8mxjMs,985
@@ -21,7 +21,7 @@ common_python_tasks/data/generic/Dockerfile.j2,sha256=hgLrIRU5xvS2xqw9Xm4kfkcBGH
21
21
  common_python_tasks/data/generic/cliff.toml,sha256=po1MnEGEEgrpyBz3XLWr0h7emXb6ASX63lrU2r6o240,77
22
22
  common_python_tasks/data/generic/pytest.ini,sha256=RHak5QrLzKHvUBjFmaaiOYFrd_8YBRQubXEOT0NJfco,134
23
23
  common_python_tasks/data/generic/ruff.toml,sha256=njvd-5inSGmgY865SWCT4SR9IrKlzS7JQFZmAG2UT_E,53
24
- common_python_tasks-0.11.0.dist-info/METADATA,sha256=00ugE5R39fK6NpEH2LDIaUm87vNhxC2jVTGoa_FI4aI,3307
25
- common_python_tasks-0.11.0.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
26
- common_python_tasks-0.11.0.dist-info/licenses/LICENSE,sha256=RsSweM-DyzxFPx38fQOJqHrKyeT9Fiz8rO7yox-fOsw,1070
27
- common_python_tasks-0.11.0.dist-info/RECORD,,
24
+ common_python_tasks-0.11.1.dist-info/METADATA,sha256=v1Gf_iMcyv-ZWpREpc9ji35TtQ9T8oyEmdKPga5yyak,3307
25
+ common_python_tasks-0.11.1.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
26
+ common_python_tasks-0.11.1.dist-info/licenses/LICENSE,sha256=RsSweM-DyzxFPx38fQOJqHrKyeT9Fiz8rO7yox-fOsw,1070
27
+ common_python_tasks-0.11.1.dist-info/RECORD,,