Skip to content

Commit

Permalink
adress changes
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Sep 3, 2024
1 parent f31adee commit 8ef0c60
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 25 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ python.toolchain(
tools = use_extension("//py:extensions.bzl", "py_tools")
tools.rules_py_tools()
use_repo(tools, "rules_py_tools")
use_repo(tools, "pex_2_3_1")
use_repo(tools, "rules_py_pex_2_3_1")

register_toolchains(
"@rules_py_tools//:all",
Expand Down
4 changes: 0 additions & 4 deletions examples/py_pex_binary/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
load("//py:defs.bzl", "py_binary", "py_pex_binary")


py_binary(
name = "binary",
srcs = ["say.py"],
Expand All @@ -14,13 +13,10 @@ py_binary(
],
)



py_pex_binary(
name = "py_pex_binary",
binary = ":binary",
inject_env = {
"TEST": "1"
}
)

38 changes: 26 additions & 12 deletions py/private/py_pex_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,41 @@ exclude_paths = [
"rules_python~~python~",
"aspect_rules_py~/py/tools/"
]

# determines if the given file is a `distinfo`, `dep` or a `source`
# this required to allow PEX to put files into different places.
#
# --dep: into `<PEX_UNPACK_ROOT>/.deps/<name_of_the_package>`
# --distinfo: is only used for determining package metadata
# --source: into `<PEX_UNPACK_ROOT>/<relative_path_to_workspace_root>/<file_name>`
def _map_srcs(f, workspace):
dest_path = _runfiles_path(f, workspace)

# TODO: better way to exclude hermetic toolchain.
# We exclude files from hermetic python toolchain.
for exclude in exclude_paths:
if dest_path.find(exclude) != -1:
return []


site_packages_i = f.path.find("site-packages")

# determine if the src if a third party.

# if path contains `site-packages` and there is only two path segments
# after it, it will be treated as third party dep.
# Here are some examples of path we expect and use and ones we ignore.
#
# Match: `external/rules_python~~pip~pypi_39_rtoml/site-packages/rtoml-0.11.0.dist-info/INSTALLER`
# Reason: It has two `/` after first `site-packages` substring.
#
# No Match: `external/rules_python~~pip~pypi_39_rtoml/site-packages/rtoml-0.11.0/src/mod/parse.py`
# Reason: It has three `/` after first `site-packages` substring.
if site_packages_i != -1 and f.path.count("/", site_packages_i) == 2:
if f.path.find("dist-info", site_packages_i) != -1:
return ["--distinfo", f.dirname]
return ["--dep", f.dirname]
return ["--distinfo={}".format(f.dirname)]
return ["--dep={}".format(f.dirname)]

# If the path does not have a `site-packages` in it, then put it into
# the standard runfiles tree.
elif site_packages_i == -1:
return ["--source=%s=%s" % (f.path, dest_path)]
return ["--source={}={}".format(f.path, dest_path)]

return []

Expand All @@ -56,6 +72,7 @@ def _py_python_pex_impl(ctx):
ctx.attr.inject_env.items(),
map_each = lambda e: "--inject-env=%s=%s" % (e[0], e[1]),
allow_closure = True,
# this is needed to allow passing a lambda to map_each
)

args.add_all(
Expand All @@ -67,6 +84,7 @@ def _py_python_pex_impl(ctx):
runfiles.files,
map_each = lambda f: _map_srcs(f, workspace_name),
uniquify = True,
# this is needed to allow passing a lambda (with workspace_name) to map_each
allow_closure = True,
)
args.add(binary[DefaultInfo].files_to_run.executable, format = "--executable=%s")
Expand All @@ -90,10 +108,6 @@ def _py_python_pex_impl(ctx):
outputs = [output],
mnemonic = "PyPex",
progress_message = "Building PEX binary %{label}",
# Unfortunately there is no way to disable pex cache, so just set it to . allow
# bazel to discard cache once the action is done.
# TODO: this is probably not the right thing to do if the action is unsandboxed.
env = {"PEX_ROOT": "."}
)

return [
Expand All @@ -113,7 +127,7 @@ _attrs = dict({
),
"python_shebang": attr.string(default = "#!/usr/bin/env python3"),
"python_interpreter_constraints": attr.string_list(
default = [],
default = ["CPython=={major}.{minor}.*"],
doc = """\
Python interpreter versions this PEX binary is compatible with. A list of semver strings.
The placeholder strings `{major}`, `{minor}`, `{patch}` can be used for gathering version
Expand Down
2 changes: 1 addition & 1 deletion py/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def rules_py_toolchains(name = DEFAULT_TOOLS_REPOSITORY, register = True, is_pre
# The url and digest information can be found at https://pypi.org/pypi/pex/json
# WARNING: when updated, also update MODULE.bazel
http_file(
name = "pex_2_3_1",
name = "rules_py_pex_2_3_1",
urls = ["https://files.pythonhosted.org/packages/e7/d0/fbda2a4d41d62d86ce53f5ae4fbaaee8c34070f75bb7ca009090510ae874/pex-2.3.1-py2.py3-none-any.whl"],
sha256 = "64692a5bf6f298403aab930d22f0d836ae4736c5bc820e262e9092fe8c56f830",
downloaded_file_path = "pex-2.3.1-py2.py3-none-any.whl",
Expand Down
2 changes: 1 addition & 1 deletion py/tools/pex/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load("//py:defs.bzl", "py_binary", "py_unpacked_wheel")

py_unpacked_wheel(
name = "pex_unpacked",
src = "@pex_2_3_1//file",
src = "@rules_py_pex_2_3_1//file",
py_package_name = "pex"
)

Expand Down
19 changes: 13 additions & 6 deletions py/tools/pex/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Unfortunately there is no way to stop pex from writing to a PEX_ROOT during build.
# Closest thing seems to be creating a tmp folder and deleting it after.
# pex cli does the same here;
# https://github.com/pex-tool/pex/blob/252459bdd879fc1e3446a6221571875d46fad1bd/pex/commands/command.py#L362-L382
import os
from pex.common import safe_mkdtemp, safe_rmtree
TMP_PEX_ROOT=safe_mkdtemp()
os.environ["PEX_ROOT"] = TMP_PEX_ROOT

import sys
from pex.common import Chroot
from pex.pex_builder import Check, CopyMode, PEXBuilder
from pex.pex_builder import Check,PEXBuilder
from pex.interpreter import PythonInterpreter
from pex.interpreter_constraints import InterpreterConstraint
from pex.layout import Layout
Expand Down Expand Up @@ -150,7 +158,6 @@ def __call__(self, parser, namespace, value, option_str=None):
for constraint in options.constraints
]

print(pex_info.interpreter_constraints)
for dep in options.dependencies:
dist = Distribution.load(dep + "/../")

Expand All @@ -160,7 +167,6 @@ def __call__(self, parser, namespace, value, option_str=None):
path= dist.location,
dist_name = key
)
print(dist.location, dist.key)
pex_info.add_distribution(key, dist_hash)
pex_builder.add_requirement(dist.as_requirement())

Expand All @@ -183,12 +189,13 @@ def __call__(self, parser, namespace, value, option_str=None):

pex_builder.freeze(bytecode_compile=False)

print(options.dependencies)

pex_builder.build(
options.pex_name,
deterministic_timestamp=True,
layout=Layout.ZIPAPP,
check=Check.WARN,
)


# Cleanup temporary pex root
safe_rmtree(TMP_PEX_ROOT)

0 comments on commit 8ef0c60

Please sign in to comment.