Skip to content

Commit d6fac87

Browse files
Improve handing on not exists site-package
1 parent 42baee2 commit d6fac87

File tree

5 files changed

+80
-44
lines changed

5 files changed

+80
-44
lines changed

client/backend_arguments.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ def serialize(self) -> Dict[str, object]:
6969
}
7070

7171
def get_checked_directory_allowlist(self) -> Set[str]:
72-
return {element.path() for element in self.elements}
72+
paths = set()
73+
74+
for element in self.elements:
75+
excepted_path = element.path()
76+
if excepted_path is not None:
77+
paths.add(excepted_path)
78+
79+
return paths
7380

7481
def cleanup(self) -> None:
7582
pass
@@ -98,7 +105,14 @@ def serialize(self) -> Dict[str, object]:
98105
}
99106

100107
def get_checked_directory_allowlist(self) -> Set[str]:
101-
return {element.path() for element in self.elements}
108+
paths = set()
109+
110+
for element in self.elements:
111+
excepted_path = element.path()
112+
if excepted_path is not None:
113+
paths.add(excepted_path)
114+
115+
return paths
102116

103117
def cleanup(self) -> None:
104118
pass

client/configuration/exceptions.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,3 @@ def __init__(self, message: str) -> None:
1818
class InvalidPythonVersion(InvalidConfiguration):
1919
def __init__(self, message: str) -> None:
2020
super().__init__(message)
21-
22-
23-
class InvalidPackage(ValueError):
24-
def __init__(self, pkg_name: str) -> None:
25-
super().__init__(f"Invalid package: {pkg_name} does not exist.")

client/configuration/search_path.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ def _expand_relative_root(path: str, relative_root: str) -> str:
4444

4545
class Element(abc.ABC):
4646
@abc.abstractmethod
47-
def path(self) -> str:
47+
def path(self) -> Union[str, None]:
4848
raise NotImplementedError()
4949

5050
@abc.abstractmethod
51-
def command_line_argument(self) -> str:
51+
def command_line_argument(self) -> Union[str, None]:
5252
raise NotImplementedError()
5353

5454

@@ -81,9 +81,11 @@ class SitePackageElement(Element):
8181
package_name: str
8282
is_toplevel_module: bool = False
8383

84-
def package_path(self) -> str:
84+
def package_path(self) -> Union[str, None]:
8585
if not self.is_toplevel_module:
86-
return self.package_name
86+
if os.path.exists(f"{self.site_root}/{self.package_name}"):
87+
return self.package_name
88+
return None
8789

