This commit is contained in:
ton
2024-10-07 10:13:40 +07:00
parent aa1631742f
commit 3a7d696db6
9729 changed files with 1832837 additions and 161742 deletions

View File

@@ -4,29 +4,28 @@ Provides the Distribution class, which represents the module distribution
being built/installed/distributed.
"""
import sys
import os
import re
import pathlib
import contextlib
import logging
import os
import pathlib
import re
import sys
import warnings
from collections.abc import Iterable
from email import message_from_file
try:
import warnings
except ImportError:
warnings = None
from packaging.utils import canonicalize_name, canonicalize_version
from .errors import (
DistutilsOptionError,
DistutilsModuleError,
DistutilsArgError,
DistutilsClassError,
)
from .fancy_getopt import FancyGetopt, translate_longopt
from .util import check_environ, strtobool, rfc822_escape
from ._log import log
from .debug import DEBUG
from .errors import (
DistutilsArgError,
DistutilsClassError,
DistutilsModuleError,
DistutilsOptionError,
)
from .fancy_getopt import FancyGetopt, translate_longopt
from .util import check_environ, rfc822_escape, strtobool
# Regex to define acceptable Distutils command names. This is not *quite*
# the same as a Python NAME -- I don't allow leading underscores. The fact
@@ -136,9 +135,9 @@ Common commands: (see '--help-commands' for more)
"""
# Default values for our command-line options
self.verbose = 1
self.dry_run = 0
self.help = 0
self.verbose = True
self.dry_run = False
self.help = False
for attr in self.display_option_names:
setattr(self, attr, 0)
@@ -246,10 +245,7 @@ Common commands: (see '--help-commands' for more)
attrs['license'] = attrs['licence']
del attrs['licence']
msg = "'licence' distribution option is deprecated; use 'license'"
if warnings is not None:
warnings.warn(msg)
else:
sys.stderr.write(msg + "\n")
warnings.warn(msg)
# Now work on the rest of the attributes. Any attribute that's
# not already defined is invalid!
@@ -261,7 +257,7 @@ Common commands: (see '--help-commands' for more)
elif hasattr(self, key):
setattr(self, key, val)
else:
msg = "Unknown distribution option: %s" % repr(key)
msg = f"Unknown distribution option: {key!r}"
warnings.warn(msg)
# no-user-cfg is handled before other command line args
@@ -310,9 +306,9 @@ Common commands: (see '--help-commands' for more)
for cmd_name in commands:
opt_dict = self.command_options.get(cmd_name)
if opt_dict is None:
self.announce(indent + "no option dict for '%s' command" % cmd_name)
self.announce(indent + f"no option dict for '{cmd_name}' command")
else:
self.announce(indent + "option dict for '%s' command:" % cmd_name)
self.announce(indent + f"option dict for '{cmd_name}' command:")
out = pformat(opt_dict)
for line in out.split('\n'):
self.announce(indent + " " + line)
@@ -338,7 +334,7 @@ Common commands: (see '--help-commands' for more)
files = [str(path) for path in self._gen_paths() if os.path.isfile(path)]
if DEBUG:
self.announce("using config files: %s" % ', '.join(files))
self.announce("using config files: {}".format(', '.join(files)))
return files
@@ -351,7 +347,8 @@ Common commands: (see '--help-commands' for more)
prefix = '.' * (os.name == 'posix')
filename = prefix + 'pydistutils.cfg'
if self.want_user_cfg:
yield pathlib.Path('~').expanduser() / filename
with contextlib.suppress(RuntimeError):
yield pathlib.Path('~').expanduser() / filename
# All platforms support local setup.cfg
yield pathlib.Path('setup.cfg')
@@ -394,8 +391,8 @@ Common commands: (see '--help-commands' for more)
parser = ConfigParser()
for filename in filenames:
if DEBUG:
self.announce(" reading %s" % filename)
parser.read(filename)
self.announce(f" reading {filename}")
parser.read(filename, encoding='utf-8')
for section in parser.sections():
options = parser.options(section)
opt_dict = self.get_option_dict(section)
@@ -414,7 +411,7 @@ Common commands: (see '--help-commands' for more)
# to set Distribution options.
if 'global' in self.command_options:
for opt, (src, val) in self.command_options['global'].items():
for opt, (_src, val) in self.command_options['global'].items():
alias = self.negative_opt.get(opt)
try:
if alias:
@@ -524,7 +521,7 @@ Common commands: (see '--help-commands' for more)
# Pull the current command from the head of the command line
command = args[0]
if not command_re.match(command):
raise SystemExit("invalid command name '%s'" % command)
raise SystemExit(f"invalid command name '{command}'")
self.commands.append(command)
# Dig up the command class that implements this command, so we
@@ -539,7 +536,7 @@ Common commands: (see '--help-commands' for more)
# to be sure that the basic "command" interface is implemented.
if not issubclass(cmd_class, Command):
raise DistutilsClassError(
"command class %s must subclass Command" % cmd_class
f"command class {cmd_class} must subclass Command"
)
# Also make sure that the command object provides a list of its
@@ -578,23 +575,22 @@ Common commands: (see '--help-commands' for more)
parser.set_negative_aliases(negative_opt)
(args, opts) = parser.getopt(args[1:])
if hasattr(opts, 'help') and opts.help:
self._show_help(parser, display_options=0, commands=[cmd_class])
self._show_help(parser, display_options=False, commands=[cmd_class])
return
if hasattr(cmd_class, 'help_options') and isinstance(
cmd_class.help_options, list
):
help_option_found = 0
for help_option, short, desc, func in cmd_class.help_options:
for help_option, _short, _desc, func in cmd_class.help_options:
if hasattr(opts, parser.get_attr_name(help_option)):
help_option_found = 1
if callable(func):
func()
else:
raise DistutilsClassError(
"invalid help function %r for help option '%s': "
f"invalid help function {func!r} for help option '{help_option}': "
"must be a callable object (function, etc.)"
% (func, help_option)
)
if help_option_found:
@@ -621,7 +617,9 @@ Common commands: (see '--help-commands' for more)
value = [elm.strip() for elm in value.split(',')]
setattr(self.metadata, attr, value)
def _show_help(self, parser, global_options=1, display_options=1, commands=[]):
def _show_help(
self, parser, global_options=True, display_options=True, commands: Iterable = ()
):
"""Show help for the setup script command-line in the form of
several lists of command-line options. 'parser' should be a
FancyGetopt instance; do not expect it to be returned in the
@@ -635,8 +633,8 @@ Common commands: (see '--help-commands' for more)
in 'commands'.
"""
# late import because of mutual dependence between these modules
from distutils.core import gen_usage
from distutils.cmd import Command
from distutils.core import gen_usage
if global_options:
if display_options:
@@ -645,17 +643,16 @@ Common commands: (see '--help-commands' for more)
options = self.global_options
parser.set_option_table(options)
parser.print_help(self.common_usage + "\nGlobal options:")
print('')
print()
if display_options:
parser.set_option_table(self.display_options)
parser.print_help(
"Information display options (just display "
+ "information, ignore any commands)"
"Information display options (just display information, ignore any commands)"
)
print('')
print()
for command in self.commands:
for command in commands:
if isinstance(command, type) and issubclass(command, Command):
klass = command
else:
@@ -666,8 +663,8 @@ Common commands: (see '--help-commands' for more)
)
else:
parser.set_option_table(klass.user_options)
parser.print_help("Options for '%s' command:" % klass.__name__)
print('')
parser.print_help(f"Options for '{klass.__name__}' command:")
print()
print(gen_usage(self.script_name))
@@ -684,7 +681,7 @@ Common commands: (see '--help-commands' for more)
# we ignore "foo bar").
if self.help_commands:
self.print_commands()
print('')
print()
print(gen_usage(self.script_name))
return 1
@@ -692,12 +689,12 @@ Common commands: (see '--help-commands' for more)
# display that metadata in the order in which the user supplied the
# metadata options.
any_display_options = 0
is_display_option = {}
is_display_option = set()
for option in self.display_options:
is_display_option[option[0]] = 1
is_display_option.add(option[0])
for opt, val in option_order:
if val and is_display_option.get(opt):
if val and opt in is_display_option:
opt = translate_longopt(opt)
value = getattr(self.metadata, "get_" + opt)()
if opt in ('keywords', 'platforms'):
@@ -738,14 +735,9 @@ Common commands: (see '--help-commands' for more)
import distutils.command
std_commands = distutils.command.__all__
is_std = {}
for cmd in std_commands:
is_std[cmd] = 1
is_std = set(std_commands)
extra_commands = []
for cmd in self.cmdclass.keys():
if not is_std.get(cmd):
extra_commands.append(cmd)
extra_commands = [cmd for cmd in self.cmdclass.keys() if cmd not in is_std]
max_length = 0
for cmd in std_commands + extra_commands:
@@ -769,14 +761,9 @@ Common commands: (see '--help-commands' for more)
import distutils.command
std_commands = distutils.command.__all__
is_std = {}
for cmd in std_commands:
is_std[cmd] = 1
is_std = set(std_commands)
extra_commands = []
for cmd in self.cmdclass.keys():
if not is_std.get(cmd):
extra_commands.append(cmd)
extra_commands = [cmd for cmd in self.cmdclass.keys() if cmd not in is_std]
rv = []
for cmd in std_commands + extra_commands:
@@ -821,7 +808,7 @@ Common commands: (see '--help-commands' for more)
return klass
for pkgname in self.get_command_packages():
module_name = "{}.{}".format(pkgname, command)
module_name = f"{pkgname}.{command}"
klass_name = command
try:
@@ -834,16 +821,15 @@ Common commands: (see '--help-commands' for more)
klass = getattr(module, klass_name)
except AttributeError:
raise DistutilsModuleError(
"invalid command '%s' (no class '%s' in module '%s')"
% (command, klass_name, module_name)
f"invalid command '{command}' (no class '{klass_name}' in module '{module_name}')"
)
self.cmdclass[command] = klass
return klass
raise DistutilsModuleError("invalid command '%s'" % command)
raise DistutilsModuleError(f"invalid command '{command}'")
def get_command_obj(self, command, create=1):
def get_command_obj(self, command, create=True):
"""Return the command object for 'command'. Normally this object
is cached on a previous call to 'get_command_obj()'; if no command
object for 'command' is in the cache, then we either create and
@@ -854,12 +840,12 @@ Common commands: (see '--help-commands' for more)
if DEBUG:
self.announce(
"Distribution.get_command_obj(): "
"creating '%s' command object" % command
f"creating '{command}' command object"
)
klass = self.get_command_class(command)
cmd_obj = self.command_obj[command] = klass(self)
self.have_run[command] = 0
self.have_run[command] = False
# Set any options that were supplied in config files
# or on the command line. (NB. support for error
@@ -886,10 +872,10 @@ Common commands: (see '--help-commands' for more)
option_dict = self.get_option_dict(command_name)
if DEBUG:
self.announce(" setting options for '%s' command:" % command_name)
self.announce(f" setting options for '{command_name}' command:")
for option, (source, value) in option_dict.items():
if DEBUG:
self.announce(" {} = {} (from {})".format(option, value, source))
self.announce(f" {option} = {value} (from {source})")
try:
bool_opts = [translate_longopt(o) for o in command_obj.boolean_options]
except AttributeError:
@@ -909,13 +895,12 @@ Common commands: (see '--help-commands' for more)
setattr(command_obj, option, value)
else:
raise DistutilsOptionError(
"error in %s: command '%s' has no such option '%s'"
% (source, command_name, option)
f"error in {source}: command '{command_name}' has no such option '{option}'"
)
except ValueError as msg:
raise DistutilsOptionError(msg)
def reinitialize_command(self, command, reinit_subcommands=0):
def reinitialize_command(self, command, reinit_subcommands=False):
"""Reinitializes a command to the state it was in when first
returned by 'get_command_obj()': ie., initialized but not yet
finalized. This provides the opportunity to sneak option
@@ -945,8 +930,8 @@ Common commands: (see '--help-commands' for more)
if not command.finalized:
return command
command.initialize_options()
command.finalized = 0
self.have_run[command_name] = 0
command.finalized = False
self.have_run[command_name] = False
self._set_command_options(command)
if reinit_subcommands:
@@ -986,7 +971,7 @@ Common commands: (see '--help-commands' for more)
cmd_obj = self.get_command_obj(command)
cmd_obj.ensure_finalized()
cmd_obj.run()
self.have_run[command] = 1
self.have_run[command] = True
# -- Distribution query methods ------------------------------------
@@ -1149,9 +1134,9 @@ class DistributionMetadata:
version = '1.1'
# required fields
file.write('Metadata-Version: %s\n' % version)
file.write('Name: %s\n' % self.get_name())
file.write('Version: %s\n' % self.get_version())
file.write(f'Metadata-Version: {version}\n')
file.write(f'Name: {self.get_name()}\n')
file.write(f'Version: {self.get_version()}\n')
def maybe_write(header, val):
if val:
@@ -1178,7 +1163,7 @@ class DistributionMetadata:
def _write_list(self, file, name, values):
values = values or []
for value in values:
file.write('{}: {}\n'.format(name, value))
file.write(f'{name}: {value}\n')
# -- Metadata query methods ----------------------------------------
@@ -1189,7 +1174,26 @@ class DistributionMetadata:
return self.version or "0.0.0"
def get_fullname(self):
return "{}-{}".format(self.get_name(), self.get_version())
return self._fullname(self.get_name(), self.get_version())
@staticmethod
def _fullname(name: str, version: str) -> str:
"""
>>> DistributionMetadata._fullname('setup.tools', '1.0-2')
'setup_tools-1.0.post2'
>>> DistributionMetadata._fullname('setup-tools', '1.2post2')
'setup_tools-1.2.post2'
>>> DistributionMetadata._fullname('setup-tools', '1.0-r2')
'setup_tools-1.0.post2'
>>> DistributionMetadata._fullname('setup.tools', '1.0.post')
'setup_tools-1.0.post0'
>>> DistributionMetadata._fullname('setup.tools', '1.0+ubuntu-1')
'setup_tools-1.0+ubuntu.1'
"""
return "{}-{}".format(
canonicalize_name(name).replace('-', '_'),
canonicalize_version(version, strip_trailing_zero=False),
)
def get_author(self):
return self.author
@@ -1281,7 +1285,4 @@ def fix_help_options(options):
"""Convert a 4-tuple 'help_options' list as found in various command
classes to the 3-tuple form required by FancyGetopt.
"""
new_options = []
for help_tuple in options:
new_options.append(help_tuple[0:3])
return new_options
return [opt[0:3] for opt in options]