python (3.11.7)
       1  """distutils.command.bdist
       2  
       3  Implements the Distutils 'bdist' command (create a built [binary]
       4  distribution)."""
       5  
       6  import os
       7  import warnings
       8  
       9  from distutils.core import Command
      10  from distutils.errors import DistutilsPlatformError, DistutilsOptionError
      11  from distutils.util import get_platform
      12  
      13  
      14  def show_formats():
      15      """Print list of available formats (arguments to "--format" option)."""
      16      from distutils.fancy_getopt import FancyGetopt
      17  
      18      formats = []
      19      for format in bdist.format_commands:
      20          formats.append(("formats=" + format, None, bdist.format_commands[format][1]))
      21      pretty_printer = FancyGetopt(formats)
      22      pretty_printer.print_help("List of available distribution formats:")
      23  
      24  
      25  class ESC[4;38;5;81mListCompat(ESC[4;38;5;149mdict):
      26      # adapter to allow for Setuptools compatibility in format_commands
      27      def append(self, item):
      28          warnings.warn(
      29              """format_commands is now a dict. append is deprecated.""",
      30              DeprecationWarning,
      31              stacklevel=2,
      32          )
      33  
      34  
      35  class ESC[4;38;5;81mbdist(ESC[4;38;5;149mCommand):
      36  
      37      description = "create a built (binary) distribution"
      38  
      39      user_options = [
      40          ('bdist-base=', 'b', "temporary directory for creating built distributions"),
      41          (
      42              'plat-name=',
      43              'p',
      44              "platform name to embed in generated filenames "
      45              "(default: %s)" % get_platform(),
      46          ),
      47          ('formats=', None, "formats for distribution (comma-separated list)"),
      48          (
      49              'dist-dir=',
      50              'd',
      51              "directory to put final built distributions in " "[default: dist]",
      52          ),
      53          ('skip-build', None, "skip rebuilding everything (for testing/debugging)"),
      54          (
      55              'owner=',
      56              'u',
      57              "Owner name used when creating a tar file" " [default: current user]",
      58          ),
      59          (
      60              'group=',
      61              'g',
      62              "Group name used when creating a tar file" " [default: current group]",
      63          ),
      64      ]
      65  
      66      boolean_options = ['skip-build']
      67  
      68      help_options = [
      69          ('help-formats', None, "lists available distribution formats", show_formats),
      70      ]
      71  
      72      # The following commands do not take a format option from bdist
      73      no_format_option = ('bdist_rpm',)
      74  
      75      # This won't do in reality: will need to distinguish RPM-ish Linux,
      76      # Debian-ish Linux, Solaris, FreeBSD, ..., Windows, Mac OS.
      77      default_format = {'posix': 'gztar', 'nt': 'zip'}
      78  
      79      # Define commands in preferred order for the --help-formats option
      80      format_commands = ListCompat(
      81          {
      82              'rpm': ('bdist_rpm', "RPM distribution"),
      83              'gztar': ('bdist_dumb', "gzip'ed tar file"),
      84              'bztar': ('bdist_dumb', "bzip2'ed tar file"),
      85              'xztar': ('bdist_dumb', "xz'ed tar file"),
      86              'ztar': ('bdist_dumb', "compressed tar file"),
      87              'tar': ('bdist_dumb', "tar file"),
      88              'zip': ('bdist_dumb', "ZIP file"),
      89          }
      90      )
      91  
      92      # for compatibility until consumers only reference format_commands
      93      format_command = format_commands
      94  
      95      def initialize_options(self):
      96          self.bdist_base = None
      97          self.plat_name = None
      98          self.formats = None
      99          self.dist_dir = None
     100          self.skip_build = 0
     101          self.group = None
     102          self.owner = None
     103  
     104      def finalize_options(self):
     105          # have to finalize 'plat_name' before 'bdist_base'
     106          if self.plat_name is None:
     107              if self.skip_build:
     108                  self.plat_name = get_platform()
     109              else:
     110                  self.plat_name = self.get_finalized_command('build').plat_name
     111  
     112          # 'bdist_base' -- parent of per-built-distribution-format
     113          # temporary directories (eg. we'll probably have
     114          # "build/bdist.<plat>/dumb", "build/bdist.<plat>/rpm", etc.)
     115          if self.bdist_base is None:
     116              build_base = self.get_finalized_command('build').build_base
     117              self.bdist_base = os.path.join(build_base, 'bdist.' + self.plat_name)
     118  
     119          self.ensure_string_list('formats')
     120          if self.formats is None:
     121              try:
     122                  self.formats = [self.default_format[os.name]]
     123              except KeyError:
     124                  raise DistutilsPlatformError(
     125                      "don't know how to create built distributions "
     126                      "on platform %s" % os.name
     127                  )
     128  
     129          if self.dist_dir is None:
     130              self.dist_dir = "dist"
     131  
     132      def run(self):
     133          # Figure out which sub-commands we need to run.
     134          commands = []
     135          for format in self.formats:
     136              try:
     137                  commands.append(self.format_commands[format][0])
     138              except KeyError:
     139                  raise DistutilsOptionError("invalid format '%s'" % format)
     140  
     141          # Reinitialize and run each command.
     142          for i in range(len(self.formats)):
     143              cmd_name = commands[i]
     144              sub_cmd = self.reinitialize_command(cmd_name)
     145              if cmd_name not in self.no_format_option:
     146                  sub_cmd.format = self.formats[i]
     147  
     148              # passing the owner and group names for tar archiving
     149              if cmd_name == 'bdist_dumb':
     150                  sub_cmd.owner = self.owner
     151                  sub_cmd.group = self.group
     152  
     153              # If we're going to need to run this command again, tell it to
     154              # keep its temporary files around so subsequent runs go faster.
     155              if cmd_name in commands[i + 1 :]:
     156                  sub_cmd.keep_temp = 1
     157              self.run_command(cmd_name)