Skip to content

Commit 286cbf0

Browse files
committed
Do the substitutions of the doc string and version into
pytest_dependency.py also also for setup.py build.
1 parent be1051b commit 286cbf0

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed

setup.py

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
skipped if any of the dependencies did fail or has been skipped.
77
"""
88

9+
from distutils.cmd import Command as du_cmd
910
import distutils.log
1011
import os
1112
import os.path
1213
import re
14+
import stat
1315
import string
1416
from setuptools import setup
15-
import setuptools.command.sdist as st_sdist
17+
import setuptools.command.build_py
18+
import setuptools.command.sdist
1619
try:
1720
import setuptools_scm
1821
version = setuptools_scm.get_version()
@@ -26,19 +29,47 @@
2629
distutils.log.warn("warning: cannot determine version number")
2730
version = "UNKNOWN"
2831

32+
doc_string = __doc__
2933

30-
class sdist(st_sdist.sdist):
31-
def make_release_tree(self, base_dir, files):
32-
st_sdist.sdist.make_release_tree(self, base_dir, files)
33-
if not self.dry_run:
34-
src = "pytest_dependency.py"
35-
dest = os.path.join(base_dir, src)
36-
if hasattr(os, 'link') and os.path.exists(dest):
37-
os.unlink(dest)
38-
subst = {'DOC': __doc__, 'VERSION': version}
39-
with open(src, "rt") as sf, open(dest, "wt") as df:
40-
df.write(string.Template(sf.read()).substitute(subst))
34+
class copy_file_mixin:
35+
"""Distutils copy_file() mixin.
4136
37+
Inject a custom version version of the copy_file() method that
38+
does some substitutions on the fly into distutils command class
39+
hierarchy.
40+
"""
41+
Subst_srcs = {"pytest_dependency.py"}
42+
Subst = {'DOC': doc_string, 'VERSION': version}
43+
def copy_file(self, infile, outfile, preserve_mode=1, preserve_times=1,
44+
link=None, level=1):
45+
if infile in self.Subst_srcs:
46+
fstat = os.stat(infile)
47+
if os.path.basename(outfile) == os.path.basename(infile):
48+
distutils.log.info("copying (with substitutions) %s -> %s",
49+
infile, os.path.dirname(outfile))
50+
else:
51+
distutils.log.info("copying (with substitutions) %s -> %s",
52+
infile, outfile)
53+
if not self.dry_run:
54+
if os.path.exists(outfile):
55+
os.unlink(outfile)
56+
with open(infile, "rt") as sf, open(outfile, "wt") as df:
57+
df.write(string.Template(sf.read()).substitute(self.Subst))
58+
if preserve_mode:
59+
os.chmod(outfile, stat.S_IMODE(fstat[stat.ST_MODE]))
60+
return (outfile, 1)
61+
else:
62+
# Note: can't use super() with Python 2.
63+
return du_cmd.copy_file(self, infile, outfile,
64+
preserve_mode=preserve_mode,
65+
preserve_times=preserve_times,
66+
link=link, level=level)
67+
68+
class build_py(copy_file_mixin, setuptools.command.build_py.build_py):
69+
pass
70+
71+
class sdist(copy_file_mixin, setuptools.command.sdist.sdist):
72+
pass
4273

4374
setup(
4475
name='pytest-dependency',
@@ -50,7 +81,7 @@ def make_release_tree(self, base_dir, files):
5081
maintainer_email='[email protected]',
5182
url='https://github.com/RKrahl/pytest-dependency',
5283
license='Apache Software License 2.0',
53-
long_description=__doc__,
84+
long_description=doc_string,
5485
project_urls={
5586
'Documentation': 'https://pytest-dependency.readthedocs.io/',
5687
'Source Code': 'https://github.com/RKrahl/pytest-dependency',
@@ -79,5 +110,5 @@ def make_release_tree(self, base_dir, files):
79110
'dependency = pytest_dependency',
80111
],
81112
},
82-
cmdclass = {'sdist': sdist},
113+
cmdclass = {'build_py': build_py, 'sdist': sdist},
83114
)

0 commit comments

Comments
 (0)