Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Added
to pants' use of PEX lockfiles. This is not a user-facing addition.
#6118 #6141 #6133 #6120 #6181 #6183 #6200 #6237 #6229 #6240 #6241 #6244 #6251 #6253
#6254 #6258 #6259 #6260 #6269 #6275 #6279 #6278 #6282 #6283 #6273 #6287 #6306 #6307
#6311 #6314 #6315 #6317 #6319 #6312
#6311 #6314 #6315 #6317 #6319 #6312 #6321
Contributed by @cognifloyd
* Build of ST2 EL9 packages #6153
Contributed by @amanda11
Expand Down
96 changes: 81 additions & 15 deletions pants-plugins/release/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@
from __future__ import annotations

import re
from dataclasses import dataclass

from pants.backend.nfpm.fields.version import NfpmVersionField, NfpmVersionSchemaField
from pants.backend.nfpm.util_rules.inject_config import (
InjectedNfpmPackageFields,
InjectNfpmPackageFieldsRequest,
)
from pants.backend.python.util_rules.package_dists import (
SetupKwargs,
SetupKwargsRequest,
)
from pants.engine.fs import DigestContents, GlobMatchErrorBehavior, PathGlobs
from pants.engine.internals.native_engine import Field
from pants.engine.target import Target
from pants.engine.rules import collect_rules, Get, MultiGet, rule, UnionRule
from pants.util.frozendict import FrozenDict
Expand Down Expand Up @@ -88,6 +95,40 @@ def is_applicable(cls, _: Target) -> bool:
# return target.address.spec.startswith("st2")


@dataclass(frozen=True)
class StackStormVersionRequest:
version_file: str
description_of_origin: str


@dataclass(frozen=True)
class StackStormVersion:
value: str


@rule
async def extract_version(request: StackStormVersionRequest) -> StackStormVersion:
version_digest_contents = await Get(
DigestContents,
PathGlobs(
[request.version_file],
description_of_origin=request.description_of_origin,
glob_match_error_behavior=GlobMatchErrorBehavior.error,
),
)

version_file_contents = version_digest_contents[0].content.decode()
version_match = re.search(
r"^__version__ = ['\"]([^'\"]*)['\"]", version_file_contents, re.M
)
if not version_match:
raise ValueError(
f"Could not find the __version__ in {request.version_file}\n{version_file_contents}"
)

return StackStormVersion(version_match.group(1))


@rule
async def setup_kwargs_plugin(request: StackStormSetupKwargsRequest) -> SetupKwargs:
kwargs = request.explicit_kwargs.copy()
Expand All @@ -100,13 +141,12 @@ async def setup_kwargs_plugin(request: StackStormSetupKwargsRequest) -> SetupKwa

version_file = kwargs.pop("version_file")

version_digest_contents, readme_digest_contents = await MultiGet(
version, readme_digest_contents = await MultiGet(
Get(
DigestContents,
PathGlobs(
[f"{request.target.address.spec_path}/{version_file}"],
StackStormVersion,
StackStormVersionRequest(
version_file=f"{request.target.address.spec_path}/{version_file}",
description_of_origin=f"StackStorm version file: {version_file}",
glob_match_error_behavior=GlobMatchErrorBehavior.error,
),
),
Get(
Expand All @@ -118,19 +158,10 @@ async def setup_kwargs_plugin(request: StackStormSetupKwargsRequest) -> SetupKwa
),
)

version_file_contents = version_digest_contents[0].content.decode()
version_match = re.search(
r"^__version__ = ['\"]([^'\"]*)['\"]", version_file_contents, re.M
)
if not version_match:
raise ValueError(
f"Could not find the __version__ in {request.target.address.spec_path}/{version_file}\n{version_file_contents}"
)

# Hardcode certain kwargs and validate that they weren't already set.
hardcoded_kwargs = PROJECT_METADATA.copy()
hardcoded_kwargs["project_urls"] = FrozenDict(PROJECT_URLS)
hardcoded_kwargs["version"] = version_match.group(1)
hardcoded_kwargs["version"] = version.value

long_description = (
readme_digest_contents[0].content.decode() if readme_digest_contents else ""
Expand Down Expand Up @@ -162,8 +193,43 @@ async def setup_kwargs_plugin(request: StackStormSetupKwargsRequest) -> SetupKwa
return SetupKwargs(kwargs, address=request.target.address)


class StackStormNfpmPackageFieldsRequest(InjectNfpmPackageFieldsRequest):
@classmethod
def is_applicable(cls, _: Target) -> bool:
return True


@rule
async def inject_package_fields(
request: StackStormNfpmPackageFieldsRequest,
) -> InjectedNfpmPackageFields:
address = request.target.address

version_file = "st2common/st2common/__init__.py"
extracted_version = await Get(
StackStormVersion,
StackStormVersionRequest(
version_file=version_file,
description_of_origin=f"StackStorm version file: {version_file}",
),
)

version: str = extracted_version.value
if version.endswith("dev") and version[-4] != "-":
# nfpm parses this into version[-version_prerelease][+version_metadata]
# that dash is required to be a valid semver version.
version = version.replace("dev", "-dev")

fields: list[Field] = [
NfpmVersionSchemaField("semver", address=address),
NfpmVersionField(version, address=address),
]
return InjectedNfpmPackageFields(fields, address=address)


def rules():
return [
*collect_rules(),
UnionRule(SetupKwargsRequest, StackStormSetupKwargsRequest),
UnionRule(InjectNfpmPackageFieldsRequest, StackStormNfpmPackageFieldsRequest),
]
3 changes: 3 additions & 0 deletions pants.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ backend_packages = [
# packaging
"pants.backend.experimental.makeself",

# packaging
"pants.backend.experimental.nfpm",

# internal plugins in pants-plugins/
"pants.backend.plugin_development",
"api_spec",
Expand Down