Skip to content

Commit

Permalink
feat: rename s/host/system/g and use host interpret from rules_python
Browse files Browse the repository at this point in the history
  • Loading branch information
oxidase committed Jul 3, 2024
1 parent 5eb2765 commit ef2ca6c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
4 changes: 4 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ bazel_dep(name = "rules_python", version = "0.32.2")

internal_deps = use_extension("@rules_ophiuchus//python:extensions.bzl", "internal_deps")
use_repo(internal_deps, "rules_ophiuchus_pip", "rules_ophiuchus_poetry_deps")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(python_version = "3.12")
use_repo(python, "python_3_12_host")
4 changes: 2 additions & 2 deletions python/poetry_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _package_impl(ctx):
# Get Python target toolchain and corresponding tags
py_toolchain = ctx.toolchains["@bazel_tools//tools/python:toolchain_type"]
py_runtime_info = py_toolchain.py3_runtime
runtime_tag, tags = _derive_environment_markers(py_runtime_info.interpreter.path, ctx.attr.platforms, ctx.attr.host_platform)
runtime_tag, tags = _derive_environment_markers(py_runtime_info.interpreter.path, ctx.attr.platforms, ctx.attr.system_platform)
python_version = tags["python_version"]
platform_tags = tags["platform_tags"]

Expand Down Expand Up @@ -145,7 +145,7 @@ package = rule(
"Default value corresponds to platforms defined at " +
"https://github.com/bazelbuild/rules_python/blob/23cf6b66/python/versions.bzl#L231-L277",
),
"host_platform": attr.string(doc = "The host platform environment markers as a JSON string"),
"system_platform": attr.string(doc = "The system platform environment markers as a JSON string"),
"_poetry_deps": attr.label(default = ":poetry_deps", cfg = "exec", executable = True),
},
toolchains = [
Expand Down
27 changes: 19 additions & 8 deletions python/private/poetry_parse.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def dfs_cycles(graph):
def normalize_dep_name(dep_name):
return dep_name.strip().strip('"').strip("'").replace("_", "-").replace(".", "-").lower()

def parse_lock_file(data, platforms = None, generate_extras = True, host_platform_tags = None):
def parse_lock_file(data, platforms = None, generate_extras = True, system_platform_tags = None):
_MARKERS = "markers = "
_SOURCE_URL = "url = "
_SOURCE_TYPE = "type = "
Expand Down Expand Up @@ -112,7 +112,7 @@ package(
name = "{name}",
constraint = "{name}=={version}",{description}
files = {{{files}
}},{deps}{markers}{source_urls}{extra_index_urls}{platforms}{host_platform_tags}
}},{deps}{markers}{source_urls}{extra_index_urls}{platforms}{system_platform_tags}
visibility = [{visibility}],
)
""".format(
Expand All @@ -125,7 +125,7 @@ package(
source_urls = "\n source_urls = [\n{}\n ],".format("\n".join([" " + url + "," for url in source_urls])) if source_urls else "",
extra_index_urls = "\n extra_index_urls = [{}],".format(", ".join(extra_index_urls)) if extra_index_urls else "",
platforms = "\n platforms = {},".format(platforms) if platforms else "",
host_platform_tags = "\n host_platform = {},".format(host_platform_tags) if host_platform_tags else "",
system_platform_tags = "\n system_platform = {},".format(system_platform_tags) if system_platform_tags else "",
visibility = ", ".join(['"{}"'.format(vis) for vis in visibility]),
)

Expand All @@ -144,7 +144,7 @@ package(

return result

def get_host_platform_tags(rctx):
def get_system_platform_tags(rctx):
# Return a JSON string with a dict values as described in
# https://peps.python.org/pep-0508/#environment-markers and
# https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/#platform-tag
Expand Down Expand Up @@ -175,14 +175,21 @@ print(json.dumps(json.dumps(dict(
return "{}"

def _poetry_parse_impl(rctx):
print(rctx.attr._python_host)
interpter = rctx.path(rctx.attr._python_host)
command = "import tomllib; print('{0}', 1); print(tomllib.load(open('{0}', 'rb')))".format(rctx.path(rctx.attr.lock))
print(command)
exec_result = rctx.execute([interpter, "-c", command], quiet = False)
print(exec_result)

header = "# Autogenerated file _poetry_parse_impl at rules_ophiuchus/python/private/poetry_parse.bzl:175"
rules_repository = str(rctx.path(rctx.attr._self)).split("/")[-4]
rules_repository = ("@@" if "~" in rules_repository else "@") + rules_repository
host_platform_tags_variable = "host_platform_tags"
system_platform_tags_variable = "system_platform_tags"
prefix = '''{header}\n\nload("{name}//python:poetry_deps.bzl", "package")'''.format(header = header, name = rules_repository)
host_platform_tags = """{k} = {v}""".format(k = host_platform_tags_variable, v = get_host_platform_tags(rctx))
lock_file_content = parse_lock_file(rctx.read(rctx.attr.lock), rctx.attr.platforms, rctx.attr.generate_extras, host_platform_tags_variable)
rctx.file("BUILD", "{}\n\n{}\n\n{}".format(prefix, host_platform_tags, lock_file_content))
system_platform_tags = """{k} = {v}""".format(k = system_platform_tags_variable, v = get_system_platform_tags(rctx))
lock_file_content = parse_lock_file(rctx.read(rctx.attr.lock), rctx.attr.platforms, rctx.attr.generate_extras, system_platform_tags_variable)
rctx.file("BUILD", "{}\n\n{}\n\n{}".format(prefix, system_platform_tags, lock_file_content))
rctx.file("WORKSPACE")

poetry_parse = repository_rule(
Expand All @@ -198,6 +205,10 @@ poetry_parse = repository_rule(
"platforms": attr.string_dict(
doc = "The mapping of interpter substrings to Python platform tags and environment markers as a JSON string",
),
"_python_host": attr.label(
allow_single_file = True,
default = "@python_3_12_host//:bin/python3",
),
"_self": attr.label(
allow_single_file = True,
default = ":poetry_parse.bzl",
Expand Down

0 comments on commit ef2ca6c

Please sign in to comment.