Skip to content

ENH: add support for symbolic links in package repository #728

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions mesonpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import argparse
import collections
import contextlib
import copy
import difflib
import functools
import importlib.machinery
Expand All @@ -23,6 +24,7 @@
import os
import pathlib
import platform
import posixpath
import re
import shutil
import subprocess
Expand Down Expand Up @@ -937,6 +939,29 @@ def sdist(self, directory: Path) -> pathlib.Path:

with tarfile.open(meson_dist_path, 'r:gz') as meson_dist, mesonpy._util.create_targz(sdist_path) as sdist:
for member in meson_dist.getmembers():
# Recursively resolve symbolic links. The source distribution
# archive format specification allows for symbolic links as
# long as the target path does not include a '..' component.
# This makes symbolic links support unusable in most cases,
# therefore include the symbolic link targets as regular files
# in all cases.
while member.issym():
name = member.name
target = posixpath.normpath(posixpath.join(posixpath.dirname(member.name), member.linkname))
try:
# This can be implemented using the .replace() method
# in Python 3.12 and later. The .replace() method was
# added as part of PEP 706 and back-ported to Python
# 3.9 and later in patch releases, thus it cannot be
# relied upon until the minimum supported Python
# version is 3.12.
member = copy.copy(meson_dist.getmember(target))
member.name = name
except KeyError:
# Symbolic link with absolute path target, pointing
# outside the archive, or dangling: ignore.
break

if member.isfile():
file = meson_dist.extractfile(member.name)

Expand Down
3 changes: 3 additions & 0 deletions tests/packages/symlinks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: 2025 The meson-python developers
#
# SPDX-License-Identifier: MIT
1 change: 1 addition & 0 deletions tests/packages/symlinks/baz.py
16 changes: 16 additions & 0 deletions tests/packages/symlinks/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# SPDX-FileCopyrightText: 2025 The meson-python developers
#
# SPDX-License-Identifier: MIT

project('symlinks', version: '1.0.0')

py = import('python').find_installation()

py.install_sources(
'__init__.py',
'submodule/__init__.py',
'submodule/aaa.py',
'submodule/bbb.py',
subdir: 'symlinks',
preserve_path: true,
)
7 changes: 7 additions & 0 deletions tests/packages/symlinks/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-FileCopyrightText: 2021 The meson-python developers
#
# SPDX-License-Identifier: MIT

[build-system]
build-backend = 'mesonpy'
requires = ['meson-python']
1 change: 1 addition & 0 deletions tests/packages/symlinks/qux.py
1 change: 1 addition & 0 deletions tests/packages/symlinks/submodule/__init__.py
6 changes: 6 additions & 0 deletions tests/packages/symlinks/submodule/aaa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SPDX-FileCopyrightText: 2025 The meson-python developers
#
# SPDX-License-Identifier: MIT

def foo():
return 42
1 change: 1 addition & 0 deletions tests/packages/symlinks/submodule/bbb.py
19 changes: 19 additions & 0 deletions tests/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,22 @@ def test_reproducible(package_pure, tmp_path):

assert sdist_path_a == sdist_path_b
assert tmp_path.joinpath('a', sdist_path_a).read_bytes() == tmp_path.joinpath('b', sdist_path_b).read_bytes()


def test_symlinks(sdist_symlinks):
with tarfile.open(sdist_symlinks, 'r:gz') as sdist:
names = {member.name for member in sdist.getmembers()}
mtimes = {member.mtime for member in sdist.getmembers()}

assert names == {
'symlinks-1.0.0/PKG-INFO',
'symlinks-1.0.0/meson.build',
'symlinks-1.0.0/pyproject.toml',
'symlinks-1.0.0/__init__.py',
'symlinks-1.0.0/submodule/__init__.py',
'symlinks-1.0.0/submodule/aaa.py',
'symlinks-1.0.0/submodule/bbb.py',
}

# All the archive members have a valid mtime.
assert 0 not in mtimes
Loading