8890
this_pkg_filter = re.compile(
8991
r"{}-([0-99]\.)*dist-info(/)*.*".format(self.package_name)
@@ -100,7 +102,7 @@ def package_path(self) -> str:
100102
dist_info_path = f"{self.site_root}/{directory}"
101103
break
102104
else:
103-
raise exceptions.InvalidPackage(self.package_name)
105+
return None
104106

105107
not_toplevel_patterns: Tuple[re.Pattern[str], re.Pattern[str]] = (
106108
this_pkg_filter,
@@ -109,25 +111,27 @@ def package_path(self) -> str:
109111

110112
# pyre-fixme[61]: Local variable `dist_info_path` is undefined, or not always defined.
111113
with open(file=f"{dist_info_path}/RECORD", mode="r") as record:
112-
files = []
113-
for val in [line.split(",") for line in record.readlines()]:
114-
files.append(*val)
115-
for file in files:
116-
if not file:
117-
continue
114+
for line in record.readlines():
115+
file_name = line.split(",")[0]
118116
for pattern in not_toplevel_patterns:
119-
if pattern.fullmatch(file) is not None:
117+
if pattern.fullmatch(file_name):
120118
break
121119
else:
122-
return file
120+
return file_name
123121

124-
raise exceptions.InvalidPackage(self.package_name)
122+
return None
125123

126-
def path(self) -> str:
127-
return os.path.join(self.site_root, self.package_path())
124+
def path(self) -> Union[str, None]:
125+
excepted_package_path: Union[str, None] = self.package_path()
126+
if excepted_package_path is None:
127+
return None
128+
return os.path.join(self.site_root, excepted_package_path)
128129

129-
def command_line_argument(self) -> str:
130-
return self.site_root + "$" + self.package_path()
130+
def command_line_argument(self) -> Union[str, None]:
131+
excepted_package_path: Union[str, None] = self.package_path()
132+
if excepted_package_path is None:
133+
return None
134+
return self.site_root + "$" + excepted_package_path
131135

132136

133137
class RawElement(abc.ABC):
@@ -270,7 +274,10 @@ def process_raw_elements(
270274
elements: List[Element] = []
271275

272276
def add_if_exists(element: Element) -> bool:
273-
if os.path.exists(element.path()):
277+
excepted_path = element.path()
278+
if excepted_path is None:
279+
return False
280+
if os.path.exists(excepted_path):
274281
elements.append(element)
275282
return True
276283
return False

client/configuration/tests/search_path_test.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,32 @@ def test_create_raw_element(self) -> None:
7171
)
7272

7373
def test_path(self) -> None:
74-
self.assertEqual(SimpleElement("foo").path(), "foo")
75-
self.assertEqual(SubdirectoryElement("foo", "bar").path(), "foo/bar")
76-
self.assertEqual(SitePackageElement("foo", "bar").path(), "foo/bar")
74+
Path.mkdir(Path("foo"))
75+
Path.mkdir(Path("foo/bar"))
76+
77+
try:
78+
self.assertEqual(SimpleElement("foo").path(), "foo")
79+
self.assertEqual(SubdirectoryElement("foo", "bar").path(), "foo/bar")
80+
self.assertEqual(SitePackageElement("foo", "bar").path(), "foo/bar")
81+
finally:
82+
shutil.rmtree("foo")
7783

7884
def test_command_line_argument(self) -> None:
79-
self.assertEqual(SimpleElement("foo").command_line_argument(), "foo")
80-
self.assertEqual(
81-
SubdirectoryElement("foo", "bar").command_line_argument(),
82-
"foo$bar",
83-
)
84-
self.assertEqual(
85-
SitePackageElement("foo", "bar").command_line_argument(),
86-
"foo$bar",
87-
)
85+
Path.mkdir(Path("foo"))
86+
Path.mkdir(Path("foo/bar"))
87+
88+
try:
89+
self.assertEqual(SimpleElement("foo").command_line_argument(), "foo")
90+
self.assertEqual(
91+
SubdirectoryElement("foo", "bar").command_line_argument(),
92+
"foo$bar",
93+
)
94+
self.assertEqual(
95+
SitePackageElement("foo", "bar").command_line_argument(),
96+
"foo$bar",
97+
)
98+
finally:
99+
shutil.rmtree("foo")
88100

89101
Path.mkdir(Path("foo"))
90102
Path.mkdir(Path("foo/bar-1.0.0.dist-info"))

client/tests/backend_arguments_test.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# This source code is licensed under the MIT license found in the
44
# LICENSE file in the root directory of this source tree.
55

6+
import shutil
67
import tempfile
78
from pathlib import Path
89
from typing import Iterable, Tuple
@@ -576,15 +577,22 @@ def test_get_source_path__confliciting_source_specified(self) -> None:
576577
)
577578

578579
def test_get_checked_directory_for_simple_source_path(self) -> None:
580+
Path.mkdir(Path("super"))
581+
Path.mkdir(Path("super/slash"))
582+
579583
element0 = search_path.SimpleElement("ozzie")
580584
element1 = search_path.SubdirectoryElement("diva", "flea")
581585
element2 = search_path.SitePackageElement("super", "slash")
582-
self.assertCountEqual(
583-
SimpleSourcePath(
584-
[element0, element1, element2, element0]
585-
).get_checked_directory_allowlist(),
586-
[element0.path(), element1.path(), element2.path()],
587-
)
586+
587+
try:
588+
self.assertCountEqual(
589+
SimpleSourcePath(
590+
[element0, element1, element2, element0]
591+
).get_checked_directory_allowlist(),
592+
[element0.path(), element1.path(), element2.path()],
593+
)
594+
finally:
595+
shutil.rmtree("super")
588596

589597
def test_get_checked_directory_for_buck_source_path(self) -> None:
590598
self.assertCountEqual(

0 commit comments

Comments
 (0)