Skip to content

Commit

Permalink
Create template configuration files (#15)
Browse files Browse the repository at this point in the history
Also:
- Add timing debug logs
- Improve consistency of logging in general
  • Loading branch information
zanieb authored Dec 14, 2023
1 parent 8d38464 commit f3bb943
Show file tree
Hide file tree
Showing 13 changed files with 737 additions and 1,052 deletions.
58 changes: 43 additions & 15 deletions src/packse/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil
import subprocess
import textwrap
import time
from pathlib import Path
from typing import Generator

Expand All @@ -21,7 +22,7 @@
load_scenarios,
scenario_prefix,
)
from packse.template import create_from_template
from packse.template import TemplateConfig, create_from_template, load_template_config

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -57,8 +58,9 @@ def build_scenario(scenario: Scenario, rm_destination: bool) -> str:
work_dir = Path.cwd()
build_destination = work_dir / "build" / prefix
dist_destination = work_dir / "dist" / prefix
start_time = time.time()

logging.info(
logger.info(
"Building '%s' in directory '%s'",
prefix,
build_destination.relative_to(work_dir),
Expand Down Expand Up @@ -101,6 +103,11 @@ def build_scenario(scenario: Scenario, rm_destination: bool) -> str:
dist_destination=dist_destination,
)

logger.info(
"Built scenario '%s' in %.2fs",
prefix,
time.time() - start_time,
)
return prefix


Expand Down Expand Up @@ -138,8 +145,12 @@ def build_scenario_package(
# Generate a Python module name
module_name = package_name.replace("-", "_")

template_config = load_template_config(scenario.template)

for version, package_version in package.versions.items():
logger.info("Generating project for '%s'", package_name)
start_time = time.time()

logger.debug("Generating project for '%s-%s'", package_name, version)
package_destination = create_from_template(
build_destination,
template_name=scenario.template,
Expand All @@ -156,39 +167,56 @@ def build_scenario_package(
},
)

for dist in build_package_distributions(package_version, package_destination):
logger.info(
"Generated project for '%s-%s' in %.2fms",
package_name,
version,
(time.time() - start_time) * 1000.0,
)

for dist in build_package_distributions(
template_config, package_version, package_destination
):
shared_path = dist_destination / dist.name
logger.info("Linked distribution to %s", shared_path.relative_to(work_dir))
logger.debug("Linked distribution to %s", shared_path.relative_to(work_dir))
shared_path.hardlink_to(dist)


def build_package_distributions(
package_version: PackageVersion, target: Path
template_config: TemplateConfig, package_version: PackageVersion, target: Path
) -> Generator[Path, None, None]:
"""
Build package distributions, yield each built distribution path, then delete the distribution folder.
"""
command = template_config.build_base

command = ["hatch", "build"]
if package_version.sdist:
command.extend(["-t", "sdist"])
command.extend(template_config.build_sdist)
if package_version.wheel:
command.extend(["-t", "wheel"])
command.extend(template_config.build_wheel)

start_time = time.time()

try:
output = subprocess.check_output(command, cwd=target, stderr=subprocess.STDOUT)

yield from sorted((target / "dist").iterdir())
shutil.rmtree(target / "dist")

except subprocess.CalledProcessError as exc:
raise BuildError(
f"Building {target.name} with hatch failed",
exc.output.decode(),
)
else:
logger.debug(
"Building %s:\n\n%s",
logs = (
(":\n\n" + textwrap.indent(output.decode(), " " * 4))
if logger.getEffectiveLevel() <= logging.DEBUG
else ""
)
logger.info(
"Built package '%s' in %.2fs%s",
target.name,
textwrap.indent(output.decode(), " " * 4),
time.time() - start_time,
logs,
)

yield from sorted((target / "dist").iterdir())
shutil.rmtree(target / "dist")
12 changes: 10 additions & 2 deletions src/packse/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import subprocess
import textwrap
import time
from pathlib import Path

from packse.error import (
Expand Down Expand Up @@ -44,6 +45,7 @@ def publish_package_distribution(target: Path, dry_run: bool) -> None:
print("Would execute: " + " ".join(command))
return

start_time = time.time()
try:
output = subprocess.check_output(command, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
Expand All @@ -55,8 +57,14 @@ def publish_package_distribution(target: Path, dry_run: bool) -> None:
output,
)
else:
logs = (
(":\n\n" + textwrap.indent(output.decode(), " " * 4))
if logger.getEffectiveLevel() <= logging.DEBUG
else ""
)
logger.debug(
"Publishing %s:\n\n%s",
"Published %s in %.2fs%s",
target.name,
textwrap.indent(output.decode(), " " * 4),
time.time() - start_time,
logs,
)
6 changes: 3 additions & 3 deletions src/packse/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import msgspec

from packse.template import get_template_version
from packse.template import load_template_config


class PackageVersion(msgspec.Struct):
Expand Down Expand Up @@ -108,9 +108,9 @@ def scenario_version(scenario: Scenario) -> str:
"""
Generate a unique version for a scenario based on its contents.
"""
template_version = get_template_version(scenario.template)
template_version = load_template_config(scenario.template).version
hasher = hashlib.new("md5", usedforsecurity=False)
hasher.update(template_version.encode())
hasher.update(template_version.to_bytes())
hasher.update(scenario.hash().encode())
hasher.update(os.environ.get("PACKSE_VERSION_SEED", "").encode())
return hasher.hexdigest()[:8]
Expand Down
26 changes: 17 additions & 9 deletions src/packse/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
from pathlib import Path
from typing import Any

import msgspec

from packse.templates import __templates_path__

TEMPLATE_VERSION = ".template-version"
TEMPLATE_CONFIG = "template.toml"

logger = logging.getLogger(__name__)


def get_template_version(template_name: str) -> str:
class TemplateConfig(msgspec.Struct):
version: int
build_base: list[str]
build_sdist: list[str]
build_wheel: list[str]


def load_template_config(template_name: str) -> TemplateConfig:
template_path = __templates_path__ / template_name
version_path = template_path / TEMPLATE_VERSION
if version_path.exists():
return version_path.read_text()
else:
return "0"
config = template_path / TEMPLATE_CONFIG
if not config.exists():
raise RuntimeError(f"Template {template_name} missing config file.")
return msgspec.toml.decode(config.read_text(), type=TemplateConfig)


def create_from_template(
Expand All @@ -37,15 +45,15 @@ def create_from_template(
first_root = new_root

# Create the new directory
logger.info("Creating %s", new_root.relative_to(destination))
logger.debug("Creating %s", new_root.relative_to(destination))
new_root.mkdir()

for file in files:
file_path = root / file

# Determine the new file path
new_file_path = new_root / replace_placeholders(file, variables)
logger.info("Creating %s", new_file_path.relative_to(destination))
logger.debug("Creating %s", new_file_path.relative_to(destination))

new_file_path.write_text(
replace_placeholders(file_path.read_text(), variables)
Expand Down
1 change: 0 additions & 1 deletion src/packse/templates/simple/.template-version

This file was deleted.

4 changes: 4 additions & 0 deletions src/packse/templates/simple/template.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version = 1
build_base = ["hatch", "build"]
build_sdist = ["-t", "sdist"]
build_wheel = ["-t", "wheel"]
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build]
sources = ["src"]
[tool.hatch.build.targets.wheel]
packages = ["src/{{ module-name }}"]

[tool.hatch.build.targets.sdist]
only-include = ["src/{{ module-name }}"]

[project]
name = "{{ package-name }}"
Expand Down
2 changes: 1 addition & 1 deletion src/packse/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def view(targets: list[Path]):

# Then view each one
for scenario in scenarios:
logging.debug("Viewing %s", scenario.name)
logger.debug("Viewing %s", scenario.name)
view_scenario(scenario)


Expand Down
Loading

0 comments on commit f3bb943

Please sign in to comment.