21
21
from __future__ import annotations
22
22
23
23
import re
24
+ from dataclasses import dataclass
24
25
26
+ from pants .backend .nfpm .fields .version import NfpmVersionField , NfpmVersionSchemaField
27
+ from pants .backend .nfpm .util_rules .inject_config import (
28
+ InjectedNfpmPackageFields ,
29
+ InjectNfpmPackageFieldsRequest ,
30
+ )
25
31
from pants .backend .python .util_rules .package_dists import (
26
32
SetupKwargs ,
27
33
SetupKwargsRequest ,
28
34
)
29
35
from pants .engine .fs import DigestContents , GlobMatchErrorBehavior , PathGlobs
36
+ from pants .engine .internals .native_engine import Field
30
37
from pants .engine .target import Target
31
38
from pants .engine .rules import collect_rules , Get , MultiGet , rule , UnionRule
32
39
from pants .util .frozendict import FrozenDict
@@ -88,6 +95,40 @@ def is_applicable(cls, _: Target) -> bool:
88
95
# return target.address.spec.startswith("st2")
89
96
90
97
98
+ @dataclass (frozen = True )
99
+ class StackStormVersionRequest :
100
+ version_file : str
101
+ description_of_origin : str
102
+
103
+
104
+ @dataclass (frozen = True )
105
+ class StackStormVersion :
106
+ value : str
107
+
108
+
109
+ @rule
110
+ async def extract_version (request : StackStormVersionRequest ) -> StackStormVersion :
111
+ version_digest_contents = await Get (
112
+ DigestContents ,
113
+ PathGlobs (
114
+ [request .version_file ],
115
+ description_of_origin = request .description_of_origin ,
116
+ glob_match_error_behavior = GlobMatchErrorBehavior .error ,
117
+ ),
118
+ )
119
+
120
+ version_file_contents = version_digest_contents [0 ].content .decode ()
121
+ version_match = re .search (
122
+ r"^__version__ = ['\"]([^'\"]*)['\"]" , version_file_contents , re .M
123
+ )
124
+ if not version_match :
125
+ raise ValueError (
126
+ f"Could not find the __version__ in { request .version_file } \n { version_file_contents } "
127
+ )
128
+
129
+ return StackStormVersion (version_match .group (1 ))
130
+
131
+
91
132
@rule
92
133
async def setup_kwargs_plugin (request : StackStormSetupKwargsRequest ) -> SetupKwargs :
93
134
kwargs = request .explicit_kwargs .copy ()
@@ -100,13 +141,12 @@ async def setup_kwargs_plugin(request: StackStormSetupKwargsRequest) -> SetupKwa
100
141
101
142
version_file = kwargs .pop ("version_file" )
102
143
103
- version_digest_contents , readme_digest_contents = await MultiGet (
144
+ version , readme_digest_contents = await MultiGet (
104
145
Get (
105
- DigestContents ,
106
- PathGlobs (
107
- [ f"{ request .target .address .spec_path } /{ version_file } " ] ,
146
+ StackStormVersion ,
147
+ StackStormVersionRequest (
148
+ version_file = f"{ request .target .address .spec_path } /{ version_file } " ,
108
149
description_of_origin = f"StackStorm version file: { version_file } " ,
109
- glob_match_error_behavior = GlobMatchErrorBehavior .error ,
110
150
),
111
151
),
112
152
Get (
@@ -118,19 +158,10 @@ async def setup_kwargs_plugin(request: StackStormSetupKwargsRequest) -> SetupKwa
118
158
),
119
159
)
120
160
121
- version_file_contents = version_digest_contents [0 ].content .decode ()
122
- version_match = re .search (
123
- r"^__version__ = ['\"]([^'\"]*)['\"]" , version_file_contents , re .M
124
- )
125
- if not version_match :
126
- raise ValueError (
127
- f"Could not find the __version__ in { request .target .address .spec_path } /{ version_file } \n { version_file_contents } "
128
- )
129
-
130
161
# Hardcode certain kwargs and validate that they weren't already set.
131
162
hardcoded_kwargs = PROJECT_METADATA .copy ()
132
163
hardcoded_kwargs ["project_urls" ] = FrozenDict (PROJECT_URLS )
133
- hardcoded_kwargs ["version" ] = version_match . group ( 1 )
164
+ hardcoded_kwargs ["version" ] = version . value
134
165
135
166
long_description = (
136
167
readme_digest_contents [0 ].content .decode () if readme_digest_contents else ""
@@ -162,8 +193,43 @@ async def setup_kwargs_plugin(request: StackStormSetupKwargsRequest) -> SetupKwa
162
193
return SetupKwargs (kwargs , address = request .target .address )
163
194
164
195
196
+ class StackStormNfpmPackageFieldsRequest (InjectNfpmPackageFieldsRequest ):
197
+ @classmethod
198
+ def is_applicable (cls , _ : Target ) -> bool :
199
+ return True
200
+
201
+
202
+ @rule
203
+ async def inject_package_fields (
204
+ request : StackStormNfpmPackageFieldsRequest ,
205
+ ) -> InjectedNfpmPackageFields :
206
+ address = request .target .address
207
+
208
+ version_file = "st2common/st2common/__init__.py"
209
+ extracted_version = await Get (
210
+ StackStormVersion ,
211
+ StackStormVersionRequest (
212
+ version_file = version_file ,
213
+ description_of_origin = f"StackStorm version file: { version_file } " ,
214
+ ),
215
+ )
216
+
217
+ version : str = extracted_version .value
218
+ if version .endswith ("dev" ) and version [- 4 ] != "-" :
219
+ # nfpm parses this into version[-version_prerelease][+version_metadata]
220
+ # that dash is required to be a valid semver version.
221
+ version = version .replace ("dev" , "-dev" )
222
+
223
+ fields : list [Field ] = [
224
+ NfpmVersionSchemaField ("semver" , address = address ),
225
+ NfpmVersionField (version , address = address ),
226
+ ]
227
+ return InjectedNfpmPackageFields (fields , address = address )
228
+
229
+
165
230
def rules ():
166
231
return [
167
232
* collect_rules (),
168
233
UnionRule (SetupKwargsRequest , StackStormSetupKwargsRequest ),
234
+ UnionRule (InjectNfpmPackageFieldsRequest , StackStormNfpmPackageFieldsRequest ),
169
235
]
0 commit comments