Relative paths improvements for editable installs (#5896)

* Relative paths improvements for editable installs
This commit is contained in:
Matt Davis
2023-08-31 19:53:26 -04:00
committed by GitHub
parent 16334c9139
commit 90714e2268
2 changed files with 22 additions and 2 deletions
+1
View File
@@ -0,0 +1 @@
Relative paths improvements for editable installs.
+21 -2
View File
@@ -645,10 +645,29 @@ def determine_path_specifier(package: InstallRequirement):
if package.link.scheme in ["http", "https"]:
return package.link.url_without_fragment
if package.link.scheme == "file":
abs_path = Path(package.link.file_path).resolve()
current_dir = Path.cwd()
try:
return Path(package.link.file_path).relative_to(Path.cwd()).as_posix()
relative_path = abs_path.relative_to(current_dir)
return relative_path.as_posix()
except ValueError:
return Path(package.link.file_path).as_posix()
# If the direct relative_to fails, manually compute the relative path
common_parts = 0
for part_a, part_b in zip(abs_path.parts, current_dir.parts):
if part_a == part_b:
common_parts += 1
else:
break
# Number of ".." needed are the extra parts in the current directory
# beyond the common parts
up_levels = [".."] * (len(current_dir.parts) - common_parts)
# The relative path is constructed by going up as needed and then
# appending the non-common parts of the absolute path
rel_parts = up_levels + list(abs_path.parts[common_parts:])
relative_path = Path(*rel_parts)
return relative_path.as_posix()
def determine_vcs_specifier(package: InstallRequirement):