forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_python_binary.py
99 lines (87 loc) · 3.46 KB
/
create_python_binary.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from dataclasses import dataclass
from typing import Tuple
from pants.backend.python.rules.pex import PexPlatforms, TwoStepPex
from pants.backend.python.rules.pex_from_targets import (
PexFromTargetsRequest,
TwoStepPexFromTargetsRequest,
)
from pants.backend.python.target_types import (
PexAlwaysWriteCache,
PexEmitWarnings,
PexIgnoreErrors,
PexInheritPath,
PexShebang,
PexZipSafe,
PythonBinaryDefaults,
PythonBinarySources,
PythonEntryPoint,
)
from pants.backend.python.target_types import PythonPlatforms as PythonPlatformsField
from pants.core.goals.binary import BinaryFieldSet, CreatedBinary
from pants.core.util_rules.determine_source_files import AllSourceFilesRequest, SourceFiles
from pants.engine.addresses import Addresses
from pants.engine.rules import SubsystemRule, rule
from pants.engine.selectors import Get
from pants.engine.unions import UnionRule
@dataclass(frozen=True)
class PythonBinaryFieldSet(BinaryFieldSet):
required_fields = (PythonEntryPoint, PythonBinarySources)
sources: PythonBinarySources
entry_point: PythonEntryPoint
always_write_cache: PexAlwaysWriteCache
emit_warnings: PexEmitWarnings
ignore_errors: PexIgnoreErrors
inherit_path: PexInheritPath
shebang: PexShebang
zip_safe: PexZipSafe
platforms: PythonPlatformsField
def generate_additional_args(
self, python_binary_defaults: PythonBinaryDefaults
) -> Tuple[str, ...]:
args = []
if self.always_write_cache.value is True:
args.append("--always-write-cache")
if self.emit_warnings.value_or_global_default(python_binary_defaults) is False:
args.append("--no-emit-warnings")
if self.ignore_errors.value is True:
args.append("--ignore-errors")
if self.inherit_path.value is not None:
args.append(f"--inherit-path={self.inherit_path.value}")
if self.shebang.value is not None:
args.append(f"--python-shebang={self.shebang.value}")
if self.zip_safe.value is False:
args.append("--not-zip-safe")
return tuple(args)
@rule
async def create_python_binary(
field_set: PythonBinaryFieldSet, python_binary_defaults: PythonBinaryDefaults
) -> CreatedBinary:
entry_point = field_set.entry_point.value
if entry_point is None:
source_files = await Get(
SourceFiles, AllSourceFilesRequest([field_set.sources], strip_source_roots=True)
)
entry_point = PythonBinarySources.translate_source_file_to_entry_point(source_files.files)
output_filename = f"{field_set.address.target_name}.pex"
two_step_pex = await Get(
TwoStepPex,
TwoStepPexFromTargetsRequest(
PexFromTargetsRequest(
addresses=Addresses([field_set.address]),
entry_point=entry_point,
platforms=PexPlatforms.create_from_platforms_field(field_set.platforms),
output_filename=output_filename,
additional_args=field_set.generate_additional_args(python_binary_defaults),
)
),
)
pex = two_step_pex.pex
return CreatedBinary(digest=pex.digest, binary_name=pex.output_filename)
def rules():
return [
create_python_binary,
UnionRule(BinaryFieldSet, PythonBinaryFieldSet),
SubsystemRule(PythonBinaryDefaults),
]