codekingpro/portable-devtools
115k
1"""distutils.command.bdist2 3Implements the Distutils 'bdist' command (create a built [binary]4distribution)."""5 6from __future__ import annotations7 8import os9import warnings10from collections.abc import Callable11from typing import TYPE_CHECKING, ClassVar12 13from ..core import Command14from ..errors import DistutilsOptionError, DistutilsPlatformError15from ..util import get_platform16 17if TYPE_CHECKING:18 from typing_extensions import deprecated19else:20 21 def deprecated(message):22 return lambda fn: fn23 24 25def show_formats():26 """Print list of available formats (arguments to "--format" option)."""27 from ..fancy_getopt import FancyGetopt28 29 formats = [30 ("formats=" + format, None, bdist.format_commands[format][1])31 for format in bdist.format_commands32 ]33 pretty_printer = FancyGetopt(formats)34 pretty_printer.print_help("List of available distribution formats:")35 36 37class ListCompat(dict[str, tuple[str, str]]):38 # adapter to allow for Setuptools compatibility in format_commands39 @deprecated("format_commands is now a dict. append is deprecated.")40 def append(self, item: object) -> None:41 warnings.warn(42 "format_commands is now a dict. append is deprecated.",43 DeprecationWarning,44 stacklevel=2,45 )46 47 48class bdist(Command):49 description = "create a built (binary) distribution"50 51 user_options = [52 ('bdist-base=', 'b', "temporary directory for creating built distributions"),53 (54 'plat-name=',55 'p',56 "platform name to embed in generated filenames "57 f"[default: {get_platform()}]",58 ),59 ('formats=', None, "formats for distribution (comma-separated list)"),60 (61 'dist-dir=',62 'd',63 "directory to put final built distributions in [default: dist]",64 ),65 ('skip-build', None, "skip rebuilding everything (for testing/debugging)"),66 (67 'owner=',68 'u',69 "Owner name used when creating a tar file [default: current user]",70 ),71 (72 'group=',73 'g',74 "Group name used when creating a tar file [default: current group]",75 ),76 ]77 78 boolean_options: ClassVar[list[str]] = ['skip-build']79 80 help_options: ClassVar[list[tuple[str, str | None, str, Callable[[], object]]]] = [81 ('help-formats', None, "lists available distribution formats", show_formats),82 ]83 84 # The following commands do not take a format option from bdist85 no_format_option: ClassVar[tuple[str, ...]] = ('bdist_rpm',)86 87 # This won't do in reality: will need to distinguish RPM-ish Linux,88 # Debian-ish Linux, Solaris, FreeBSD, ..., Windows, Mac OS.89 default_format: ClassVar[dict[str, str]] = {'posix': 'gztar', 'nt': 'zip'}90 91 # Define commands in preferred order for the --help-formats option92 format_commands = ListCompat({93 'rpm': ('bdist_rpm', "RPM distribution"),94 'gztar': ('bdist_dumb', "gzip'ed tar file"),95 'bztar': ('bdist_dumb', "bzip2'ed tar file"),96 'xztar': ('bdist_dumb', "xz'ed tar file"),97 'ztar': ('bdist_dumb', "compressed tar file"),98 'tar': ('bdist_dumb', "tar file"),99 'zip': ('bdist_dumb', "ZIP file"),100 })101 102 # for compatibility until consumers only reference format_commands103 format_command = format_commands104 105 def initialize_options(self):106 self.bdist_base = None107 self.plat_name = None108 self.formats = None109 self.dist_dir = None110 self.skip_build = False111 self.group = None112 self.owner = None113 114 def finalize_options(self) -> None:115 # have to finalize 'plat_name' before 'bdist_base'116 if self.plat_name is None:117 if self.skip_build:118 self.plat_name = get_platform()119 else:120 self.plat_name = self.get_finalized_command('build').plat_name121 122 # 'bdist_base' -- parent of per-built-distribution-format123 # temporary directories (eg. we'll probably have124 # "build/bdist.<plat>/dumb", "build/bdist.<plat>/rpm", etc.)125 if self.bdist_base is None:126 build_base = self.get_finalized_command('build').build_base127 self.bdist_base = os.path.join(build_base, 'bdist.' + self.plat_name)128 129 self.ensure_string_list('formats')130 if self.formats is None:131 try:132 self.formats = [self.default_format[os.name]]133 except KeyError:134 raise DistutilsPlatformError(135 "don't know how to create built distributions "136 f"on platform {os.name}"137 )138 139 if self.dist_dir is None:140 self.dist_dir = "dist"141 142 def run(self) -> None:143 # Figure out which sub-commands we need to run.144 commands = []145 for format in self.formats:146 try:147 commands.append(self.format_commands[format][0])148 except KeyError:149 raise DistutilsOptionError(f"invalid format '{format}'")150 151 # Reinitialize and run each command.152 for i in range(len(self.formats)):153 cmd_name = commands[i]154 sub_cmd = self.reinitialize_command(cmd_name)155 if cmd_name not in self.no_format_option:156 sub_cmd.format = self.formats[i]157 158 # passing the owner and group names for tar archiving159 if cmd_name == 'bdist_dumb':160 sub_cmd.owner = self.owner161 sub_cmd.group = self.group162 163 # If we're going to need to run this command again, tell it to164 # keep its temporary files around so subsequent runs go faster.165 if cmd_name in commands[i + 1 :]:166 sub_cmd.keep_temp = True167 self.run_command(cmd_name)168 