Skip to content

Commit

Permalink
Merge pull request #3060 from pypa/bugfix/3059-simple-session-build
Browse files Browse the repository at this point in the history
Rely on session context when building sdist and wheel
  • Loading branch information
jaraco authored Feb 2, 2022
2 parents b2ba0e3 + 253ecb0 commit 7956c10
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
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

0 comments on commit 7956c10

Please sign in to comment.