-
-
Notifications
You must be signed in to change notification settings - Fork 606
/
awslambda_common_rules.py
82 lines (65 loc) · 2.43 KB
/
awslambda_common_rules.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from abc import ABCMeta
from dataclasses import dataclass
from textwrap import dedent
from pants.core.util_rules.distdir import DistDir
from pants.engine.console import Console
from pants.engine.fs import Digest, MergeDigests, Workspace
from pants.engine.goal import Goal, GoalSubsystem, LineOriented
from pants.engine.rules import goal_rule, register_rules
from pants.engine.selectors import Get, MultiGet
from pants.engine.target import FieldSet, TargetsToValidFieldSets, TargetsToValidFieldSetsRequest
from pants.engine.unions import union
class AWSLambdaError(Exception):
pass
@dataclass(frozen=True)
class CreatedAWSLambda:
digest: Digest
zip_file_relpath: str
runtime: str
handler: str
@union
class AWSLambdaFieldSet(FieldSet, metaclass=ABCMeta):
"""The fields necessary to create an AWS Lambda from a target."""
class AWSLambdaSubsystem(LineOriented, GoalSubsystem):
"""Generate an AWS Lambda."""
name = "awslambda"
class AWSLambdaGoal(Goal):
subsystem_cls = AWSLambdaSubsystem
@goal_rule
async def create_awslambda(
console: Console,
awslambda_subsystem: AWSLambdaSubsystem,
distdir: DistDir,
workspace: Workspace,
) -> AWSLambdaGoal:
targets_to_valid_field_sets = await Get(
TargetsToValidFieldSets,
TargetsToValidFieldSetsRequest(
AWSLambdaFieldSet,
goal_description="the `awslambda` goal",
error_if_no_valid_targets=True,
),
)
awslambdas = await MultiGet(
Get(CreatedAWSLambda, AWSLambdaFieldSet, field_set)
for field_set in targets_to_valid_field_sets.field_sets
)
merged_digest = await Get(Digest, MergeDigests(awslambda.digest for awslambda in awslambdas))
workspace.write_digest(merged_digest, path_prefix=str(distdir.relpath))
with awslambda_subsystem.line_oriented(console) as print_stdout:
for awslambda in awslambdas:
output_path = distdir.relpath / awslambda.zip_file_relpath
print_stdout(
dedent(
f"""\
Wrote code bundle to {output_path}
Runtime: {awslambda.runtime}
Handler: {awslambda.handler}
"""
)
)
return AWSLambdaGoal(exit_code=0)
def rules():
return register_rules()