From 2d8f3cd12a9f319e633893e32ccf551f7a23c96c Mon Sep 17 00:00:00 2001 From: cosine0 Date: Mon, 17 Jul 2023 19:23:44 +0900 Subject: [PATCH 1/3] Remove redundant code --- src/pythonfinder/environment.py | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/src/pythonfinder/environment.py b/src/pythonfinder/environment.py index eec23fe..c902d1a 100644 --- a/src/pythonfinder/environment.py +++ b/src/pythonfinder/environment.py @@ -3,8 +3,6 @@ import os import platform import sys -import posixpath -import ntpath import re import shutil @@ -28,7 +26,7 @@ def possibly_convert_to_windows_style_path(path): return revised_path elif path.startswith('\\'): drive, tail = re.match(r"^\\([a-zA-Z])\\(.*)", path).groups() - revised_path = drive.upper() + ":\\" + tail.replace('\\', '\\') + revised_path = drive.upper() + ":\\" + tail return revised_path return path @@ -38,11 +36,11 @@ def possibly_convert_to_windows_style_path(path): os.path.expandvars(os.environ.get("PYENV_ROOT", "~/.pyenv")) ) PYENV_ROOT = possibly_convert_to_windows_style_path(PYENV_ROOT) -PYENV_INSTALLED = shutil.which("pyenv") != None +PYENV_INSTALLED = shutil.which("pyenv") is not None ASDF_DATA_DIR = os.path.expanduser( os.path.expandvars(os.environ.get("ASDF_DATA_DIR", "~/.asdf")) ) -ASDF_INSTALLED = shutil.which("asdf") != None +ASDF_INSTALLED = shutil.which("asdf") is not None IS_64BIT_OS = None SYSTEM_ARCH = platform.architecture()[0] @@ -61,20 +59,9 @@ def possibly_convert_to_windows_style_path(path): """ -def join_path_for_platform(path, path_parts): - # If we're on Unix or Unix-like system - if os.name == 'posix' or sys.platform == 'linux': - return posixpath.join(path, *path_parts) - # If we're on Windows - elif os.name == 'nt' or sys.platform == 'win32': - return ntpath.join(path, *path_parts) - else: - raise Exception("Unknown environment") - - def set_asdf_paths(): if ASDF_INSTALLED: - python_versions = join_path_for_platform(ASDF_DATA_DIR, ["installs", "python"]) + python_versions = os.path.join(ASDF_DATA_DIR, "installs", "python") try: # Get a list of all files and directories in the given path all_files_and_dirs = os.listdir(python_versions) @@ -92,10 +79,10 @@ def set_pyenv_paths(): if PYENV_INSTALLED: is_windows = False if os.name == "nt": - python_versions = join_path_for_platform(PYENV_ROOT, ["pyenv-win", "versions"]) + python_versions = os.path.join(PYENV_ROOT, "pyenv-win", "versions") is_windows = True else: - python_versions = join_path_for_platform(PYENV_ROOT, ["versions"]) + python_versions = os.path.join(PYENV_ROOT, "versions") try: # Get a list of all files and directories in the given path all_files_and_dirs = os.listdir(python_versions) From 78317895faeb1b87c00d27bf447aac999a5d86dc Mon Sep 17 00:00:00 2001 From: cosine0 Date: Tue, 18 Jul 2023 18:53:41 +0900 Subject: [PATCH 2/3] Correct `possibly_convert_to_windows_style_path` --- setup.cfg | 1 - src/pythonfinder/environment.py | 22 ++++++++++++---------- tests/conftest.py | 5 ----- tests/test_environment.py | 25 +++++++++++++++++++++++++ tests/test_python.py | 2 +- 5 files changed, 38 insertions(+), 17 deletions(-) create mode 100644 tests/test_environment.py diff --git a/setup.cfg b/setup.cfg index 9da917e..bd7e49c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -75,7 +75,6 @@ testpaths = tests/ norecursedirs = .* build dist news tasks docs markers = parse - skip_nt is_python filterwarnings = ignore::DeprecationWarning diff --git a/src/pythonfinder/environment.py b/src/pythonfinder/environment.py index c902d1a..ad614a7 100644 --- a/src/pythonfinder/environment.py +++ b/src/pythonfinder/environment.py @@ -19,16 +19,18 @@ def possibly_convert_to_windows_style_path(path): if not isinstance(path, str): path = str(path) # Check if the path is in Unix-style (Git Bash) - if os.name == 'nt': - if path.startswith('/'): - drive, tail = re.match(r"^/([a-zA-Z])/(.*)", path).groups() - revised_path = drive.upper() + ":\\" + tail.replace('/', '\\') - return revised_path - elif path.startswith('\\'): - drive, tail = re.match(r"^\\([a-zA-Z])\\(.*)", path).groups() - revised_path = drive.upper() + ":\\" + tail - return revised_path - + if os.name != 'nt': + return path + if os.path.exists(path): + return path + match = re.match(r"[/\\]([a-zA-Z])[/\\](.*)", path) + if match is None: + return path + drive, rest_of_path = match.groups() + rest_of_path = rest_of_path.replace("/", "\\") + revised_path = f"{drive.upper()}:\\{rest_of_path}" + if os.path.exists(revised_path): + return revised_path return path diff --git a/tests/conftest.py b/tests/conftest.py index 0dadf5d..50ef101 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -24,11 +24,6 @@ pythoninfo = namedtuple("PythonVersion", ["name", "version", "path", "arch"]) -def pytest_runtest_setup(item): - if item.get_closest_marker("skip_nt") is not None and os.name == "nt": - pytest.skip("does not run on windows") - - @pytest.fixture def pathlib_tmpdir(request, tmpdir): yield Path(str(tmpdir)) diff --git a/tests/test_environment.py b/tests/test_environment.py new file mode 100644 index 0000000..43d4146 --- /dev/null +++ b/tests/test_environment.py @@ -0,0 +1,25 @@ +from __future__ import annotations + +import os +import tempfile + +import pytest + +from pythonfinder.environment import possibly_convert_to_windows_style_path + + +@pytest.mark.skipif(os.name != 'nt', reason="Only run on Windows") +def test_possibly_convert_to_windows_style_path(): + # Create a temporary directory + with tempfile.TemporaryDirectory() as tmpdirname: + # Get an input path in the form "\path\to\tempdir" + drive, tail = os.path.splitdrive(tmpdirname) + input_path = tail.replace('/', '\\') + revised_path = possibly_convert_to_windows_style_path(input_path) + assert input_path == revised_path + + # Get path in a form "/c/path/to/tempdir/test" + input_path = '/' + drive[0] + tail.replace('\\', '/') + expected = drive.upper() + tail.replace('/', '\\') + revised_path = possibly_convert_to_windows_style_path(input_path) + assert expected == revised_path diff --git a/tests/test_python.py b/tests/test_python.py index ee4a7a7..4c56cb1 100644 --- a/tests/test_python.py +++ b/tests/test_python.py @@ -95,6 +95,6 @@ def get_python_version(path, orig_fn=None): assert isinstance(parsed.version, Version) -@pytest.mark.skip_nt +@pytest.mark.skipif(os.name == "nt", reason="Does not run on Windows") def test_pythonfinder(expected_python_versions, all_python_versions): assert sorted(expected_python_versions) == sorted(all_python_versions) From dc7592a05689eaf5eec59c8c886d6e0583060cb6 Mon Sep 17 00:00:00 2001 From: cosine0 Date: Thu, 20 Jul 2023 07:31:08 +0900 Subject: [PATCH 3/3] Minor test consistency improvement --- tests/test_environment.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_environment.py b/tests/test_environment.py index 43d4146..2b9860f 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -1,6 +1,7 @@ from __future__ import annotations import os +import re import tempfile import pytest @@ -15,11 +16,13 @@ def test_possibly_convert_to_windows_style_path(): # Get an input path in the form "\path\to\tempdir" drive, tail = os.path.splitdrive(tmpdirname) input_path = tail.replace('/', '\\') + assert re.match(r"(\\[^/\\]+)+", input_path) revised_path = possibly_convert_to_windows_style_path(input_path) assert input_path == revised_path - # Get path in a form "/c/path/to/tempdir/test" - input_path = '/' + drive[0] + tail.replace('\\', '/') + # Get an input path in the form "/c/path/to/tempdir" + input_path = '/' + drive[0].lower() + tail.replace('\\', '/') + assert re.match(r"/[a-z](/[^/\\]+)+", input_path) expected = drive.upper() + tail.replace('/', '\\') revised_path = possibly_convert_to_windows_style_path(input_path) assert expected == revised_path