Skip to content

Commit

Permalink
chore: drop use of system Python interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
oxidase committed Jul 4, 2024
1 parent 220a39d commit a5258da
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 43 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@ jobs:
include: # TODO: windows-latest
- os: ubuntu-latest
cache: ~/.cache/bazel
python: '3.8'
- os: macos-latest
cache: /private/var/tmp/_bazel_runner
python: '3.12'

steps:
- name: Checkout Code
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
python-version: "3.8"

- name: Setup bazelisk
uses: bazelbuild/setup-bazelisk@v2
Expand All @@ -38,7 +36,6 @@ jobs:
id: info
run: |
uname -a
python3 -V
bazel info
shell: bash

Expand Down
42 changes: 4 additions & 38 deletions python/private/poetry_parse.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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, system_platform_tags = None):
def parse_lock_file(data, platforms = None, generate_extras = True):
_MARKERS = "markers = "
_SOURCE_URL = "url = "
_SOURCE_TYPE = "type = "
Expand Down Expand Up @@ -114,7 +114,7 @@ package(
name = "{name}",
constraint = "{name}=={version}",{description}
files = {{{files}
}},{deps}{markers}{source_urls}{extra_index_urls}{platforms}{system_platform_tags}
}},{deps}{markers}{source_urls}{extra_index_urls}{platforms}
visibility = [{visibility}],
)
""".format(
Expand All @@ -127,7 +127,6 @@ 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 "",
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 @@ -146,37 +145,6 @@ package(

return result

def get_system_platform_tags(rctx):
# DEPRECATED: the sytem interpreter must not be used
# 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
result = rctx.execute(["python3", "-c", """
import json
import os
import platform
import sys
import sysconfig
print(json.dumps(json.dumps(dict(
os_name=os.name,
sys_platform=sys.platform,
platform_machine=platform.machine(),
platform_python_implementation=platform.python_implementation(),
platform_release=platform.release(),
platform_system=platform.system(),
platform_version=platform.version(),
python_version='.'.join(platform.python_version_tuple()[:2]),
python_full_version=platform.python_version(),
implementation_name=sys.implementation.name,
platform_tags=[sysconfig.get_platform()]
))))"""])

if result.return_code == 0:
return result.stdout

print(result.stderr)
return "{}"

def _poetry_parse_impl(rctx):
interpreter = rctx.path(rctx.attr._python_host)
command = "import tomllib; print('{0}', 1); print('ok'); print(tomllib.load(open('{0}', 'rb')))".format(rctx.path(rctx.attr.lock))
Expand All @@ -188,11 +156,9 @@ def _poetry_parse_impl(rctx):
header = "# Autogenerated file by _poetry_parse_impl in {}".format(self)
rules_repository = self.split("/", 1)[0]
rules_repository = ("@@" if "~" in rules_repository else "@") + rules_repository
system_platform_tags_variable = "system_platform_tags"
prefix = '''{header}\n\nload("{name}//python:poetry_deps.bzl", "package")'''.format(header = header, name = rules_repository)
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))
lock_file_content = parse_lock_file(rctx.read(rctx.attr.lock), rctx.attr.platforms, rctx.attr.generate_extras)
rctx.file("BUILD", "{}\n\n{}".format(prefix, lock_file_content))
rctx.file("WORKSPACE")

poetry_parse = repository_rule(
Expand Down
1 change: 1 addition & 0 deletions python/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def install_dependencies(toolchain_prefix, python_version, auth_patterns = {}, n
srcs = glob(include=["**/*.py"]),
data = glob(include=["**/*"], exclude=[
"bin/**/*",
"**/__pycache__/**",
"**/*.py",
"**/*.pyc",
"**/* *",
Expand Down

0 comments on commit a5258da

Please sign in to comment.