Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/d2-bin/build_wheels.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import Literal, NamedTuple

D2_VERSION = "0.9.0"
WHEEL_VERSION = "0.9.0.post1"
D2_TAG = f"v{D2_VERSION}"
RELEASE_URL = f"https://github.com/terrastruct/d2/releases/download/{D2_TAG}"

Expand All @@ -36,9 +37,9 @@
}

PACKAGE_DIR = Path(__file__).resolve().parent
DIST_INFO = f"d2_bin-{D2_VERSION}.dist-info"
DIST_INFO = f"d2_bin-{WHEEL_VERSION}.dist-info"
ENTRY_POINTS = b"[console_scripts]\nd2-bin = d2_bin.__main__:main\n"
DATA_DIR = f"d2_bin-{D2_VERSION}.data"
DATA_DIR = f"d2_bin-{WHEEL_VERSION}.data"
LICENSE_FILES = ("LICENSE.txt", "THIRD_PARTY_NOTICES.txt")
SUMMARY = (
"The unmodified official D2 diagram scripting language binary,"
Expand Down Expand Up @@ -93,7 +94,7 @@ def metadata() -> bytes:
headers = [
"Metadata-Version: 2.4",
"Name: d2-bin",
f"Version: {D2_VERSION}",
f"Version: {WHEEL_VERSION}",
f"Summary: {SUMMARY}",
"Description-Content-Type: text/markdown",
"License-Expression: MPL-2.0",
Expand Down Expand Up @@ -159,7 +160,7 @@ def build_wheel(platform: Platform, checksums: dict[str, str], out_dir: Path) ->
files.append(WheelFile(f"{DIST_INFO}/RECORD", record(files), 0o644))

out_dir.mkdir(parents=True, exist_ok=True)
wheel_path = out_dir / f"d2_bin-{D2_VERSION}-py3-none-{tag}.whl"
wheel_path = out_dir / f"d2_bin-{WHEEL_VERSION}-py3-none-{tag}.whl"
with zipfile.ZipFile(wheel_path, "w", zipfile.ZIP_DEFLATED) as wheel:
for file in files:
info = zipfile.ZipInfo(file.path, date_time=(2020, 1, 1, 0, 0, 0))
Expand Down
27 changes: 7 additions & 20 deletions packages/d2-bin/src/d2_bin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
import sys
import sysconfig
from importlib.metadata import distribution
from pathlib import Path


def _user_scheme() -> str:
if sys.platform == "win32":
return "nt_user"
framework = sysconfig.get_config_var("PYTHONFRAMEWORK")
if sys.platform == "darwin" and isinstance(framework, str) and framework != "":
return "osx_framework_user"
return "posix_user"


def find_d2_bin() -> str:
exe = "d2.exe" if sys.platform == "win32" else "d2"
scripts_dirs = [
sysconfig.get_path("scripts"),
sysconfig.get_path("scripts", scheme=_user_scheme()),
str(Path(sys.prefix) / ("Scripts" if sys.platform == "win32" else "bin")),
]
for directory in scripts_dirs:
path = Path(directory) / exe
if path.is_file():
return str(path)
raise FileNotFoundError(f"{exe} not found in: {', '.join(scripts_dirs)}")
for file in distribution("d2-bin").files or ():
if file.name == exe and file.parent.name in {"bin", "Scripts"}:
path = Path(str(file.locate())).resolve()
if path.is_file():
return str(path)
raise FileNotFoundError(f"{exe} is missing from the d2-bin installation")
12 changes: 12 additions & 0 deletions tests/test_d2/test_d2_bin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pathlib import Path

import pytest

d2_bin = pytest.importorskip("d2_bin")


def test_find_d2_bin__returns_the_installed_binary() -> None:
path = Path(d2_bin.find_d2_bin())

assert path.is_file()
assert path.name in {"d2", "d2.exe"}
Loading