| 1 | n/a | """Create a source distribution.""" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | import os |
|---|
| 4 | n/a | import re |
|---|
| 5 | n/a | import sys |
|---|
| 6 | n/a | from io import StringIO |
|---|
| 7 | n/a | from shutil import get_archive_formats, rmtree |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | from packaging import logger |
|---|
| 10 | n/a | from packaging.util import resolve_name |
|---|
| 11 | n/a | from packaging.errors import (PackagingPlatformError, PackagingOptionError, |
|---|
| 12 | n/a | PackagingModuleError, PackagingFileError) |
|---|
| 13 | n/a | from packaging.command import get_command_names |
|---|
| 14 | n/a | from packaging.command.cmd import Command |
|---|
| 15 | n/a | from packaging.manifest import Manifest |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | def show_formats(): |
|---|
| 19 | n/a | """Print all possible values for the 'formats' option (used by |
|---|
| 20 | n/a | the "--help-formats" command-line option). |
|---|
| 21 | n/a | """ |
|---|
| 22 | n/a | from packaging.fancy_getopt import FancyGetopt |
|---|
| 23 | n/a | formats = sorted(('formats=' + name, None, desc) |
|---|
| 24 | n/a | for name, desc in get_archive_formats()) |
|---|
| 25 | n/a | FancyGetopt(formats).print_help( |
|---|
| 26 | n/a | "List of available source distribution formats:") |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | # a \ followed by some spaces + EOL |
|---|
| 29 | n/a | _COLLAPSE_PATTERN = re.compile('\\\w\n', re.M) |
|---|
| 30 | n/a | _COMMENTED_LINE = re.compile('^#.*\n$|^\w*\n$', re.M) |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | class sdist(Command): |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | description = "create a source distribution (tarball, zip file, etc.)" |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | user_options = [ |
|---|
| 38 | n/a | ('manifest=', 'm', |
|---|
| 39 | n/a | "name of manifest file [default: MANIFEST]"), |
|---|
| 40 | n/a | ('use-defaults', None, |
|---|
| 41 | n/a | "include the default file set in the manifest " |
|---|
| 42 | n/a | "[default; disable with --no-defaults]"), |
|---|
| 43 | n/a | ('no-defaults', None, |
|---|
| 44 | n/a | "don't include the default file set"), |
|---|
| 45 | n/a | ('prune', None, |
|---|
| 46 | n/a | "specifically exclude files/directories that should not be " |
|---|
| 47 | n/a | "distributed (build tree, RCS/CVS dirs, etc.) " |
|---|
| 48 | n/a | "[default; disable with --no-prune]"), |
|---|
| 49 | n/a | ('no-prune', None, |
|---|
| 50 | n/a | "don't automatically exclude anything"), |
|---|
| 51 | n/a | ('manifest-only', 'o', |
|---|
| 52 | n/a | "just regenerate the manifest and then stop "), |
|---|
| 53 | n/a | ('formats=', None, |
|---|
| 54 | n/a | "formats for source distribution (comma-separated list)"), |
|---|
| 55 | n/a | ('keep-temp', 'k', |
|---|
| 56 | n/a | "keep the distribution tree around after creating " + |
|---|
| 57 | n/a | "archive file(s)"), |
|---|
| 58 | n/a | ('dist-dir=', 'd', |
|---|
| 59 | n/a | "directory to put the source distribution archive(s) in " |
|---|
| 60 | n/a | "[default: dist]"), |
|---|
| 61 | n/a | ('check-metadata', None, |
|---|
| 62 | n/a | "Ensure that all required elements of metadata " |
|---|
| 63 | n/a | "are supplied. Warn if any missing. [default]"), |
|---|
| 64 | n/a | ('owner=', 'u', |
|---|
| 65 | n/a | "Owner name used when creating a tar file [default: current user]"), |
|---|
| 66 | n/a | ('group=', 'g', |
|---|
| 67 | n/a | "Group name used when creating a tar file [default: current group]"), |
|---|
| 68 | n/a | ('manifest-builders=', None, |
|---|
| 69 | n/a | "manifest builders (comma-separated list)"), |
|---|
| 70 | n/a | ] |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | boolean_options = ['use-defaults', 'prune', |
|---|
| 73 | n/a | 'manifest-only', 'keep-temp', 'check-metadata'] |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | help_options = [ |
|---|
| 76 | n/a | ('help-formats', None, |
|---|
| 77 | n/a | "list available distribution formats", show_formats), |
|---|
| 78 | n/a | ] |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | negative_opt = {'no-defaults': 'use-defaults', |
|---|
| 81 | n/a | 'no-prune': 'prune'} |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | default_format = {'posix': 'gztar', |
|---|
| 84 | n/a | 'nt': 'zip'} |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | def initialize_options(self): |
|---|
| 87 | n/a | self.manifest = None |
|---|
| 88 | n/a | # 'use_defaults': if true, we will include the default file set |
|---|
| 89 | n/a | # in the manifest |
|---|
| 90 | n/a | self.use_defaults = True |
|---|
| 91 | n/a | self.prune = True |
|---|
| 92 | n/a | self.manifest_only = False |
|---|
| 93 | n/a | self.formats = None |
|---|
| 94 | n/a | self.keep_temp = False |
|---|
| 95 | n/a | self.dist_dir = None |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | self.archive_files = None |
|---|
| 98 | n/a | self.metadata_check = True |
|---|
| 99 | n/a | self.owner = None |
|---|
| 100 | n/a | self.group = None |
|---|
| 101 | n/a | self.filelist = None |
|---|
| 102 | n/a | self.manifest_builders = None |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | def _check_archive_formats(self, formats): |
|---|
| 105 | n/a | supported_formats = [name for name, desc in get_archive_formats()] |
|---|
| 106 | n/a | for format in formats: |
|---|
| 107 | n/a | if format not in supported_formats: |
|---|
| 108 | n/a | return format |
|---|
| 109 | n/a | return None |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | def finalize_options(self): |
|---|
| 112 | n/a | if self.manifest is None: |
|---|
| 113 | n/a | self.manifest = "MANIFEST" |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | self.ensure_string_list('formats') |
|---|
| 116 | n/a | if self.formats is None: |
|---|
| 117 | n/a | try: |
|---|
| 118 | n/a | self.formats = [self.default_format[os.name]] |
|---|
| 119 | n/a | except KeyError: |
|---|
| 120 | n/a | raise PackagingPlatformError("don't know how to create source " |
|---|
| 121 | n/a | "distributions on platform %s" % os.name) |
|---|
| 122 | n/a | |
|---|
| 123 | n/a | bad_format = self._check_archive_formats(self.formats) |
|---|
| 124 | n/a | if bad_format: |
|---|
| 125 | n/a | raise PackagingOptionError("unknown archive format '%s'" \ |
|---|
| 126 | n/a | % bad_format) |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | if self.dist_dir is None: |
|---|
| 129 | n/a | self.dist_dir = "dist" |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | if self.filelist is None: |
|---|
| 132 | n/a | self.filelist = Manifest() |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | if self.manifest_builders is None: |
|---|
| 135 | n/a | self.manifest_builders = [] |
|---|
| 136 | n/a | else: |
|---|
| 137 | n/a | if isinstance(self.manifest_builders, str): |
|---|
| 138 | n/a | self.manifest_builders = self.manifest_builders.split(',') |
|---|
| 139 | n/a | builders = [] |
|---|
| 140 | n/a | for builder in self.manifest_builders: |
|---|
| 141 | n/a | builder = builder.strip() |
|---|
| 142 | n/a | if builder == '': |
|---|
| 143 | n/a | continue |
|---|
| 144 | n/a | try: |
|---|
| 145 | n/a | builder = resolve_name(builder) |
|---|
| 146 | n/a | except ImportError as e: |
|---|
| 147 | n/a | raise PackagingModuleError(e) |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | builders.append(builder) |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | self.manifest_builders = builders |
|---|
| 152 | n/a | |
|---|
| 153 | n/a | def run(self): |
|---|
| 154 | n/a | # 'filelist' contains the list of files that will make up the |
|---|
| 155 | n/a | # manifest |
|---|
| 156 | n/a | self.filelist.clear() |
|---|
| 157 | n/a | |
|---|
| 158 | n/a | # Check the package metadata |
|---|
| 159 | n/a | if self.metadata_check: |
|---|
| 160 | n/a | self.run_command('check') |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | # Do whatever it takes to get the list of files to process |
|---|
| 163 | n/a | # (process the manifest template, read an existing manifest, |
|---|
| 164 | n/a | # whatever). File list is accumulated in 'self.filelist'. |
|---|
| 165 | n/a | self.get_file_list() |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | # If user just wanted us to regenerate the manifest, stop now. |
|---|
| 168 | n/a | if self.manifest_only: |
|---|
| 169 | n/a | return |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | # Otherwise, go ahead and create the source distribution tarball, |
|---|
| 172 | n/a | # or zipfile, or whatever. |
|---|
| 173 | n/a | self.make_distribution() |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | def get_file_list(self): |
|---|
| 176 | n/a | """Figure out the list of files to include in the source |
|---|
| 177 | n/a | distribution, and put it in 'self.filelist'. This might involve |
|---|
| 178 | n/a | reading the manifest template (and writing the manifest), or just |
|---|
| 179 | n/a | reading the manifest, or just using the default file set -- it all |
|---|
| 180 | n/a | depends on the user's options. |
|---|
| 181 | n/a | """ |
|---|
| 182 | n/a | template_exists = len(self.distribution.extra_files) > 0 |
|---|
| 183 | n/a | if not template_exists: |
|---|
| 184 | n/a | logger.warning('%s: using default file list', |
|---|
| 185 | n/a | self.get_command_name()) |
|---|
| 186 | n/a | self.filelist.findall() |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | if self.use_defaults: |
|---|
| 189 | n/a | self.add_defaults() |
|---|
| 190 | n/a | if template_exists: |
|---|
| 191 | n/a | template = '\n'.join(self.distribution.extra_files) |
|---|
| 192 | n/a | self.filelist.read_template(StringIO(template)) |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | # call manifest builders, if any. |
|---|
| 195 | n/a | for builder in self.manifest_builders: |
|---|
| 196 | n/a | builder(self.distribution, self.filelist) |
|---|
| 197 | n/a | |
|---|
| 198 | n/a | if self.prune: |
|---|
| 199 | n/a | self.prune_file_list() |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | self.filelist.write(self.manifest) |
|---|
| 202 | n/a | |
|---|
| 203 | n/a | def add_defaults(self): |
|---|
| 204 | n/a | """Add all default files to self.filelist. |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | In addition to the setup.cfg file, this will include all files returned |
|---|
| 207 | n/a | by the get_source_files of every registered command. This will find |
|---|
| 208 | n/a | Python modules and packages, data files listed in package_data_, |
|---|
| 209 | n/a | data_files and extra_files, scripts, C sources of extension modules or |
|---|
| 210 | n/a | C libraries (headers are missing). |
|---|
| 211 | n/a | """ |
|---|
| 212 | n/a | if os.path.exists('setup.cfg'): |
|---|
| 213 | n/a | self.filelist.append('setup.cfg') |
|---|
| 214 | n/a | else: |
|---|
| 215 | n/a | logger.warning("%s: standard 'setup.cfg' file not found", |
|---|
| 216 | n/a | self.get_command_name()) |
|---|
| 217 | n/a | |
|---|
| 218 | n/a | for cmd_name in get_command_names(): |
|---|
| 219 | n/a | try: |
|---|
| 220 | n/a | cmd_obj = self.get_finalized_command(cmd_name) |
|---|
| 221 | n/a | except PackagingOptionError: |
|---|
| 222 | n/a | pass |
|---|
| 223 | n/a | else: |
|---|
| 224 | n/a | self.filelist.extend(cmd_obj.get_source_files()) |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | def prune_file_list(self): |
|---|
| 227 | n/a | """Prune off branches that might slip into the file list as created |
|---|
| 228 | n/a | by 'read_template()', but really don't belong there: |
|---|
| 229 | n/a | * the build tree (typically "build") |
|---|
| 230 | n/a | * the release tree itself (only an issue if we ran "sdist" |
|---|
| 231 | n/a | previously with --keep-temp, or it aborted) |
|---|
| 232 | n/a | * any RCS, CVS, .svn, .hg, .git, .bzr, _darcs directories |
|---|
| 233 | n/a | """ |
|---|
| 234 | n/a | build = self.get_finalized_command('build') |
|---|
| 235 | n/a | base_dir = self.distribution.get_fullname() |
|---|
| 236 | n/a | |
|---|
| 237 | n/a | self.filelist.exclude_pattern(None, prefix=build.build_base) |
|---|
| 238 | n/a | self.filelist.exclude_pattern(None, prefix=base_dir) |
|---|
| 239 | n/a | |
|---|
| 240 | n/a | # pruning out vcs directories |
|---|
| 241 | n/a | # both separators are used under win32 |
|---|
| 242 | n/a | if sys.platform == 'win32': |
|---|
| 243 | n/a | seps = r'/|\\' |
|---|
| 244 | n/a | else: |
|---|
| 245 | n/a | seps = '/' |
|---|
| 246 | n/a | |
|---|
| 247 | n/a | vcs_dirs = ['RCS', 'CVS', r'\.svn', r'\.hg', r'\.git', r'\.bzr', |
|---|
| 248 | n/a | '_darcs'] |
|---|
| 249 | n/a | vcs_ptrn = r'(^|%s)(%s)(%s).*' % (seps, '|'.join(vcs_dirs), seps) |
|---|
| 250 | n/a | self.filelist.exclude_pattern(vcs_ptrn, is_regex=True) |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | def make_release_tree(self, base_dir, files): |
|---|
| 253 | n/a | """Create the directory tree that will become the source |
|---|
| 254 | n/a | distribution archive. All directories implied by the filenames in |
|---|
| 255 | n/a | 'files' are created under 'base_dir', and then we hard link or copy |
|---|
| 256 | n/a | (if hard linking is unavailable) those files into place. |
|---|
| 257 | n/a | Essentially, this duplicates the developer's source tree, but in a |
|---|
| 258 | n/a | directory named after the distribution, containing only the files |
|---|
| 259 | n/a | to be distributed. |
|---|
| 260 | n/a | """ |
|---|
| 261 | n/a | # Create all the directories under 'base_dir' necessary to |
|---|
| 262 | n/a | # put 'files' there; the 'mkpath()' is just so we don't die |
|---|
| 263 | n/a | # if the manifest happens to be empty. |
|---|
| 264 | n/a | self.mkpath(base_dir) |
|---|
| 265 | n/a | self.create_tree(base_dir, files, dry_run=self.dry_run) |
|---|
| 266 | n/a | |
|---|
| 267 | n/a | # And walk over the list of files, either making a hard link (if |
|---|
| 268 | n/a | # os.link exists) to each one that doesn't already exist in its |
|---|
| 269 | n/a | # corresponding location under 'base_dir', or copying each file |
|---|
| 270 | n/a | # that's out-of-date in 'base_dir'. (Usually, all files will be |
|---|
| 271 | n/a | # out-of-date, because by default we blow away 'base_dir' when |
|---|
| 272 | n/a | # we're done making the distribution archives.) |
|---|
| 273 | n/a | |
|---|
| 274 | n/a | if hasattr(os, 'link'): # can make hard links on this system |
|---|
| 275 | n/a | link = 'hard' |
|---|
| 276 | n/a | msg = "making hard links in %s..." % base_dir |
|---|
| 277 | n/a | else: # nope, have to copy |
|---|
| 278 | n/a | link = None |
|---|
| 279 | n/a | msg = "copying files to %s..." % base_dir |
|---|
| 280 | n/a | |
|---|
| 281 | n/a | if not files: |
|---|
| 282 | n/a | logger.warning("no files to distribute -- empty manifest?") |
|---|
| 283 | n/a | else: |
|---|
| 284 | n/a | logger.info(msg) |
|---|
| 285 | n/a | |
|---|
| 286 | n/a | for file in self.distribution.metadata.requires_files: |
|---|
| 287 | n/a | if file not in files: |
|---|
| 288 | n/a | msg = "'%s' must be included explicitly in 'extra_files'" \ |
|---|
| 289 | n/a | % file |
|---|
| 290 | n/a | raise PackagingFileError(msg) |
|---|
| 291 | n/a | |
|---|
| 292 | n/a | for file in files: |
|---|
| 293 | n/a | if not os.path.isfile(file): |
|---|
| 294 | n/a | logger.warning("'%s' not a regular file -- skipping", file) |
|---|
| 295 | n/a | else: |
|---|
| 296 | n/a | dest = os.path.join(base_dir, file) |
|---|
| 297 | n/a | self.copy_file(file, dest, link=link) |
|---|
| 298 | n/a | |
|---|
| 299 | n/a | self.distribution.metadata.write(os.path.join(base_dir, 'PKG-INFO')) |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | def make_distribution(self): |
|---|
| 302 | n/a | """Create the source distribution(s). First, we create the release |
|---|
| 303 | n/a | tree with 'make_release_tree()'; then, we create all required |
|---|
| 304 | n/a | archive files (according to 'self.formats') from the release tree. |
|---|
| 305 | n/a | Finally, we clean up by blowing away the release tree (unless |
|---|
| 306 | n/a | 'self.keep_temp' is true). The list of archive files created is |
|---|
| 307 | n/a | stored so it can be retrieved later by 'get_archive_files()'. |
|---|
| 308 | n/a | """ |
|---|
| 309 | n/a | # Don't warn about missing metadata here -- should be (and is!) |
|---|
| 310 | n/a | # done elsewhere. |
|---|
| 311 | n/a | base_dir = self.distribution.get_fullname() |
|---|
| 312 | n/a | base_name = os.path.join(self.dist_dir, base_dir) |
|---|
| 313 | n/a | |
|---|
| 314 | n/a | self.make_release_tree(base_dir, self.filelist.files) |
|---|
| 315 | n/a | archive_files = [] # remember names of files we create |
|---|
| 316 | n/a | # tar archive must be created last to avoid overwrite and remove |
|---|
| 317 | n/a | if 'tar' in self.formats: |
|---|
| 318 | n/a | self.formats.append(self.formats.pop(self.formats.index('tar'))) |
|---|
| 319 | n/a | |
|---|
| 320 | n/a | for fmt in self.formats: |
|---|
| 321 | n/a | file = self.make_archive(base_name, fmt, base_dir=base_dir, |
|---|
| 322 | n/a | owner=self.owner, group=self.group) |
|---|
| 323 | n/a | archive_files.append(file) |
|---|
| 324 | n/a | self.distribution.dist_files.append(('sdist', '', file)) |
|---|
| 325 | n/a | |
|---|
| 326 | n/a | self.archive_files = archive_files |
|---|
| 327 | n/a | |
|---|
| 328 | n/a | if not self.keep_temp: |
|---|
| 329 | n/a | if self.dry_run: |
|---|
| 330 | n/a | logger.info('removing %s', base_dir) |
|---|
| 331 | n/a | else: |
|---|
| 332 | n/a | rmtree(base_dir) |
|---|
| 333 | n/a | |
|---|
| 334 | n/a | def get_archive_files(self): |
|---|
| 335 | n/a | """Return the list of archive files created when the command |
|---|
| 336 | n/a | was run, or None if the command hasn't run yet. |
|---|
| 337 | n/a | """ |
|---|
| 338 | n/a | return self.archive_files |
|---|
| 339 | n/a | |
|---|
| 340 | n/a | def create_tree(self, base_dir, files, mode=0o777, dry_run=False): |
|---|
| 341 | n/a | need_dir = set() |
|---|
| 342 | n/a | for file in files: |
|---|
| 343 | n/a | need_dir.add(os.path.join(base_dir, os.path.dirname(file))) |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | # Now create them |
|---|
| 346 | n/a | for dir in sorted(need_dir): |
|---|
| 347 | n/a | self.mkpath(dir, mode, dry_run=dry_run) |
|---|