Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: support multiple visits of symlinks #277

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions backend/src/hatchling/builders/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,34 @@ def replace_file(src, dst):
os.remove(src)


def _safe_walk(root, followed_links=set()):
dirs = []
files = []
for entry in os.scandir(root):
try:
is_dir = entry.is_dir()
except OSError:
is_dir = False

if is_dir:
if entry.is_symlink():
# Don't get stuck in cycle
if entry.path in followed_links:
continue
followed_links = followed_links | { entry.path }

dirs.append(entry.name)
else:
files.append(entry.name)

yield root, dirs, files
for name in dirs:
path = os.path.join(root, name)
yield from _safe_walk(path, followed_links)


def safe_walk(path):
seen = set()
for root, dirs, files in os.walk(path, followlinks=True):
stat = os.stat(root)
identifier = stat.st_dev, stat.st_ino
if identifier in seen:
del dirs[:]
continue

seen.add(identifier)
yield root, dirs, files
return _safe_walk(path)


def get_known_python_major_versions():
Expand Down