OLD | NEW |
(Empty) | |
| 1 from distutils import log |
| 2 import distutils.command.sdist as orig |
| 3 import os |
| 4 import sys |
| 5 import io |
| 6 import contextlib |
| 7 |
| 8 import six |
| 9 |
| 10 from .py36compat import sdist_add_defaults |
| 11 |
| 12 import pkg_resources |
| 13 |
| 14 _default_revctrl = list |
| 15 |
| 16 |
| 17 def walk_revctrl(dirname=''): |
| 18 """Find all files under revision control""" |
| 19 for ep in pkg_resources.iter_entry_points('setuptools.file_finders'): |
| 20 for item in ep.load()(dirname): |
| 21 yield item |
| 22 |
| 23 |
| 24 class sdist(sdist_add_defaults, orig.sdist): |
| 25 """Smart sdist that finds anything supported by revision control""" |
| 26 |
| 27 user_options = [ |
| 28 ('formats=', None, |
| 29 "formats for source distribution (comma-separated list)"), |
| 30 ('keep-temp', 'k', |
| 31 "keep the distribution tree around after creating " + |
| 32 "archive file(s)"), |
| 33 ('dist-dir=', 'd', |
| 34 "directory to put the source distribution archive(s) in " |
| 35 "[default: dist]"), |
| 36 ] |
| 37 |
| 38 negative_opt = {} |
| 39 |
| 40 READMES = 'README', 'README.rst', 'README.txt' |
| 41 |
| 42 def run(self): |
| 43 self.run_command('egg_info') |
| 44 ei_cmd = self.get_finalized_command('egg_info') |
| 45 self.filelist = ei_cmd.filelist |
| 46 self.filelist.append(os.path.join(ei_cmd.egg_info, 'SOURCES.txt')) |
| 47 self.check_readme() |
| 48 |
| 49 # Run sub commands |
| 50 for cmd_name in self.get_sub_commands(): |
| 51 self.run_command(cmd_name) |
| 52 |
| 53 # Call check_metadata only if no 'check' command |
| 54 # (distutils <= 2.6) |
| 55 import distutils.command |
| 56 |
| 57 if 'check' not in distutils.command.__all__: |
| 58 self.check_metadata() |
| 59 |
| 60 self.make_distribution() |
| 61 |
| 62 dist_files = getattr(self.distribution, 'dist_files', []) |
| 63 for file in self.archive_files: |
| 64 data = ('sdist', '', file) |
| 65 if data not in dist_files: |
| 66 dist_files.append(data) |
| 67 |
| 68 def initialize_options(self): |
| 69 orig.sdist.initialize_options(self) |
| 70 |
| 71 self._default_to_gztar() |
| 72 |
| 73 def _default_to_gztar(self): |
| 74 # only needed on Python prior to 3.6. |
| 75 if sys.version_info >= (3, 6, 0, 'beta', 1): |
| 76 return |
| 77 self.formats = ['gztar'] |
| 78 |
| 79 def make_distribution(self): |
| 80 """ |
| 81 Workaround for #516 |
| 82 """ |
| 83 with self._remove_os_link(): |
| 84 orig.sdist.make_distribution(self) |
| 85 |
| 86 @staticmethod |
| 87 @contextlib.contextmanager |
| 88 def _remove_os_link(): |
| 89 """ |
| 90 In a context, remove and restore os.link if it exists |
| 91 """ |
| 92 |
| 93 class NoValue: |
| 94 pass |
| 95 |
| 96 orig_val = getattr(os, 'link', NoValue) |
| 97 try: |
| 98 del os.link |
| 99 except Exception: |
| 100 pass |
| 101 try: |
| 102 yield |
| 103 finally: |
| 104 if orig_val is not NoValue: |
| 105 setattr(os, 'link', orig_val) |
| 106 |
| 107 def __read_template_hack(self): |
| 108 # This grody hack closes the template file (MANIFEST.in) if an |
| 109 # exception occurs during read_template. |
| 110 # Doing so prevents an error when easy_install attempts to delete the |
| 111 # file. |
| 112 try: |
| 113 orig.sdist.read_template(self) |
| 114 except Exception: |
| 115 _, _, tb = sys.exc_info() |
| 116 tb.tb_next.tb_frame.f_locals['template'].close() |
| 117 raise |
| 118 |
| 119 # Beginning with Python 2.7.2, 3.1.4, and 3.2.1, this leaky file handle |
| 120 # has been fixed, so only override the method if we're using an earlier |
| 121 # Python. |
| 122 has_leaky_handle = ( |
| 123 sys.version_info < (2, 7, 2) |
| 124 or (3, 0) <= sys.version_info < (3, 1, 4) |
| 125 or (3, 2) <= sys.version_info < (3, 2, 1) |
| 126 ) |
| 127 if has_leaky_handle: |
| 128 read_template = __read_template_hack |
| 129 |
| 130 def _add_defaults_python(self): |
| 131 """getting python files""" |
| 132 if self.distribution.has_pure_modules(): |
| 133 build_py = self.get_finalized_command('build_py') |
| 134 self.filelist.extend(build_py.get_source_files()) |
| 135 # This functionality is incompatible with include_package_data, and |
| 136 # will in fact create an infinite recursion if include_package_data |
| 137 # is True. Use of include_package_data will imply that |
| 138 # distutils-style automatic handling of package_data is disabled |
| 139 if not self.distribution.include_package_data: |
| 140 for _, src_dir, _, filenames in build_py.data_files: |
| 141 self.filelist.extend([os.path.join(src_dir, filename) |
| 142 for filename in filenames]) |
| 143 |
| 144 def _add_defaults_data_files(self): |
| 145 try: |
| 146 if six.PY2: |
| 147 sdist_add_defaults._add_defaults_data_files(self) |
| 148 else: |
| 149 super()._add_defaults_data_files() |
| 150 except TypeError: |
| 151 log.warn("data_files contains unexpected objects") |
| 152 |
| 153 def check_readme(self): |
| 154 for f in self.READMES: |
| 155 if os.path.exists(f): |
| 156 return |
| 157 else: |
| 158 self.warn( |
| 159 "standard file not found: should have one of " + |
| 160 ', '.join(self.READMES) |
| 161 ) |
| 162 |
| 163 def make_release_tree(self, base_dir, files): |
| 164 orig.sdist.make_release_tree(self, base_dir, files) |
| 165 |
| 166 # Save any egg_info command line options used to create this sdist |
| 167 dest = os.path.join(base_dir, 'setup.cfg') |
| 168 if hasattr(os, 'link') and os.path.exists(dest): |
| 169 # unlink and re-copy, since it might be hard-linked, and |
| 170 # we don't want to change the source version |
| 171 os.unlink(dest) |
| 172 self.copy_file('setup.cfg', dest) |
| 173 |
| 174 self.get_finalized_command('egg_info').save_version_info(dest) |
| 175 |
| 176 def _manifest_is_not_generated(self): |
| 177 # check for special comment used in 2.7.1 and higher |
| 178 if not os.path.isfile(self.manifest): |
| 179 return False |
| 180 |
| 181 with io.open(self.manifest, 'rb') as fp: |
| 182 first_line = fp.readline() |
| 183 return (first_line != |
| 184 '# file GENERATED by distutils, do NOT edit\n'.encode()) |
| 185 |
| 186 def read_manifest(self): |
| 187 """Read the manifest file (named by 'self.manifest') and use it to |
| 188 fill in 'self.filelist', the list of files to include in the source |
| 189 distribution. |
| 190 """ |
| 191 log.info("reading manifest file '%s'", self.manifest) |
| 192 manifest = open(self.manifest, 'rb') |
| 193 for line in manifest: |
| 194 # The manifest must contain UTF-8. See #303. |
| 195 if six.PY3: |
| 196 try: |
| 197 line = line.decode('UTF-8') |
| 198 except UnicodeDecodeError: |
| 199 log.warn("%r not UTF-8 decodable -- skipping" % line) |
| 200 continue |
| 201 # ignore comments and blank lines |
| 202 line = line.strip() |
| 203 if line.startswith('#') or not line: |
| 204 continue |
| 205 self.filelist.append(line) |
| 206 manifest.close() |
OLD | NEW |