|
20 | 20 | from pip._vendor.packaging.specifiers import Specifier
|
21 | 21 | from pip._vendor.pkg_resources import RequirementParseError, parse_requirements
|
22 | 22 |
|
23 |
| -from pip._internal.download import ( |
24 |
| - is_archive_file, is_url, path_to_url, url_to_path, |
25 |
| -) |
| 23 | +from pip._internal.download import is_archive_file, is_url, url_to_path |
26 | 24 | from pip._internal.exceptions import InstallationError
|
27 | 25 | from pip._internal.models.index import PyPI, TestPyPI
|
28 | 26 | from pip._internal.models.link import Link
|
@@ -203,58 +201,60 @@ def install_req_from_editable(
|
203 | 201 | )
|
204 | 202 |
|
205 | 203 |
|
206 |
| -def _get_path_or_url(path, name): |
207 |
| - # type: (str, str) -> str |
208 |
| - """ |
209 |
| - First, it checks whether a provided path is an installable directory |
210 |
| - (e.g. it has a setup.py). If it is, returns the path. |
211 |
| -
|
212 |
| - If false, check if the path is an archive file (such as a .whl). |
213 |
| - The function checks if the path is a file. If false, if the path has |
214 |
| - an @, it will treat it as a PEP 440 URL requirement and return the path. |
215 |
| - """ |
216 |
| - if os.path.isdir(path) and _looks_like_path(name): |
217 |
| - if not is_installable_dir(path): |
218 |
| - raise InstallationError( |
219 |
| - "Directory %r is not installable. Neither 'setup.py' " |
220 |
| - "nor 'pyproject.toml' found." % name |
221 |
| - ) |
222 |
| - return path_to_url(path) |
223 |
| - elif is_archive_file(path): |
224 |
| - if os.path.isfile(path): |
225 |
| - return path_to_url(path) |
226 |
| - else: |
227 |
| - urlreq_parts = name.split('@', 1) |
228 |
| - if len(urlreq_parts) < 2 or _looks_like_path(urlreq_parts[0]): |
229 |
| - logger.warning( |
230 |
| - 'Requirement %r looks like a filename, but the ' |
231 |
| - 'file does not exist', |
232 |
| - name |
233 |
| - ) |
234 |
| - return path_to_url(path) |
235 |
| - # If the path contains '@' and the part before it does not look |
236 |
| - # like a path, try to treat it as a PEP 440 URL req instead. |
237 |
| - |
238 |
| - |
239 | 204 | def _looks_like_path(name):
|
240 | 205 | # type: (str) -> bool
|
241 | 206 | """Checks whether the string "looks like" a path on the filesystem.
|
242 | 207 |
|
243 | 208 | This does not check whether the target actually exists, only judge from the
|
244 |
| - apperance. Returns true if any of the following is true: |
| 209 | + appearance. |
245 | 210 |
|
246 |
| - * A path separator is found (either os.path.sep or os.path.altsep). |
247 |
| - * The string starts with "." (current directory). |
| 211 | + Returns true if any of the following conditions is true: |
| 212 | + * a path separator is found (either os.path.sep or os.path.altsep); |
| 213 | + * a dot is found (which represents the current directory). |
248 | 214 | """
|
249 | 215 | if os.path.sep in name:
|
250 | 216 | return True
|
251 | 217 | if os.path.altsep is not None and os.path.altsep in name:
|
252 | 218 | return True
|
253 |
| - if name.startswith('.'): |
| 219 | + if name.startswith("."): |
254 | 220 | return True
|
255 | 221 | return False
|
256 | 222 |
|
257 | 223 |
|
| 224 | +def _get_url_from_path(path, name): |
| 225 | + # type: (str, str) -> str |
| 226 | + """ |
| 227 | + First, it checks whether a provided path is an installable directory |
| 228 | + (e.g. it has a setup.py). If it is, returns the path. |
| 229 | +
|
| 230 | + If false, check if the path is an archive file (such as a .whl). |
| 231 | + The function checks if the path is a file. If false, if the path has |
| 232 | + an @, it will treat it as a PEP 440 URL requirement and return the path. |
| 233 | + """ |
| 234 | + if _looks_like_path(name) and os.path.isdir(path): |
| 235 | + if is_installable_dir(path): |
| 236 | + return path_to_url(path) |
| 237 | + raise InstallationError( |
| 238 | + "Directory %r is not installable. Neither 'setup.py' " |
| 239 | + "nor 'pyproject.toml' found." % name |
| 240 | + ) |
| 241 | + if not is_archive_file(path): |
| 242 | + return None |
| 243 | + if os.path.isfile(path): |
| 244 | + return path_to_url(path) |
| 245 | + urlreq_parts = name.split('@', 1) |
| 246 | + if len(urlreq_parts) >= 2 and not _looks_like_path(urlreq_parts[0]): |
| 247 | + # If the path contains '@' and the part before it does not look |
| 248 | + # like a path, try to treat it as a PEP 440 URL req instead. |
| 249 | + return None |
| 250 | + logger.warning( |
| 251 | + 'Requirement %r looks like a filename, but the ' |
| 252 | + 'file does not exist', |
| 253 | + name |
| 254 | + ) |
| 255 | + return path_to_url(path) |
| 256 | + |
| 257 | + |
258 | 258 | def install_req_from_line(
|
259 | 259 | name, # type: str
|
260 | 260 | comes_from=None, # type: Optional[Union[str, InstallRequirement]]
|
@@ -295,9 +295,9 @@ def install_req_from_line(
|
295 | 295 | link = Link(name)
|
296 | 296 | else:
|
297 | 297 | p, extras_as_string = _strip_extras(path)
|
298 |
| - path_or_url = _get_path_or_url(p, name) |
299 |
| - if path_or_url: |
300 |
| - link = Link(path_or_url) |
| 298 | + url = _get_url_from_path(p, name) |
| 299 | + if url is not None: |
| 300 | + link = Link(url) |
301 | 301 |
|
302 | 302 | # it's a local file, dir, or url
|
303 | 303 | if link:
|
|
0 commit comments