py36compat.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import os
  2. from glob import glob
  3. from distutils.util import convert_path
  4. from distutils.command import sdist
  5. from setuptools.extern.six.moves import filter
  6. class sdist_add_defaults:
  7. """
  8. Mix-in providing forward-compatibility for functionality as found in
  9. distutils on Python 3.7.
  10. Do not edit the code in this class except to update functionality
  11. as implemented in distutils. Instead, override in the subclass.
  12. """
  13. def add_defaults(self):
  14. """Add all the default files to self.filelist:
  15. - README or README.txt
  16. - setup.py
  17. - test/test*.py
  18. - all pure Python modules mentioned in setup script
  19. - all files pointed by package_data (build_py)
  20. - all files defined in data_files.
  21. - all files defined as scripts.
  22. - all C sources listed as part of extensions or C libraries
  23. in the setup script (doesn't catch C headers!)
  24. Warns if (README or README.txt) or setup.py are missing; everything
  25. else is optional.
  26. """
  27. self._add_defaults_standards()
  28. self._add_defaults_optional()
  29. self._add_defaults_python()
  30. self._add_defaults_data_files()
  31. self._add_defaults_ext()
  32. self._add_defaults_c_libs()
  33. self._add_defaults_scripts()
  34. @staticmethod
  35. def _cs_path_exists(fspath):
  36. """
  37. Case-sensitive path existence check
  38. >>> sdist_add_defaults._cs_path_exists(__file__)
  39. True
  40. >>> sdist_add_defaults._cs_path_exists(__file__.upper())
  41. False
  42. """
  43. if not os.path.exists(fspath):
  44. return False
  45. # make absolute so we always have a directory
  46. abspath = os.path.abspath(fspath)
  47. directory, filename = os.path.split(abspath)
  48. return filename in os.listdir(directory)
  49. def _add_defaults_standards(self):
  50. standards = [self.READMES, self.distribution.script_name]
  51. for fn in standards:
  52. if isinstance(fn, tuple):
  53. alts = fn
  54. got_it = False
  55. for fn in alts:
  56. if self._cs_path_exists(fn):
  57. got_it = True
  58. self.filelist.append(fn)
  59. break
  60. if not got_it:
  61. self.warn("standard file not found: should have one of " +
  62. ', '.join(alts))
  63. else:
  64. if self._cs_path_exists(fn):
  65. self.filelist.append(fn)
  66. else:
  67. self.warn("standard file '%s' not found" % fn)
  68. def _add_defaults_optional(self):
  69. optional = ['test/test*.py', 'setup.cfg']
  70. for pattern in optional:
  71. files = filter(os.path.isfile, glob(pattern))
  72. self.filelist.extend(files)
  73. def _add_defaults_python(self):
  74. # build_py is used to get:
  75. # - python modules
  76. # - files defined in package_data
  77. build_py = self.get_finalized_command('build_py')
  78. # getting python files
  79. if self.distribution.has_pure_modules():
  80. self.filelist.extend(build_py.get_source_files())
  81. # getting package_data files
  82. # (computed in build_py.data_files by build_py.finalize_options)
  83. for pkg, src_dir, build_dir, filenames in build_py.data_files:
  84. for filename in filenames:
  85. self.filelist.append(os.path.join(src_dir, filename))
  86. def _add_defaults_data_files(self):
  87. # getting distribution.data_files
  88. if self.distribution.has_data_files():
  89. for item in self.distribution.data_files:
  90. if isinstance(item, str):
  91. # plain file
  92. item = convert_path(item)
  93. if os.path.isfile(item):
  94. self.filelist.append(item)
  95. else:
  96. # a (dirname, filenames) tuple
  97. dirname, filenames = item
  98. for f in filenames:
  99. f = convert_path(f)
  100. if os.path.isfile(f):
  101. self.filelist.append(f)
  102. def _add_defaults_ext(self):
  103. if self.distribution.has_ext_modules():
  104. build_ext = self.get_finalized_command('build_ext')
  105. self.filelist.extend(build_ext.get_source_files())
  106. def _add_defaults_c_libs(self):
  107. if self.distribution.has_c_libraries():
  108. build_clib = self.get_finalized_command('build_clib')
  109. self.filelist.extend(build_clib.get_source_files())
  110. def _add_defaults_scripts(self):
  111. if self.distribution.has_scripts():
  112. build_scripts = self.get_finalized_command('build_scripts')
  113. self.filelist.extend(build_scripts.get_source_files())
  114. if hasattr(sdist.sdist, '_add_defaults_standards'):
  115. # disable the functionality already available upstream
  116. class sdist_add_defaults: # noqa
  117. pass