-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
178 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |