Path: blob/main/test/lib/python3.9/site-packages/setuptools/command/sdist.py
4799 views
from distutils import log1import distutils.command.sdist as orig2import os3import sys4import io5import contextlib67from .py36compat import sdist_add_defaults89from .._importlib import metadata1011_default_revctrl = list121314def walk_revctrl(dirname=''):15"""Find all files under revision control"""16for ep in metadata.entry_points(group='setuptools.file_finders'):17for item in ep.load()(dirname):18yield item192021class sdist(sdist_add_defaults, orig.sdist):22"""Smart sdist that finds anything supported by revision control"""2324user_options = [25('formats=', None,26"formats for source distribution (comma-separated list)"),27('keep-temp', 'k',28"keep the distribution tree around after creating " +29"archive file(s)"),30('dist-dir=', 'd',31"directory to put the source distribution archive(s) in "32"[default: dist]"),33('owner=', 'u',34"Owner name used when creating a tar file [default: current user]"),35('group=', 'g',36"Group name used when creating a tar file [default: current group]"),37]3839negative_opt = {}4041README_EXTENSIONS = ['', '.rst', '.txt', '.md']42READMES = tuple('README{0}'.format(ext) for ext in README_EXTENSIONS)4344def run(self):45self.run_command('egg_info')46ei_cmd = self.get_finalized_command('egg_info')47self.filelist = ei_cmd.filelist48self.filelist.append(os.path.join(ei_cmd.egg_info, 'SOURCES.txt'))49self.check_readme()5051# Run sub commands52for cmd_name in self.get_sub_commands():53self.run_command(cmd_name)5455self.make_distribution()5657dist_files = getattr(self.distribution, 'dist_files', [])58for file in self.archive_files:59data = ('sdist', '', file)60if data not in dist_files:61dist_files.append(data)6263def initialize_options(self):64orig.sdist.initialize_options(self)6566self._default_to_gztar()6768def _default_to_gztar(self):69# only needed on Python prior to 3.6.70if sys.version_info >= (3, 6, 0, 'beta', 1):71return72self.formats = ['gztar']7374def make_distribution(self):75"""76Workaround for #51677"""78with self._remove_os_link():79orig.sdist.make_distribution(self)8081@staticmethod82@contextlib.contextmanager83def _remove_os_link():84"""85In a context, remove and restore os.link if it exists86"""8788class NoValue:89pass9091orig_val = getattr(os, 'link', NoValue)92try:93del os.link94except Exception:95pass96try:97yield98finally:99if orig_val is not NoValue:100setattr(os, 'link', orig_val)101102def _add_defaults_optional(self):103super()._add_defaults_optional()104if os.path.isfile('pyproject.toml'):105self.filelist.append('pyproject.toml')106107def _add_defaults_python(self):108"""getting python files"""109if self.distribution.has_pure_modules():110build_py = self.get_finalized_command('build_py')111self.filelist.extend(build_py.get_source_files())112self._add_data_files(self._safe_data_files(build_py))113114def _safe_data_files(self, build_py):115"""116Since the ``sdist`` class is also used to compute the MANIFEST117(via :obj:`setuptools.command.egg_info.manifest_maker`),118there might be recursion problems when trying to obtain the list of119data_files and ``include_package_data=True`` (which in turn depends on120the files included in the MANIFEST).121122To avoid that, ``manifest_maker`` should be able to overwrite this123method and avoid recursive attempts to build/analyze the MANIFEST.124"""125return build_py.data_files126127def _add_data_files(self, data_files):128"""129Add data files as found in build_py.data_files.130"""131self.filelist.extend(132os.path.join(src_dir, name)133for _, src_dir, _, filenames in data_files134for name in filenames135)136137def _add_defaults_data_files(self):138try:139super()._add_defaults_data_files()140except TypeError:141log.warn("data_files contains unexpected objects")142143def check_readme(self):144for f in self.READMES:145if os.path.exists(f):146return147else:148self.warn(149"standard file not found: should have one of " +150', '.join(self.READMES)151)152153def make_release_tree(self, base_dir, files):154orig.sdist.make_release_tree(self, base_dir, files)155156# Save any egg_info command line options used to create this sdist157dest = os.path.join(base_dir, 'setup.cfg')158if hasattr(os, 'link') and os.path.exists(dest):159# unlink and re-copy, since it might be hard-linked, and160# we don't want to change the source version161os.unlink(dest)162self.copy_file('setup.cfg', dest)163164self.get_finalized_command('egg_info').save_version_info(dest)165166def _manifest_is_not_generated(self):167# check for special comment used in 2.7.1 and higher168if not os.path.isfile(self.manifest):169return False170171with io.open(self.manifest, 'rb') as fp:172first_line = fp.readline()173return (first_line !=174'# file GENERATED by distutils, do NOT edit\n'.encode())175176def read_manifest(self):177"""Read the manifest file (named by 'self.manifest') and use it to178fill in 'self.filelist', the list of files to include in the source179distribution.180"""181log.info("reading manifest file '%s'", self.manifest)182manifest = open(self.manifest, 'rb')183for line in manifest:184# The manifest must contain UTF-8. See #303.185try:186line = line.decode('UTF-8')187except UnicodeDecodeError:188log.warn("%r not UTF-8 decodable -- skipping" % line)189continue190# ignore comments and blank lines191line = line.strip()192if line.startswith('#') or not line:193continue194self.filelist.append(line)195manifest.close()196197198