Skip to content

Commit 19fbca9

Browse files
committed
feat: remove awk dependency
1 parent 48d356a commit 19fbca9

File tree

6 files changed

+212
-37
lines changed

6 files changed

+212
-37
lines changed

lib/tests/py_zip_test.bzl

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
"""unit tests for globstar match algorithm
2-
3-
Ref: https://github.com/aspect-build/bazel-lib/blob/1df2becc7a2cc06b76ca4f7e/lib/tests/glob_match_test.bzl
4-
"""
1+
"""unit tests for py_zip rule"""
52

63
load("@bazel_skylib//lib:partial.bzl", "partial")
74
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")

lib/tests/runfiles_test.bzl

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
"""unit tests for globstar match algorithm
2-
3-
Ref: https://github.com/aspect-build/bazel-lib/blob/1df2becc7a2cc06b76ca4f7e/lib/tests/glob_match_test.bzl
4-
"""
1+
"""unit tests for runfiles rule"""
52

63
load("@bazel_skylib//lib:partial.bzl", "partial")
74
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts", "unittest")

python/poetry_venv.bzl

+2-29
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,3 @@
1-
def _poetry_venv_impl(rctx):
2-
result = rctx.execute([
3-
"awk",
4-
"-f",
5-
rctx.attr._venv,
6-
rctx.attr.lock,
7-
])
8-
if result.return_code != 0:
9-
fail("awk failed (status {}): {}".format(result.return_code, result.stderr))
1+
load("//python:private/poetry_venv.bzl", _poetry_venv = "poetry_venv")
102

11-
rules_repository = str(rctx.path(rctx.attr._venv)).split("/")[-3]
12-
rules_repository = ("@@" if "~" in rules_repository else "@") + rules_repository
13-
prefix = '''load("{name}//python:poetry_deps.bzl", "package")\n'''.format(name = rules_repository)
14-
rctx.file("BUILD", prefix + result.stdout)
15-
rctx.file("WORKSPACE")
16-
17-
poetry_venv = repository_rule(
18-
attrs = {
19-
"lock": attr.label(
20-
allow_single_file = True,
21-
doc = "Poetry lock file",
22-
),
23-
"_venv": attr.label(
24-
allow_single_file = True,
25-
default = ":poetry_venv.awk",
26-
),
27-
},
28-
doc = """Process Poetry lock file.""",
29-
implementation = _poetry_venv_impl,
30-
)
3+
poetry_venv = _poetry_venv

python/private/poetry_venv.bzl

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
def parse_lock_file(data):
2+
_MARKERS = "markers = "
3+
result = ""
4+
for package_lines in data.split("[[package]]"):
5+
section, name, version, description, files, deps, markers = "package", "", "", "", "", [], {}
6+
for line in package_lines.split("\n"):
7+
line = line.strip()
8+
if line == "[package.dependencies]":
9+
section = "dependencies"
10+
elif line.startswith("["):
11+
section = "unlnown"
12+
elif section == "package" and line.startswith("name = "):
13+
name = line
14+
elif section == "package" and line.startswith("version = "):
15+
version = line
16+
elif section == "package" and line.startswith("description = "):
17+
description = line
18+
elif section == "package" and line.startswith("{file = ") and ", hash = " in line:
19+
files += "\n " + line.replace("{file = ", "").replace(", hash = ", ": ").replace("},", ",")
20+
elif section == "dependencies" and line:
21+
dep_name, dep_version = line.split("=", 1)
22+
dep_name = dep_name.strip().replace("_.", "-").lower()
23+
deps.append('":{}"'.format(dep_name))
24+
if _MARKERS in dep_version:
25+
dep_marker = dep_version[dep_version.find(_MARKERS) + len(_MARKERS):]
26+
for index in range(1, len(dep_marker)):
27+
if dep_marker[index - 1] != "\\" and dep_marker[index] == '"':
28+
markers[dep_name] = dep_marker[1:index]
29+
print(markers[dep_name])
30+
break
31+
32+
if name:
33+
result += """
34+
package(
35+
{name},
36+
{version},{description}
37+
files = {{{files}
38+
}},{deps}{markers}
39+
visibility = [\"//visibility:public\"],
40+
)
41+
""".format(
42+
name = name,
43+
version = version,
44+
description = "\n " + description + "," if description else "",
45+
files = files,
46+
deps = "\n deps = [{}],".format(", ".join(deps)) if deps else "",
47+
markers = "\n markers ='''{}'''".format(json.encode(markers)) if markers else "",
48+
)
49+
50+
return result
51+
52+
def _poetry_venv_impl(rctx):
53+
result = rctx.execute([
54+
"awk",
55+
"-f",
56+
rctx.attr._venv,
57+
rctx.attr.lock,
58+
])
59+
if result.return_code != 0:
60+
fail("awk failed (status {}): {}".format(result.return_code, result.stderr))
61+
62+
# lock_lines = rctx.read(rctx.attr.lock).split("\n")
63+
# print (lock)
64+
65+
rules_repository = str(rctx.path(rctx.attr._venv)).split("/")[-3]
66+
rules_repository = ("@@" if "~" in rules_repository else "@") + rules_repository
67+
prefix = '''load("{name}//python:poetry_deps.bzl", "package")\n'''.format(name = rules_repository)
68+
rctx.file("BUILD", prefix + result.stdout)
69+
rctx.file("WORKSPACE")
70+
71+
poetry_venv = repository_rule(
72+
attrs = {
73+
"lock": attr.label(
74+
allow_single_file = True,
75+
doc = "Poetry lock file",
76+
),
77+
"_venv": attr.label(
78+
allow_single_file = True,
79+
default = ":poetry_venv.awk",
80+
),
81+
},
82+
doc = """Process Poetry lock file.""",
83+
implementation = _poetry_venv_impl,
84+
)

