diff --git a/client/backend_arguments.py b/client/backend_arguments.py index c5a3703155e..78d1ac9015f 100644 --- a/client/backend_arguments.py +++ b/client/backend_arguments.py @@ -69,7 +69,14 @@ def serialize(self) -> Dict[str, object]: } def get_checked_directory_allowlist(self) -> Set[str]: - return {element.path() for element in self.elements} + paths = set() + + for element in self.elements: + excepted_path = element.path() + if excepted_path is not None: + paths.add(excepted_path) + + return paths def cleanup(self) -> None: pass @@ -98,7 +105,14 @@ def serialize(self) -> Dict[str, object]: } def get_checked_directory_allowlist(self) -> Set[str]: - return {element.path() for element in self.elements} + paths = set() + + for element in self.elements: + excepted_path = element.path() + if excepted_path is not None: + paths.add(excepted_path) + + return paths def cleanup(self) -> None: pass diff --git a/client/configuration/exceptions.py b/client/configuration/exceptions.py index fa18852f729..644217e89cc 100644 --- a/client/configuration/exceptions.py +++ b/client/configuration/exceptions.py @@ -18,8 +18,3 @@ def __init__(self, message: str) -> None: class InvalidPythonVersion(InvalidConfiguration): def __init__(self, message: str) -> None: super().__init__(message) - - -class InvalidPackage(ValueError): - def __init__(self, pkg_name: str) -> None: - super().__init__(f"Invalid package: {pkg_name} does not exist.") diff --git a/client/configuration/search_path.py b/client/configuration/search_path.py index f11497d8b2c..cdb2795b7fc 100644 --- a/client/configuration/search_path.py +++ b/client/configuration/search_path.py @@ -44,11 +44,11 @@ def _expand_relative_root(path: str, relative_root: str) -> str: class Element(abc.ABC): @abc.abstractmethod - def path(self) -> str: + def path(self) -> Union[str, None]: raise NotImplementedError() @abc.abstractmethod - def command_line_argument(self) -> str: + def command_line_argument(self) -> Union[str, None]: raise NotImplementedError() @@ -81,9 +81,11 @@ class SitePackageElement(Element): package_name: str is_toplevel_module: bool = False - def package_path(self) -> str: + def package_path(self) -> Union[str, None]: if not self.is_toplevel_module: - return self.package_name + if os.path.exists(f"{self.site_root}/{self.package_name}"): + return self.package_name + return None this_pkg_filter = re.compile( r"{}-([0-99]\.)*dist-info(/)*.*".format(self.package_name) @@ -100,7 +102,7 @@ def package_path(self) -> str: dist_info_path = f"{self.site_root}/{directory}" break else: - raise exceptions.InvalidPackage(self.package_name) + return None not_toplevel_patterns: Tuple[re.Pattern[str], re.Pattern[str]] = ( this_pkg_filter, @@ -109,25 +111,27 @@ def package_path(self) -> str: # pyre-fixme[61]: Local variable `dist_info_path` is undefined, or not always defined. with open(file=f"{dist_info_path}/RECORD", mode="r") as record: - files = [] - for val in [line.split(",") for line in record.readlines()]: - files.append(*val) - for file in files: - if not file: - continue + for line in record.readlines(): + file_name = line.split(",")[0] for pattern in not_toplevel_patterns: - if pattern.fullmatch(file) is not None: + if pattern.fullmatch(file_name): break else: - return file + return file_name - raise exceptions.InvalidPackage(self.package_name) + return None - def path(self) -> str: - return os.path.join(self.site_root, self.package_path()) + def path(self) -> Union[str, None]: + excepted_package_path: Union[str, None] = self.package_path() + if excepted_package_path is None: + return None + return os.path.join(self.site_root, excepted_package_path) - def command_line_argument(self) -> str: - return self.site_root + "$" + self.package_path() + def command_line_argument(self) -> Union[str, None]: + excepted_package_path: Union[str, None] = self.package_path() + if excepted_package_path is None: + return None + return self.site_root + "$" + excepted_package_path class RawElement(abc.ABC): @@ -270,7 +274,10 @@ def process_raw_elements( elements: List[Element] = [] def add_if_exists(element: Element) -> bool: - if os.path.exists(element.path()): + excepted_path = element.path() + if excepted_path is None: + return False + if os.path.exists(excepted_path): elements.append(element) return True return False diff --git a/client/configuration/tests/search_path_test.py b/client/configuration/tests/search_path_test.py index 3104721c7d9..5cc5fa6d363 100644 --- a/client/configuration/tests/search_path_test.py +++ b/client/configuration/tests/search_path_test.py @@ -71,20 +71,32 @@ def test_create_raw_element(self) -> None: ) def test_path(self) -> None: - self.assertEqual(SimpleElement("foo").path(), "foo") - self.assertEqual(SubdirectoryElement("foo", "bar").path(), "foo/bar") - self.assertEqual(SitePackageElement("foo", "bar").path(), "foo/bar") + Path.mkdir(Path("foo")) + Path.mkdir(Path("foo/bar")) + + try: + self.assertEqual(SimpleElement("foo").path(), "foo") + self.assertEqual(SubdirectoryElement("foo", "bar").path(), "foo/bar") + self.assertEqual(SitePackageElement("foo", "bar").path(), "foo/bar") + finally: + shutil.rmtree("foo") def test_command_line_argument(self) -> None: - self.assertEqual(SimpleElement("foo").command_line_argument(), "foo") - self.assertEqual( - SubdirectoryElement("foo", "bar").command_line_argument(), - "foo$bar", - ) - self.assertEqual( - SitePackageElement("foo", "bar").command_line_argument(), - "foo$bar", - ) + Path.mkdir(Path("foo")) + Path.mkdir(Path("foo/bar")) + + try: + self.assertEqual(SimpleElement("foo").command_line_argument(), "foo") + self.assertEqual( + SubdirectoryElement("foo", "bar").command_line_argument(), + "foo$bar", + ) + self.assertEqual( + SitePackageElement("foo", "bar").command_line_argument(), + "foo$bar", + ) + finally: + shutil.rmtree("foo") Path.mkdir(Path("foo")) Path.mkdir(Path("foo/bar-1.0.0.dist-info")) diff --git a/client/tests/backend_arguments_test.py b/client/tests/backend_arguments_test.py index a114fe2f891..558c504775c 100644 --- a/client/tests/backend_arguments_test.py +++ b/client/tests/backend_arguments_test.py @@ -3,6 +3,7 @@ # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. +import shutil import tempfile from pathlib import Path from typing import Iterable, Tuple @@ -576,15 +577,22 @@ def test_get_source_path__confliciting_source_specified(self) -> None: ) def test_get_checked_directory_for_simple_source_path(self) -> None: + Path.mkdir(Path("super")) + Path.mkdir(Path("super/slash")) + element0 = search_path.SimpleElement("ozzie") element1 = search_path.SubdirectoryElement("diva", "flea") element2 = search_path.SitePackageElement("super", "slash") - self.assertCountEqual( - SimpleSourcePath( - [element0, element1, element2, element0] - ).get_checked_directory_allowlist(), - [element0.path(), element1.path(), element2.path()], - ) + + try: + self.assertCountEqual( + SimpleSourcePath( + [element0, element1, element2, element0] + ).get_checked_directory_allowlist(), + [element0.path(), element1.path(), element2.path()], + ) + finally: + shutil.rmtree("super") def test_get_checked_directory_for_buck_source_path(self) -> None: self.assertCountEqual(