From e77b1d7fe6aa72f4c33e81c414935f44c869153a Mon Sep 17 00:00:00 2001 From: Brendan Rollins <54694656+bhrollins@users.noreply.github.com> Date: Thu, 4 Aug 2022 15:09:22 -0400 Subject: [PATCH 1/2] Fix path search upon `python3 -m maturin` `sysconfig.get_path` was insufficient on my machine as it was unable to resolve my `scripts` directory without an explicit `scheme="nt_user"` invocation (Windows 11 running Python 3.10.6). Intention here was to implement a more generic method of resolving `scripts` directories. There may be more idiomatic ways to achieve this behavior, as well as more useful error messages than what are provided in this solution. --- maturin/__main__.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/maturin/__main__.py b/maturin/__main__.py index 6c2cdde0e..3e090bc99 100644 --- a/maturin/__main__.py +++ b/maturin/__main__.py @@ -2,8 +2,37 @@ import sys from pathlib import Path import sysconfig +from typing import Optional + +def get_maturin_path() -> Optional[Path]: + SCRIPT_NAME = "maturin" + + def script_dir(scheme: str) -> Path: + return sysconfig.get_path("scripts", scheme) + + def script_exists(dir: Path) -> bool: + for _, _, files in os.walk(dir): + for f in files: + name, *_ = os.path.splitext(f) + if name == SCRIPT_NAME: + return True + + return False + + + paths = list(filter(script_exists, + filter(os.path.exists, + map(script_dir, sysconfig.get_scheme_names())))) + + if paths: + return Path(paths[0]) / SCRIPT_NAME + + return None if __name__ == "__main__": - scripts_dir = sysconfig.get_path("scripts") - maturin = Path(scripts_dir) / "maturin" + maturin = get_maturin_path() + if maturin is None: + print('Unable to find `maturin` script') + exit(1) + os.execv(maturin, [str(maturin)] + sys.argv[1:]) From 3ff682e8fd3c066bca5f6a4b04bdab3ea0af4132 Mon Sep 17 00:00:00 2001 From: Brendan Rollins <54694656+bhrollins@users.noreply.github.com> Date: Thu, 4 Aug 2022 15:19:39 -0400 Subject: [PATCH 2/2] Correcting format In my haste to submit a pull-request I neglected to check format. Apologies. --- maturin/__main__.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/maturin/__main__.py b/maturin/__main__.py index 3e090bc99..04fde236a 100644 --- a/maturin/__main__.py +++ b/maturin/__main__.py @@ -4,35 +4,39 @@ import sysconfig from typing import Optional + def get_maturin_path() -> Optional[Path]: SCRIPT_NAME = "maturin" def script_dir(scheme: str) -> Path: return sysconfig.get_path("scripts", scheme) - + def script_exists(dir: Path) -> bool: for _, _, files in os.walk(dir): for f in files: name, *_ = os.path.splitext(f) if name == SCRIPT_NAME: return True - + return False - - - paths = list(filter(script_exists, - filter(os.path.exists, - map(script_dir, sysconfig.get_scheme_names())))) + + paths = list( + filter( + script_exists, + filter(os.path.exists, map(script_dir, sysconfig.get_scheme_names())), + ) + ) if paths: return Path(paths[0]) / SCRIPT_NAME - + return None + if __name__ == "__main__": maturin = get_maturin_path() if maturin is None: - print('Unable to find `maturin` script') + print("Unable to find `maturin` script") exit(1) os.execv(maturin, [str(maturin)] + sys.argv[1:])