Skip to content

Commit

Permalink
BUG: add generated files to sdist
Browse files Browse the repository at this point in the history
PR #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]>
  • Loading branch information
3 people authored Nov 1, 2022
1 parent bfdc549 commit 71c6a3a
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 0 deletions.
13 changes: 13 additions & 0 deletions mesonpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,19 @@ def sdist(self, directory: Path) -> pathlib.Path:
continue
path = self._source_dir.joinpath(*member_parts[1:])

if not path.exists() and member.isfile():
# File doesn't exists on the source directory but exists on
# the Meson dist, so it is generated file, which we need to
# include.
# See https://mesonbuild.com/Reference-manual_builtin_meson.html#mesonadd_dist_script

# MESON_DIST_ROOT could have a different base name
# than the actual sdist basename, so we need to rename here
file = meson_dist.extractfile(member.name)
member.name = str(pathlib.Path(dist_name, *member_parts[1:]).as_posix())
tar.addfile(member, file)
continue

if not path.is_file():
continue

Expand Down
3 changes: 3 additions & 0 deletions tests/packages/generated-files/example-script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!python

print('hello!')
5 changes: 5 additions & 0 deletions tests/packages/generated-files/example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <stdio.h>

int main() {
printf("hello!");
}
6 changes: 6 additions & 0 deletions tests/packages/generated-files/executable_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def foo():
return 'bar'


if __name__ == '__main__':
print('foo?', foo())
34 changes: 34 additions & 0 deletions tests/packages/generated-files/generate_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import argparse
import os


def write_version_info(path):
# A real project would call something to generate this
dummy_version = '1.0.0'
dummy_hash = '013j2fiejqea'
if os.environ.get('MESON_DIST_ROOT'):
path = os.path.join(os.environ.get('MESON_DIST_ROOT'), path)
with open(path, 'w') as file:
file.write(f'__version__="{dummy_version}"\n')
file.write(
f'__git_version__="{dummy_hash}"\n'
)


def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'-o', '--outfile', type=str, help='Path to write version info to'
)
args = parser.parse_args()

if not args.outfile.endswith('.py'):
raise ValueError(
f'Output file must be a Python file. '
f'Got: {args.outfile} as filename instead'
)

write_version_info(args.outfile)


main()
39 changes: 39 additions & 0 deletions tests/packages/generated-files/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
project(
'executable-bit', 'c',
version: '1.0.0',
)

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

executable(
'example', 'example.c',
install: true,
)

install_data(
'example-script.py',
rename: 'example-script',
install_dir: get_option('bindir'),
)

py.install_sources('executable_module.py')

version_gen = files('generate_version.py')

if fs.exists('_version_meson.py')
py.install_sources('_version_meson.py')
else
custom_target('write_version_file',
output: '_version_meson.py',
command: [
py, version_gen, '-o', '@OUTPUT@'
],
build_by_default: true,
build_always_stale: true,
install: true,
install_dir: py.get_install_dir(pure: false)
)
meson.add_dist_script(py, version_gen, '-o', '_version_meson.py')
endif
3 changes: 3 additions & 0 deletions tests/packages/generated-files/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
build-backend = 'mesonpy'
requires = ['meson-python']
15 changes: 15 additions & 0 deletions tests/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,18 @@ def has_execbit(mode):
# this ourselves)
continue
assert has_execbit(mode) == expected[name], f'Wrong mode for {name}: {mode}'


def test_generated_files(sdist_generated_files):
sdist = tarfile.open(sdist_generated_files, 'r:gz')
expected = {
'executable_bit-1.0.0/PKG-INFO',
'executable_bit-1.0.0/example-script.py',
'executable_bit-1.0.0/example.c',
'executable_bit-1.0.0/executable_module.py',
'executable_bit-1.0.0/meson.build',
'executable_bit-1.0.0/pyproject.toml',
'executable_bit-1.0.0/_version_meson.py',
'executable_bit-1.0.0/generate_version.py',
}
assert set(tar.name for tar in sdist.getmembers()) == expected

0 comments on commit 71c6a3a

Please sign in to comment.