Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ testpaths = tests/
norecursedirs = .* build dist news tasks docs
markers =
parse
skip_nt
is_python
filterwarnings =
ignore::DeprecationWarning
Expand Down
45 changes: 17 additions & 28 deletions src/pythonfinder/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import os
import platform
import sys
import posixpath
import ntpath
import re
import shutil

Expand All @@ -21,28 +19,30 @@ 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.replace('\\', '\\')
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


PYENV_ROOT = os.path.expanduser(
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]

Expand All @@ -61,20 +61,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)
Expand All @@ -92,10 +81,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)
Expand Down
5 changes: 0 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
28 changes: 28 additions & 0 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from __future__ import annotations

import os
import re
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('/', '\\')
assert re.match(r"(\\[^/\\]+)+", input_path)
revised_path = possibly_convert_to_windows_style_path(input_path)
assert input_path == revised_path

# 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
2 changes: 1 addition & 1 deletion tests/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)