|
| 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