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

Fix site package find #777

Closed
Closed
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
42 changes: 32 additions & 10 deletions client/configuration/search_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
import glob
import logging
import os
from typing import Dict, Iterable, List, Sequence, Union
import re
from typing import Dict, Iterable, List, Sequence, Tuple, Union

from .. import filesystem
from . import exceptions

LOG: logging.Logger = logging.getLogger(__name__)
site_packages_in_root: Dict[str, Tuple[str]] = {}


def _expand_relative_root(path: str, relative_root: str) -> str:
Expand Down Expand Up @@ -222,12 +224,29 @@ def process_raw_elements(
) -> List[Element]:
elements: List[Element] = []

def add_if_exists(element: Element) -> bool:
def verify_valid(element: Element) -> bool:
# verify if the package_path or path(for SimpleElement) exist.(see facebook/pyre-check#773)
if os.path.exists(element.path()):
elements.append(element)
return True
if isinstance(element, SitePackageElement):
if not os.path.exists(element.site_root):
return False
if element.site_root not in site_packages_in_root:
site_packages_in_root[element.site_root] = tuple(
re.split(r"-([0-99]\.)*dist-info", package_path)[0]
for package_path in os.listdir(element.site_root)
if re.fullmatch(
rf"{element.package_name}-([0-99]\.)*dist-info", package_path
)
is not None
)
return element.package_name in site_packages_in_root[element.site_root]
return False

def add_if_exists(element: Element) -> None:
if os.path.exists(element.path()):
elements.append(element)

for raw_element in raw_elements:
expanded_raw_elements = raw_element.expand_glob()
if len(expanded_raw_elements) == 0 and required:
Expand All @@ -236,13 +255,14 @@ def add_if_exists(element: Element) -> bool:
)
for expanded_raw_element in expanded_raw_elements:
if isinstance(expanded_raw_element, SitePackageRawElement):
added = False
valid = False
for site_root in site_roots:
if added := add_if_exists(
expanded_raw_element.to_element(site_root)
):
element = expanded_raw_element.to_element(site_root)
valid = verify_valid(element)
if valid:
add_if_exists(element)
break
if not added:
if not valid:
if required:
raise exceptions.InvalidConfiguration(
f"Invalid path {expanded_raw_element.package_name}: does not exist."
Expand All @@ -256,14 +276,16 @@ def add_if_exists(element: Element) -> bool:
expanded_raw_element, (SimpleRawElement, SubdirectoryRawElement)
):
element = expanded_raw_element.to_element()
added = add_if_exists(element)
if not added:
valid = verify_valid(element)
Fixed Show fixed Hide fixed
if not valid:
if required:
raise exceptions.InvalidConfiguration(
f"Path does not exist for search path: {element}"
)
else:
LOG.warning(f"Path does not exist for search path: {element}")
else:
add_if_exists(element)
Fixed Show fixed Hide fixed
else:
raise RuntimeError(
f"Unhandled raw search path element type: {expanded_raw_element}"
Expand Down