Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to ignore unsupported versions #14

Merged
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
13 changes: 7 additions & 6 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/pythonfinder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

__version__ = '1.1.1.dev0'

# Add NullHandler to "pythonfinder" logger, because Python2's default root
# logger has no handler and warnings like this would be reported:
#
# > No handlers could be found for logger "pythonfinder.models.pyenv"
import logging
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())

__all__ = ["Finder", "WindowsFinder", "SystemPath", "InvalidPythonVersion"]
from .pythonfinder import Finder
from .models import SystemPath, WindowsFinder
Expand Down
8 changes: 6 additions & 2 deletions src/pythonfinder/models/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class SystemPath(object):
pyenv_finder = attr.ib(default=None, validator=optional_instance_of("PyenvPath"))
system = attr.ib(default=False)
_version_dict = attr.ib(default=attr.Factory(defaultdict))
ignore_unsupported = attr.ib(default=False)

__finders = attr.ib(default=attr.Factory(dict))

Expand Down Expand Up @@ -129,7 +130,7 @@ def _setup_pyenv(self):
pyenv_index = self.path_order.index(last_pyenv)
except ValueError:
return
self.pyenv_finder = PyenvFinder.create(root=PYENV_ROOT)
self.pyenv_finder = PyenvFinder.create(root=PYENV_ROOT, ignore_unsupported=self.ignore_unsupported)
# paths = (v.paths.values() for v in self.pyenv_finder.versions.values())
root_paths = (
p for path in self.pyenv_finder.expanded_paths for p in path if p.is_root
Expand Down Expand Up @@ -268,7 +269,7 @@ def find_python_version(
return ver

@classmethod
def create(cls, path=None, system=False, only_python=False, global_search=True):
def create(cls, path=None, system=False, only_python=False, global_search=True, ignore_unsupported=False):
"""Create a new :class:`pythonfinder.models.SystemPath` instance.

:param path: Search path to prepend when searching, defaults to None
Expand All @@ -277,6 +278,8 @@ def create(cls, path=None, system=False, only_python=False, global_search=True):
:param system: bool, optional
:param only_python: Whether to search only for python executables, defaults to False
:param only_python: bool, optional
:param ignore_unsupported: Whether to ignore unsupported python versions, if False, an error is raised, defaults to True
:param ignore_unsupported: bool, optional
:return: A new :class:`pythonfinder.models.SystemPath` instance.
:rtype: :class:`pythonfinder.models.SystemPath`
"""
Expand All @@ -303,6 +306,7 @@ def create(cls, path=None, system=False, only_python=False, global_search=True):
only_python=only_python,
system=system,
global_search=global_search,
ignore_unsupported=ignore_unsupported,
)


Expand Down
23 changes: 20 additions & 3 deletions src/pythonfinder/models/pyenv.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding=utf-8 -*-
from __future__ import absolute_import, print_function

import logging

from collections import defaultdict

import attr
Expand All @@ -13,17 +15,32 @@
from .python import PythonVersion


logger = logging.getLogger(__name__)


@attr.s
class PyenvFinder(BaseFinder):
root = attr.ib(default=None, validator=optional_instance_of(Path))
# ignore_unsupported should come before versions, because its value is used
# in versions's default initializer.
ignore_unsupported = attr.ib(default=False)
versions = attr.ib()
pythons = attr.ib()

@versions.default
def get_versions(self):
versions = defaultdict(VersionPath)
for p in self.root.glob("versions/*"):
version = PythonVersion.parse(p.name)
try:
version = PythonVersion.parse(p.name)
except Exception:
if not self.ignore_unsupported:
raise
logger.warning(
'Unsupported Python version %r, ignoring...',
p.name, exc_info=True
)
continue
version_tuple = (
version.get("major"),
version.get("minor"),
Expand All @@ -47,6 +64,6 @@ def get_pythons(self):
return pythons

@classmethod
def create(cls, root):
def create(cls, root, ignore_unsupported=False):
root = ensure_path(root)
return cls(root=root)
return cls(root=root, ignore_unsupported=ignore_unsupported)
2 changes: 1 addition & 1 deletion src/pythonfinder/models/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def parse(cls, version):
is_debug = False
if version.endswith("-debug"):
is_debug = True
version, _, _ = verson.rpartition("-")
version, _, _ = version.rpartition("-")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^_^ Good catch, I am very good at typing

try:
version = parse_version(str(version))
except TypeError:
Expand Down
10 changes: 7 additions & 3 deletions src/pythonfinder/pythonfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,30 @@


class Finder(object):
def __init__(self, path=None, system=False, global_search=True):
def __init__(self, path=None, system=False, global_search=True, ignore_unsupported=False):
"""Finder A cross-platform Finder for locating python and other executables.

Searches for python and other specified binaries starting in `path`, if supplied,
but searching the bin path of `sys.executable` if `system=True`, and then
searching in the `os.environ['PATH']` if `global_search=True`. When `global_search`
is `False`, this search operation is restricted to the allowed locations of
`path` and `system`.

:param path: A bin-directory search location, defaults to None
:param path: str, optional
:param system: Whether to include the bin-dir of `sys.executable`, defaults to False
:param system: bool, optional
:param global_search: Whether to search the global path from os.environ, defaults to True
:param global_search: bool, optional
:param ignore_unsupported: Whether to ignore unsupported python versions, if False, an error is raised, defaults to True
:param ignore_unsupported: bool, optional
:returns: a :class:`~pythonfinder.pythonfinder.Finder` object.
"""

self.path_prepend = path
self.global_search = global_search
self.system = system
self.ignore_unsupported = ignore_unsupported
self._system_path = None
self._windows_finder = None

Expand All @@ -38,6 +41,7 @@ def system_path(self):
path=self.path_prepend,
system=self.system,
global_search=self.global_search,
ignore_unsupported=self.ignore_unsupported,
)
return self._system_path

Expand Down