Skip to content

Commit 4e12400

Browse files
Update Rules_Py & Consolidate internal structure (#378)
Change all 'os.getenv[RUNFILES]' to now use the find_runfiles file. This is in preperation for the rules_python upgrade which changes this implementation again. The solution to the upgrade of the Runfiles was based on the work from @NEOatNHNG and his commit: dbb6261 And also done with the help of @PiotrKorkus
1 parent a6cf1d9 commit 4e12400

15 files changed

Lines changed: 141 additions & 344 deletions

File tree

MODULE.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ bazel_dep(name = "rules_pkg", version = "1.1.0")
3030
# Python version
3131
#
3232
###############################################################################
33-
bazel_dep(name = "rules_python", version = "1.4.1")
33+
bazel_dep(name = "rules_python", version = "1.8.3")
3434

3535
PYTHON_VERSION = "3.12"
3636

@@ -109,7 +109,7 @@ git_override(
109109

110110
# Add Linter
111111
bazel_dep(name = "rules_multitool", version = "1.9.0")
112-
bazel_dep(name = "score_tooling", version = "1.0.5")
112+
bazel_dep(name = "score_tooling", version = "1.1.0")
113113

114114
multitool_root = use_extension("@rules_multitool//multitool:extension.bzl", "multitool")
115115
use_repo(multitool_root, "actionlint_hub", "multitool", "ruff_hub", "shellcheck_hub", "yamlfmt_hub")

src/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ filegroup(
4242
"//src/extensions/score_source_code_linker:all_sources",
4343
"//src/extensions/score_sphinx_bundle:all_sources",
4444
"//src/extensions/score_sync_toml:all_sources",
45-
"//src/find_runfiles:all_sources",
4645
"//src/helper_lib:all_sources",
4746
],
4847
visibility = ["//visibility:public"],

src/extensions/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ py_library(
2020
srcs = ["@score_docs_as_code//src/extensions:score_plantuml.py"],
2121
imports = ["."],
2222
visibility = ["//visibility:public"],
23+
deps = ["@score_docs_as_code//src/helper_lib"],
2324
)

src/extensions/score_metamodel/BUILD

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ py_library(
4949
imports = ["."],
5050
visibility = ["//visibility:public"],
5151
# TODO: Figure out if all requirements are needed or if we can break it down a bit
52-
deps = all_requirements + ["@score_docs_as_code//src/helper_lib"],
52+
deps = all_requirements + [
53+
"@score_docs_as_code//src/helper_lib",
54+
],
5355
)
5456

5557
score_py_pytest(

src/extensions/score_metamodel/external_needs.py

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
# *******************************************************************************
1313

1414
import json
15-
import os
1615
import subprocess
17-
import sys
1816
from dataclasses import dataclass
1917
from pathlib import Path
2018

@@ -23,6 +21,8 @@
2321
from sphinx.util import logging
2422
from sphinx_needs.needsfile import NeedsList
2523

24+
from src.helper_lib import get_runfiles_dir
25+
2626
logger = logging.getLogger(__name__)
2727

2828

@@ -61,6 +61,7 @@ def parse_external_needs_sources_from_DATA(v: str) -> list[ExternalNeedsSource]:
6161
return []
6262

6363
logger.debug(f"Parsing external needs sources: {v}")
64+
6465
data = json.loads(v)
6566

6667
res = [res for el in data if (res := _parse_bazel_external_need(el))]
@@ -138,37 +139,21 @@ def temp(self: NeedsList):
138139

139140

140141
def get_external_needs_source(external_needs_source: str) -> list[ExternalNeedsSource]:
141-
bazel = external_needs_source or os.getenv("RUNFILES_DIR")
142-
143-
if bazel:
142+
if external_needs_source:
143+
# Path taken for all invocations via `bazel`
144144
external_needs = parse_external_needs_sources_from_DATA(external_needs_source)
145145
else:
146+
# This is the path taken for anything that doesn't
147+
# run via `bazel` e.g. esbonio or other direct executions
146148
external_needs = parse_external_needs_sources_from_bazel_query() # pyright: ignore[reportAny]
147-
148149
return external_needs
149150

150151

151152
def add_external_needs_json(e: ExternalNeedsSource, config: Config):
152-
json_file = f"{e.bazel_module}+/{e.target}/_build/needs/needs.json"
153-
if r := os.getenv("RUNFILES_DIR"):
154-
logger.debug("Using runfiles to determine external needs JSON file.")
155-
fixed_json_file = Path(r) / json_file
156-
else:
157-
logger.debug(
158-
"Running outside bazel. "
159-
+ "Determining git root for external needs JSON file."
160-
)
161-
git_root = Path.cwd().resolve()
162-
while not (git_root / ".git").exists():
163-
git_root = git_root.parent
164-
if git_root == Path("/"):
165-
sys.exit("Could not find git root.")
166-
logger.debug(f"Git root found: {git_root}")
167-
fixed_json_file = git_root / "bazel-bin" / "ide_support.runfiles" / json_file
168-
169-
logger.debug(f"Fixed JSON file path: {json_file} -> {fixed_json_file}")
170-
json_file = fixed_json_file
171-
153+
json_file_raw = f"{e.bazel_module}+/{e.target}/_build/needs/needs.json"
154+
r = get_runfiles_dir()
155+
json_file = r / json_file_raw
156+
logger.debug(f"External needs.json: {json_file}")
172157
try:
173158
needs_json_data = json.loads(Path(json_file).read_text(encoding="utf-8")) # pyright: ignore[reportAny]
174159
except FileNotFoundError:
@@ -192,11 +177,11 @@ def add_external_needs_json(e: ExternalNeedsSource, config: Config):
192177
def add_external_docs_sources(e: ExternalNeedsSource, config: Config):
193178
# Note that bazel does NOT write the files under e.target!
194179
# {e.bazel_module}+ matches the original git layout!
195-
if r := os.getenv("RUNFILES_DIR"):
196-
docs_source_path = Path(r) / f"{e.bazel_module}+"
197-
else:
180+
r = get_runfiles_dir()
181+
if "ide_support.runfiles" in str(r):
198182
logger.error("Combo builds are currently only supported with Bazel.")
199183
return
184+
docs_source_path = Path(r) / f"{e.bazel_module}+"
200185

201186
if "collections" not in config:
202187
config.collections = {}

src/extensions/score_plantuml.py

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,48 +24,14 @@
2424
In addition it sets common PlantUML options, like output to svg_obj.
2525
"""
2626

27-
import os
28-
import sys
2927
from pathlib import Path
3028

3129
from sphinx.application import Sphinx
3230
from sphinx.util import logging
3331

34-
logger = logging.getLogger(__name__)
35-
36-
37-
def get_runfiles_dir() -> Path:
38-
if r := os.getenv("RUNFILES_DIR"):
39-
# Runfiles are only available when running in Bazel.
40-
# bazel build and bazel run are both supported.
41-
# i.e. `bazel build //:docs` and `bazel run //:docs`.
42-
logger.debug("Using runfiles to determine plantuml path.")
32+
from src.helper_lib import get_runfiles_dir
4333

44-
runfiles_dir = Path(r)
45-
46-
else:
47-
# The only way to land here is when running from within the virtual
48-
# environment created by the `:ide_support` rule in the BUILD file.
49-
# i.e. esbonio or manual sphinx-build execution within the virtual
50-
# environment.
51-
# We'll still use the plantuml binary from the bazel build.
52-
# But we need to find it first.
53-
logger.debug("Running outside bazel.")
54-
55-
git_root = Path.cwd().resolve()
56-
while not (git_root / ".git").exists():
57-
git_root = git_root.parent
58-
if git_root == Path("/"):
59-
sys.exit("Could not find git root.")
60-
61-
runfiles_dir = git_root / "bazel-bin" / "ide_support.runfiles"
62-
63-
if not runfiles_dir.exists():
64-
sys.exit(
65-
f"Could not find runfiles_dir at {runfiles_dir}. "
66-
"Have a look at README.md for instructions on how to build docs."
67-
)
68-
return runfiles_dir
34+
logger = logging.getLogger(__name__)
6935

7036

7137
def find_correct_path(runfiles: Path) -> Path:

src/extensions/score_sphinx_bundle/BUILD

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ py_library(
3131
"@score_docs_as_code//src/extensions/score_metamodel",
3232
"@score_docs_as_code//src/extensions/score_source_code_linker",
3333
"@score_docs_as_code//src/extensions/score_sync_toml",
34-
"@score_docs_as_code//src/find_runfiles",
3534
"@score_docs_as_code//src/helper_lib",
3635
],
3736
)

src/find_runfiles/BUILD

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/find_runfiles/__init__.py

Lines changed: 0 additions & 117 deletions
This file was deleted.

0 commit comments

Comments
 (0)