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

Rely on session context when building sdist and wheel #3060

Merged
merged 1 commit into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
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
22 changes: 18 additions & 4 deletions setuptools/tests/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,26 @@ def suppress_exceptions(*excs):
pass


def multiproc(request):
"""
Return True if running under xdist and multiple
workers are used.
"""
try:
worker_id = request.getfixturevalue('worker_id')
except Exception:
return False
return worker_id != 'master'


@contextlib.contextmanager
def session_locked_tmp_dir(tmp_path_factory, name):
def session_locked_tmp_dir(request, tmp_path_factory, name):
"""Uses a file lock to guarantee only one worker can access a temp dir"""
root_tmp_dir = tmp_path_factory.getbasetemp().parent
# ^-- get the temp directory shared by all workers
locked_dir = root_tmp_dir / name
# get the temp directory shared by all workers
base = tmp_path_factory.getbasetemp()
shared_dir = base.parent if multiproc(request) else base

locked_dir = shared_dir / name
with FileLock(locked_dir.with_suffix(".lock")):
# ^-- prevent multiple workers to access the directory at once
locked_dir.mkdir(exist_ok=True, parents=True)
Expand Down
6 changes: 4 additions & 2 deletions setuptools/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def sample_project(tmp_path):

@pytest.fixture(scope="session")
def setuptools_sdist(tmp_path_factory, request):
with contexts.session_locked_tmp_dir(tmp_path_factory, "sdist_build") as tmp:
with contexts.session_locked_tmp_dir(
request, tmp_path_factory, "sdist_build") as tmp:
dist = next(tmp.glob("*.tar.gz"), None)
if dist:
return dist
Expand All @@ -78,7 +79,8 @@ def setuptools_sdist(tmp_path_factory, request):

@pytest.fixture(scope="session")
def setuptools_wheel(tmp_path_factory, request):
with contexts.session_locked_tmp_dir(tmp_path_factory, "wheel_build") as tmp:
with contexts.session_locked_tmp_dir(
request, tmp_path_factory, "wheel_build") as tmp:
dist = next(tmp.glob("*.whl"), None)
if dist:
return dist
Expand Down