py36compat.py 4.8 KB

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