Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
cleanup init
Browse files Browse the repository at this point in the history
  • Loading branch information
iopapamanoglou committed Oct 29, 2024
1 parent 3100002 commit ab08e01
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
56 changes: 32 additions & 24 deletions src/faebryk/core/cpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@

import json
import logging
import shutil
from importlib.metadata import Distribution
from pathlib import Path

from faebryk.libs.header import get_header

logger = logging.getLogger(__name__)

_thisfile = Path(__file__)
_thisdir = _thisfile.parent
_cmake_dir = _thisdir
_build_dir = _cmake_dir / "build"


# Check if installed as editable
def is_editable_install():
Expand All @@ -32,11 +23,24 @@ def compile_and_load():
Forces C++ to compile into faebryk_core_cpp_editable module which is then loaded
into _cpp.
"""
import os
import platform
import shutil
import sys
from pathlib import Path

from faebryk.libs.header import formatted_file_contents, get_header
from faebryk.libs.util import run_live

_thisfile = Path(__file__)
_thisdir = _thisfile.parent
_cmake_dir = _thisdir
_build_dir = _cmake_dir / "build"
pyi_source = _build_dir / "faebryk_core_cpp_editable.pyi"

date_files = [pyi_source]
dates = {k: os.path.getmtime(k) if k.exists() else 0 for k in date_files}

# check for cmake binary existing
if not shutil.which("cmake"):
raise RuntimeError(
Expand All @@ -63,8 +67,8 @@ def compile_and_load():
str(_build_dir),
"-DEDITABLE=1",
"-DPython_EXECUTABLE=" + sys.executable,
]
+ other_flags,
*other_flags,
],
logger=logger,
)
run_live(
Expand All @@ -79,26 +83,30 @@ def compile_and_load():
if not _build_dir.exists():
raise RuntimeError("build directory not found")

# add build dir to sys path
sys.path.append(str(_build_dir))

modified = {k for k, v in dates.items() if os.path.getmtime(k) > v}

# move autogenerated type stub file to source directory
if pyi_source in modified:
pyi_out = _thisfile.with_suffix(".pyi")
pyi_out.write_text(
formatted_file_contents(
get_header()
+ "\n"
+ "# This file is auto-generated by nanobind.\n"
+ "# Do not edit this file directly; edit the corresponding\n"
+ "# C++ file instead.\n\n"
+ pyi_source.read_text()
)
)


# Re-export c++ with type hints provided by __init__.pyi
if is_editable_install():
logger.warning("faebryk is installed as editable package, compiling c++ code")
compile_and_load()
from faebryk_core_cpp_editable import * # type: ignore # noqa: E402, F403

# move autogenerated type stub file to source directory
pyi_file = _build_dir / "faebryk_core_cpp_editable.pyi"
pyi_out = _thisfile.with_suffix(".pyi")
pyi_out.write_text(
get_header()
+ "\n"
+ "# This file is auto-generated by nanobind.\n"
+ "# Do not edit this file directly; edit the corresponding\n"
+ "# C++ file instead.\n"
+ pyi_file.read_text()
)

else:
from faebryk_core_cpp import * # type: ignore # noqa: E402, F403
7 changes: 7 additions & 0 deletions src/faebryk/libs/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
# SPDX-License-Identifier: MIT


import black


def get_header():
return (
"# This file is part of the faebryk project\n"
"# SPDX-License-Identifier: MIT\n"
)


def formatted_file_contents(file_contents: str) -> str:
return black.format_str(file_contents, mode=black.Mode())
4 changes: 2 additions & 2 deletions src/faebryk/libs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ def run_live(
stdout_level: int | None = logging.DEBUG,
stderr_level: int | None = logging.ERROR,
**kwargs,
) -> str:
) -> tuple[str, subprocess.Popen]:
"""Runs a process and logs the output live."""

process = subprocess.Popen(
Expand Down Expand Up @@ -1134,4 +1134,4 @@ def run_live(
process.returncode, args[0], "".join(stdout)
)

return "\n".join(stdout)
return "\n".join(stdout), process

0 comments on commit ab08e01

Please sign in to comment.