Skip to content

Commit

Permalink
cmd transonic-init-meson
Browse files Browse the repository at this point in the history
  • Loading branch information
paugier committed Feb 20, 2024
1 parent 8bec741 commit ecf4e11
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 55 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ mpi = ["mpi4py"]
[project.scripts]
transonic = "transonic.run:run"
transonic-get-include = "transonic_cl.get_includes:main"
transonic-init-meson = "transonic.init_meson:main"
transonic-clean-dir = "transonic.clean_dir:main"
transonic-init-meson = "transonic_cl.init_meson:main"
transonic-clean-dir = "transonic_cl.clean_dir:main"

[tool.pdm]
package-dir = "src"
Expand Down
52 changes: 0 additions & 52 deletions src/transonic/init_meson.py

This file was deleted.

File renamed without changes.
80 changes: 80 additions & 0 deletions src/transonic_cl/init_meson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import argparse
import sys

from pathlib import Path


template = """
python_sources = [
{sources}
]
py.install_sources(
python_sources,
subdir: '{subdir}'
)
"""


def process_directory(path_dir, path_pack=None):

if path_pack is None:
path_pack = path_dir
print(f"Process {path_dir}")
subdir=path_pack.name
else:
subdir=path_dir.relative_to(path_pack.parent)
print(f"Process subdir {subdir}")

names_py = sorted(path.name for path in path_dir.glob("*.py"))
print(f"{names_py = }")
paths_subdirs = [
path
for path in path_dir.glob("*")
if path.is_dir() and not path.name.startswith("__")
]

names_subdirs = sorted(path.name for path in paths_subdirs)
print(f"{names_subdirs = }")

if names_py:
code = template.format(
sources="\n".join(f" '{name}'," for name in names_py),
subdir=path_dir.relative_to(path_pack.parent),
)
else:
code = ""

if names_subdirs:
code += (
"\n" + "\n".join(f"subdir('{name}')" for name in names_subdirs) + "\n"
)

if not code:
return

with open(path_dir / "meson.build", "w", encoding="utf-8") as file:
file.write(code)

for path_subdir in paths_subdirs:
process_directory(path_subdir, path_pack)


def main():

parser = argparse.ArgumentParser(
prog="transonic-init-meson",
description="Create Meson files from a Python package",
)
parser.add_argument("path", help="Path package.")

args = parser.parse_args()

path_pack = Path(args.path)

if not path_pack.is_dir():
print("Path given is not a directory", file=sys.stderr)
sys.exit(1)

process_directory(path_pack)

2 changes: 1 addition & 1 deletion tests/test_clean_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from transonic.backends import backends

from transonic.clean_dir import main
from transonic_cl.clean_dir import main

cmd = "transonic-clean-dir"

Expand Down
95 changes: 95 additions & 0 deletions tests/test_init_meson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import sys
from unittest.mock import patch

from transonic_cl.init_meson import main

cmd = "transonic-init-meson"


mypack = {
"mypack": {
"a.py": None,
"b.py": None,
"c.txt": None,
"subpack0": {"d.py": None},
"subpack1": {},
"subpack2": {"subsubpack": {"e.py": None}},
}
}


meson_files = {
"mypack": """
python_sources = [
'a.py',
'b.py',
]
py.install_sources(
python_sources,
subdir: 'mypack'
)
subdir('subpack0')
subdir('subpack1')
subdir('subpack2')
""",
"mypack/subpack0": """
python_sources = [
'd.py',
]
py.install_sources(
python_sources,
subdir: 'mypack/subpack0'
)
""",
"mypack/subpack2": """
subdir('subsubpack')
""",
"mypack/subpack2/subsubpack": """
python_sources = [
'e.py',
]
py.install_sources(
python_sources,
subdir: 'mypack/subpack2/subsubpack'
)
""",
}


def create_package(package: dict, path_dir):
path_dir.mkdir(exist_ok=True)
for key, value in package.items():
if value is None:
# it should be a file
(path_dir / key).touch()
else:
assert isinstance(value, dict)
create_package(value, path_dir / key)


def test_clean_dir(tmp_path):
path_mypack = tmp_path / "mypack"
print(f"create package\n{path_mypack}")
create_package(mypack, tmp_path)

with patch("sys.argv", [cmd, str(path_mypack)]):
main()

meson_paths = sorted(path_mypack.rglob("meson.build"))
assert len(meson_paths) == len(meson_files)

for meson_path in meson_paths:
code = (path_mypack / meson_path).read_text(encoding="utf-8")

meson_path = meson_path.relative_to(path_mypack.parent)
print(meson_path)
code_should_be = meson_files[str(meson_path.parent)]

if code != code_should_be:
print(code_should_be)

assert code == code_should_be

0 comments on commit ecf4e11

Please sign in to comment.