python/tests/BUILD

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
load(":markers_test.bzl", "markers_test_suite")
2+
load(":poetry_venv_test.bzl", "poetry_venv_test_suite")
23

34
markers_test_suite()
5+
6+
poetry_venv_test_suite()

python/tests/poetry_venv_test.bzl

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""unit tests for globstar poetry_venv repository rule"""
2+
3+
load("@bazel_skylib//lib:partial.bzl", "partial")
4+
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts", "unittest")
5+
load("//python:private/poetry_venv.bzl", "parse_lock_file")
6+
7+
def _parse_lock_file_test_impl(ctx):
8+
env = unittest.begin(ctx)
9+
10+
lock = """# This file is automatically @generated by Poetry 1.4.1 and should not be changed by hand.
11+
12+
[[package]]
13+
name = "aenum"
14+
version = "3.1.12"
15+
description = "Advanced Enumerations (compatible with Python's stdlib Enum), NamedTuples, and NamedConstants"
16+
category = "main"
17+
optional = false
18+
python-versions = "*"
19+
files = [
20+
{file = "aenum-3.1.12-py2-none-any.whl", hash = "sha256:8d79c9e3ec997220e355b96b322e672fb56a53e61744138ed838407e3a07b610"},
21+
{file = "aenum-3.1.12-py3-none-any.whl", hash = "sha256:2d544ef7323c088d68abf9a84b9f3f6db0d516fec685e15678b5f84fdb7b8ba0"},
22+
{file = "aenum-3.1.12.tar.gz", hash = "sha256:3e531c91860a81f885f7e6e97d219ae9772cb899580084788935dad7d9742ef0"},
23+
]
24+
25+
[[package]]
26+
name = "moto"
27+
version = "4.1.6"
28+
description = ""
29+
category = "main"
30+
optional = false
31+
python-versions = ">=3.7"
32+
files = [
33+
{file = "moto-4.1.6-py2.py3-none-any.whl", hash = "sha256:cfe398a1f6e317d061c47c3d2dd8c6893f3eb49154984a7cbb8bcd4ba517d67d"},
34+
{file = "moto-4.1.6.tar.gz", hash = "sha256:fdcc2731212ca050a28b2bc83e87628294bcbd55cb4f4c4692f972023fb1e7e6"},
35+
]
36+
37+
[package.dependencies]
38+
aws-xray-sdk = {version = ">=0.93,<0.96 || >0.96", optional = true, markers = "extra == \\"server\\""}
39+
boto3 = ">=1.9.201"
40+
botocore = ">=1.12.201"
41+
42+
43+
[[package]]
44+
name = "click"
45+
version = "8.1.3"
46+
description = "Composable command line interface toolkit"
47+
category = "main"
48+
optional = false
49+
python-versions = ">=3.7"
50+
files = [
51+
{file = "click-8.1.3-py3-none-any.whl", hash = "sha256:bb4d8133cb15a609f44e8213d9b391b0809795062913b383c62be0ee95b1db48"},
52+
{file = "click-8.1.3.tar.gz", hash = "sha256:7682dc8afb30297001674575ea00d1814d808d6a36af415a82bd481d37ba7b8e"},
53+
]
54+
55+
[package.dependencies]
56+
colorama = {version = "*", markers = "platform_system == \\"Windows\\""}
57+
packaging = ">=14.0"
58+
pywin32 = {version = ">=304", markers = "sys_platform == \\"win32\\""}
59+
60+
[[package]]
61+
name = "tqdm"
62+
version = "4.65.0"
63+
description = "Fast, Extensible Progress Meter"
64+
category = "main"
65+
optional = false
66+
python-versions = ">=3.7"
67+
files = [
68+
{file = "tqdm-4.65.0-py3-none-any.whl", hash = "sha256:c4f53a17fe37e132815abceec022631be8ffe1b9381c2e6e30aa70edc99e9671"},
69+
{file = "tqdm-4.65.0.tar.gz", hash = "sha256:1871fb68a86b8fb3b59ca4cdd3dcccbc7e6d613eeed31f4c332531977b89beb5"},
70+
]
71+
72+
[package.dependencies]
73+
colorama = {version = "*", markers = "platform_system == \\"Windows\\""}
74+
75+
[package.extras]
76+
dev = ["py-make (>=0.1.0)", "twine", "wheel"]
77+
notebook = ["ipywidgets (>=6)"]
78+
slack = ["slack-sdk"]
79+
telegram = ["requests"]
80+
81+
[[package]]
82+
name = "flask"
83+
version = "2.2.3"
84+
description = "A simple framework for building complex web applications."
85+
category = "main"
86+
optional = false
87+
python-versions = ">=3.7"
88+
files = [
89+
{file = "Flask-2.2.3-py3-none-any.whl", hash = "sha256:c0bec9477df1cb867e5a67c9e1ab758de9cb4a3e52dd70681f59fa40a62b3f2d"},
90+
{file = "Flask-2.2.3.tar.gz", hash = "sha256:7eb373984bf1c770023fce9db164ed0c3353cd0b53f130f4693da0ca756a2e6d"},
91+
]
92+
93+
[package.dependencies]
94+
click = ">=8.0"
95+
importlib-metadata = {version = ">=3.6.0", markers = "python_version < \\"3.10\\""}
96+
itsdangerous = ">=2.0"
97+
Jinja2 = ">=3.0"
98+
Werkzeug = ">=2.2.2"
99+
colorama = {version = "*", markers = "sys_platform == \\"win32\\""}
100+
exceptiongroup = {version = ">=1.0.0rc8", markers = "python_version < \\"3.11\\""}
101+
tomli = {version = ">=1.0.0", markers = "python_version < \\"3.11\\""}
102+
103+
104+
"""
105+
106+
build_content = parse_lock_file(lock)
107+
print(build_content)
108+
print("""markers ='''{"colorama ":"platform_system == \\\\\\"Windows\\\\\"""" in build_content)
109+
asserts.true(env, """package(\n name = "click",\n version = "8.1.3",\n description = "Composable command line interface toolkit",""" in build_content)
110+
asserts.true(env, """"moto-4.1.6.tar.gz": "sha256:fdcc2731212ca050a28b2bc83e87628294bcbd55cb4f4c4692f972023fb1e7e6",""" in build_content)
111+
asserts.true(env, """deps = [":click", ":importlib-metadata", ":itsdangerous", ":jinja2", ":werkzeug", ":colorama", ":exceptiongroup", ":tomli"],""" in build_content)
112+
asserts.true(env, """markers ='''{"colorama":"platform_system == \\\\\\"Windows\\\\\\"","pywin32":"sys_platform == \\\\\\"win32\\\\\\""}'''""" in build_content)
113+
return unittest.end(env)
114+
115+
parse_lock_file_test = unittest.make(_parse_lock_file_test_impl)
116+
117+
def poetry_venv_test_suite():
118+
unittest.suite(
119+
"poetry_venv_test",
120+
partial.make(parse_lock_file_test),
121+
)

0 commit comments

Comments
 (0)