Skip to content

Commit

Permalink
Improve handing on not exists site-package
Browse files Browse the repository at this point in the history
  • Loading branch information
WangGithubUser committed Aug 22, 2023
1 parent 66edd67 commit 93863db
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 44 deletions.
18 changes: 16 additions & 2 deletions client/backend_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions client/configuration/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
45 changes: 26 additions & 19 deletions client/configuration/search_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down
36 changes: 24 additions & 12 deletions client/configuration/tests/search_path_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
20 changes: 14 additions & 6 deletions client/tests/backend_arguments_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 93863db

Please sign in to comment.