Skip to content

Commit 3e333d6

Browse files
committed
pants: stub PacksGlob target
1 parent f2d1ca7 commit 3e333d6

2 files changed

Lines changed: 103 additions & 4 deletions

File tree

pants-plugins/pack_metadata/register.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,24 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from pack_metadata import tailor
15-
from pack_metadata.target_types import PackMetadata, PackMetadataInGitSubmodule
14+
from pack_metadata import tailor, target_types_rules
15+
from pack_metadata.target_types import (
16+
PackMetadata,
17+
PackMetadataInGitSubmodule,
18+
PacksGlob,
19+
)
1620

1721

1822
def rules():
19-
return tailor.rules()
23+
return [
24+
tailor.rules(),
25+
target_types_rules.rules(),
26+
]
2027

2128

2229
def target_types():
23-
return [PackMetadata, PackMetadataInGitSubmodule]
30+
return [
31+
PackMetadata,
32+
PackMetadataInGitSubmodule,
33+
PacksGlob,
34+
]
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Copyright 2023 The StackStorm Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# repurposed from pants.backend.python.target_types_rules
15+
import dataclasses
16+
import os
17+
from dataclasses import dataclass
18+
19+
from pants.engine.addresses import Address
20+
from pants.engine.fs import GlobMatchErrorBehavior, PathGlobs, Paths
21+
from pants.engine.rules import Get, collect_rules, MultiGet, rule, UnionRule
22+
from pants.engine.target import (
23+
Dependencies,
24+
DependenciesRequest,
25+
ExplicitlyProvidedDependencies,
26+
FieldSet,
27+
InferDependenciesRequest,
28+
InferredDependencies,
29+
InvalidFieldException,
30+
)
31+
from pants.util.logging import LogLevel
32+
33+
from pack_metadata.target_types import PacksGlobDependencies
34+
35+
36+
@dataclass(frozen=True)
37+
class PacksGlobInferenceFieldSet(FieldSet):
38+
required_fields = (PacksGlobDependencies,)
39+
40+
dependencies: PacksGlobDependencies
41+
42+
43+
class InferPacksGlobDependencies(InferDependenciesRequest):
44+
infer_from = PacksGlobInferenceFieldSet
45+
46+
47+
@rule(
48+
desc="Inferring packs glob dependencies",
49+
level=LogLevel.DEBUG,
50+
)
51+
async def infer_packs_globs_dependencies(
52+
request: InferPacksGlobDependencies,
53+
) -> InferredDependencies:
54+
address = request.field_set.address
55+
56+
paths, explicitly_provided_deps = await MultiGet(
57+
Get(
58+
Paths,
59+
PathGlobs(
60+
[os.path.join(address.spec_path, "*")],
61+
glob_match_error_behavior=GlobMatchErrorBehavior.error,
62+
description_of_origin=f"{address}'s packs glob",
63+
),
64+
),
65+
Get(
66+
ExplicitlyProvidedDependencies,
67+
DependenciesRequest(request.field_set.dependencies),
68+
),
69+
)
70+
71+
# explicitly_provided_deps.includes: FrozenOrderedSet[Address]
72+
# explicitly_provided_deps.ignores: FrozenOrderedSet[Address]
73+
74+
implicit_packs = {Address(f"{pack}/") for pack in paths.dirs}
75+
76+
inferred_deps = (
77+
implicit_packs
78+
- explicitly_provided_deps.ignores
79+
- explicitly_provided_deps.includes
80+
)
81+
return InferredDependencies(inferred_deps)
82+
83+
84+
def rules():
85+
return [
86+
*collect_rules(),
87+
UnionRule(InferDependenciesRequest, InferPacksGlobDependencies),
88+
]

0 commit comments

Comments
 (0)