Skip to content

Commit 1dd2a7b

Browse files
committed
ENH: do not include uncommitted changer in the sdist
Including uncommitted changes in the sdist was implemented in #58 after the discussion in #53. However, the current behavior for which uncommitted changes to files under version control are included but not other files, is an hard to justify surprising half measure. After careful analysis, it has been determined that none of the use cases for this feature is still valid. Removing it makes the implementation easier, and the behavior less surprising and easier to document an explain. While at it, modernize the associated test.
1 parent b431df3 commit 1dd2a7b

File tree

2 files changed

+17
-50
lines changed

2 files changed

+17
-50
lines changed

mesonpy/__init__.py

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -869,45 +869,13 @@ def sdist(self, directory: Path) -> pathlib.Path:
869869

870870
with tarfile.open(meson_dist_path, 'r:gz') as meson_dist, mesonpy._util.create_targz(sdist) as tar:
871871
for member in meson_dist.getmembers():
872-
# calculate the file path in the source directory
873-
assert member.name, member.name
874-
member_parts = member.name.split('/')
875-
if len(member_parts) <= 1:
876-
continue
877-
path = self._source_dir.joinpath(*member_parts[1:])
878-
879-
if not path.exists() and member.isfile():
880-
# File doesn't exists on the source directory but exists on
881-
# the Meson dist, so it is generated file, which we need to
882-
# include.
883-
# See https://mesonbuild.com/Reference-manual_builtin_meson.html#mesonadd_dist_script
884-
885-
# MESON_DIST_ROOT could have a different base name
886-
# than the actual sdist basename, so we need to rename here
872+
if member.isfile():
887873
file = meson_dist.extractfile(member.name)
888-
member.name = str(pathlib.Path(dist_name, *member_parts[1:]).as_posix())
874+
# rewrite the path to match the sdist distribution name
875+
member.name = '/'.join((dist_name, *member.name.split('/')[1:]))
889876
tar.addfile(member, file)
890877
continue
891878

892-
if not path.is_file():
893-
continue
894-
895-
info = tarfile.TarInfo(member.name)
896-
file_stat = os.stat(path)
897-
info.mtime = member.mtime
898-
info.size = file_stat.st_size
899-
info.mode = int(oct(file_stat.st_mode)[-3:], 8)
900-
901-
# rewrite the path if necessary, to match the sdist distribution name
902-
if dist_name != meson_dist_name:
903-
info.name = pathlib.Path(
904-
dist_name,
905-
path.relative_to(self._source_dir)
906-
).as_posix()
907-
908-
with path.open('rb') as f:
909-
tar.addfile(info, fileobj=f)
910-
911879
# add PKG-INFO to dist file to make it a sdist
912880
pkginfo_info = tarfile.TarInfo(f'{dist_name}/PKG-INFO')
913881
pkginfo_info.mtime = time.time() # type: ignore[assignment]

tests/test_sdist.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: MIT
44

55
import os
6+
import pathlib
67
import stat
78
import sys
89
import tarfile
@@ -117,37 +118,35 @@ def test_contents_subdirs(sdist_subdirs):
117118

118119

119120
def test_contents_unstaged(package_pure, tmp_path):
120-
new_data = textwrap.dedent('''
121-
def bar():
121+
new = textwrap.dedent('''
122+
def bar():
122123
return 'foo'
123124
''').strip()
124125

125-
with open('pure.py', 'r') as f:
126-
old_data = f.read()
127-
128-
try:
129-
with in_git_repo_context():
130-
with open('pure.py', 'w') as f, open('crap', 'x'):
131-
f.write(new_data)
126+
old = pathlib.Path('pure.py').read_text()
132127

128+
with in_git_repo_context():
129+
try:
130+
pathlib.Path('pure.py').write_text(new)
131+
pathlib.Path('other.py').touch()
133132
sdist_path = mesonpy.build_sdist(os.fspath(tmp_path))
134-
finally:
135-
with open('pure.py', 'w') as f:
136-
f.write(old_data)
137-
os.unlink('crap')
133+
finally:
134+
pathlib.Path('pure.py').write_text(old)
135+
pathlib.Path('other.py').unlink()
138136

139137
with tarfile.open(tmp_path / sdist_path, 'r:gz') as sdist:
140138
names = {member.name for member in sdist.getmembers()}
141139
mtimes = {member.mtime for member in sdist.getmembers()}
142-
read_data = sdist.extractfile('pure-1.0.0/pure.py').read().replace(b'\r\n', b'\n')
140+
data = sdist.extractfile('pure-1.0.0/pure.py').read().replace(b'\r\n', b'\n')
143141

142+
# Verify that uncommitted changes are not included in the sdist.
144143
assert names == {
145144
'pure-1.0.0/PKG-INFO',
146145
'pure-1.0.0/meson.build',
147146
'pure-1.0.0/pure.py',
148147
'pure-1.0.0/pyproject.toml',
149148
}
150-
assert read_data == new_data.encode()
149+
assert data == old.encode()
151150

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

0 commit comments

Comments
 (0)