Skip to content

Commit 71c6a3a

Browse files
lithomas1pre-commit-ci[bot]FFY00
authored
BUG: add generated files to sdist
PR mesonbuild#177 Signed-off-by: Filipe Laíns <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Filipe Laíns <[email protected]>
1 parent bfdc549 commit 71c6a3a

File tree

8 files changed

+118
-0
lines changed

8 files changed

+118
-0
lines changed

mesonpy/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,19 @@ def sdist(self, directory: Path) -> pathlib.Path:
934934
continue
935935
path = self._source_dir.joinpath(*member_parts[1:])
936936

937+
if not path.exists() and member.isfile():
938+
# File doesn't exists on the source directory but exists on
939+
# the Meson dist, so it is generated file, which we need to
940+
# include.
941+
# See https://mesonbuild.com/Reference-manual_builtin_meson.html#mesonadd_dist_script
942+
943+
# MESON_DIST_ROOT could have a different base name
944+
# than the actual sdist basename, so we need to rename here
945+
file = meson_dist.extractfile(member.name)
946+
member.name = str(pathlib.Path(dist_name, *member_parts[1:]).as_posix())
947+
tar.addfile(member, file)
948+
continue
949+
937950
if not path.is_file():
938951
continue
939952

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!python
2+
3+
print('hello!')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
printf("hello!");
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def foo():
2+
return 'bar'
3+
4+
5+
if __name__ == '__main__':
6+
print('foo?', foo())
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import argparse
2+
import os
3+
4+
5+
def write_version_info(path):
6+
# A real project would call something to generate this
7+
dummy_version = '1.0.0'
8+
dummy_hash = '013j2fiejqea'
9+
if os.environ.get('MESON_DIST_ROOT'):
10+
path = os.path.join(os.environ.get('MESON_DIST_ROOT'), path)
11+
with open(path, 'w') as file:
12+
file.write(f'__version__="{dummy_version}"\n')
13+
file.write(
14+
f'__git_version__="{dummy_hash}"\n'
15+
)
16+
17+
18+
def main():
19+
parser = argparse.ArgumentParser()
20+
parser.add_argument(
21+
'-o', '--outfile', type=str, help='Path to write version info to'
22+
)
23+
args = parser.parse_args()
24+
25+
if not args.outfile.endswith('.py'):
26+
raise ValueError(
27+
f'Output file must be a Python file. '
28+
f'Got: {args.outfile} as filename instead'
29+
)
30+
31+
write_version_info(args.outfile)
32+
33+
34+
main()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
project(
2+
'executable-bit', 'c',
3+
version: '1.0.0',
4+
)
5+
6+
py_mod = import('python')
7+
fs = import('fs')
8+
py = py_mod.find_installation()
9+
10+
executable(
11+
'example', 'example.c',
12+
install: true,
13+
)
14+
15+
install_data(
16+
'example-script.py',
17+
rename: 'example-script',
18+
install_dir: get_option('bindir'),
19+
)
20+
21+
py.install_sources('executable_module.py')
22+
23+
version_gen = files('generate_version.py')
24+
25+
if fs.exists('_version_meson.py')
26+
py.install_sources('_version_meson.py')
27+
else
28+
custom_target('write_version_file',
29+
output: '_version_meson.py',
30+
command: [
31+
py, version_gen, '-o', '@OUTPUT@'
32+
],
33+
build_by_default: true,
34+
build_always_stale: true,
35+
install: true,
36+
install_dir: py.get_install_dir(pure: false)
37+
)
38+
meson.add_dist_script(py, version_gen, '-o', '_version_meson.py')
39+
endif
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
build-backend = 'mesonpy'
3+
requires = ['meson-python']

tests/test_sdist.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,18 @@ def has_execbit(mode):
103103
# this ourselves)
104104
continue
105105
assert has_execbit(mode) == expected[name], f'Wrong mode for {name}: {mode}'
106+
107+
108+
def test_generated_files(sdist_generated_files):
109+
sdist = tarfile.open(sdist_generated_files, 'r:gz')
110+
expected = {
111+
'executable_bit-1.0.0/PKG-INFO',
112+
'executable_bit-1.0.0/example-script.py',
113+
'executable_bit-1.0.0/example.c',
114+
'executable_bit-1.0.0/executable_module.py',
115+
'executable_bit-1.0.0/meson.build',
116+
'executable_bit-1.0.0/pyproject.toml',
117+
'executable_bit-1.0.0/_version_meson.py',
118+
'executable_bit-1.0.0/generate_version.py',
119+
}
120+
assert set(tar.name for tar in sdist.getmembers()) == expected

0 commit comments

Comments
 (0)