build: include Python files in the "dist" tarball

Add `tools/dist-setuptools`: a new command to copy all of the source
files that setuptools would include in its sdist to a different
directory.  We use that during `make dist` to make sure those files end
up in the primary automake dist tarball.  This lets the Python build
system decide what gets included.

We also take the chance to tweak the version number in `_version.py` in
the distdir to be equal to the version number of the main package.

Note: our first attempts here involved building and unpacking an sdist
but that failed because setuptools tries to write to the source
directory, which automake (particularly, `make distcheck`) dislikes.

Finally, we have to adjust our debian packaging a bit to suppress a
false-positive lintian warning.
This commit is contained in:
Allison Karlitskaya 2023-04-27 19:51:45 +02:00 committed by Martin Pitt
parent a727cdd630
commit 065927169e
3 changed files with 51 additions and 0 deletions

View File

@ -43,6 +43,12 @@ distdir: $(DISTFILES)
@echo ' DIST $(DIST_ARCHIVES)'
# Needed to ensure the tarball is correct for $(VERSION) override
dist-hook: $(distdir)/src/cockpit/_version.py
$(distdir)/src/cockpit/_version.py: FORCE
$(srcdir)/tools/dist-setuptools '$(srcdir)' '$(distdir)'
@rm -f $(distdir)/src/cockpit/_version.py
$(AM_V_GEN) echo "__version__ = '$(VERSION)'" > $@
$(distdir)/version.m4: FORCE
@rm -f $(distdir)/version.m4
$(AM_V_GEN) echo 'm4_define(VERSION_NUMBER, [$(VERSION)])' > $@

View File

@ -1,5 +1,6 @@
# false positive: this *is* the source
cockpit source: source-is-missing *pkg/static/login.html*
cockpit source: source-is-missing *src/cockpit/data/fail.html*
cockpit source: source-is-missing *src/common/fail.html*
# source contains NPM modules required for running browser integration tests
cockpit source: source-is-missing *node_modules/*

44
tools/dist-setuptools Executable file
View File

@ -0,0 +1,44 @@
#!/usr/bin/python3
import argparse
import os
import shutil
import subprocess
import sys
import tempfile
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument('srcdir')
parser.add_argument('distdir')
args = parser.parse_args()
# setuptools can't create an sdist without writing (at least) an .egg-info
# directory to the source directory. That conflicts with automake's
# desire that `make dist` should not modify the source directory.
#
# As a hack: we use the setuptools egg_info command (with its output
# redirected to /tmp) to get a list of the files that we need to ship, and
# copy those over manually.
with tempfile.TemporaryDirectory() as tmpdir:
subprocess.check_call([sys.executable, '-c', 'from setuptools import setup; setup();',
'--quiet', 'egg_info', '--egg-base', tmpdir], cwd=args.srcdir)
# Collect the result
# We need to filter out the /tmp/xyz/src/cockpit.egg-info bits,
# which we can do by only taking the relative pathnames.
with open(f'{tmpdir}/cockpit.egg-info/SOURCES.txt') as file:
distfiles = [line.strip() for line in file if not os.path.isabs(line)]
# Copy the required source files into the named destination directory
for filename in distfiles:
source = f'{args.srcdir}/{filename}'
destination = f'{args.distdir}/{filename}'
if not os.path.exists(destination): # avoid re-copying e.g. AUTHORS
os.makedirs(os.path.dirname(destination), exist_ok=True)
shutil.copy(source, destination)
if __name__ == '__main__':
main()