codekingpro/portable-devtools
114k
1"""2distutils.command.install_egg_info3 4Implements the Distutils 'install_egg_info' command, for installing5a package's PKG-INFO metadata.6"""7 8import os9import re10import sys11from typing import ClassVar12 13from .. import dir_util14from .._log import log15from ..cmd import Command16 17 18class install_egg_info(Command):19 """Install an .egg-info file for the package"""20 21 description = "Install package's PKG-INFO metadata as an .egg-info file"22 user_options: ClassVar[list[tuple[str, str, str]]] = [23 ('install-dir=', 'd', "directory to install to"),24 ]25 26 def initialize_options(self):27 self.install_dir = None28 29 @property30 def basename(self):31 """32 Allow basename to be overridden by child class.33 Ref pypa/distutils#2.34 """35 name = to_filename(safe_name(self.distribution.get_name()))36 version = to_filename(safe_version(self.distribution.get_version()))37 return f"{name}-{version}-py{sys.version_info.major}.{sys.version_info.minor}.egg-info"38 39 def finalize_options(self):40 self.set_undefined_options('install_lib', ('install_dir', 'install_dir'))41 self.target = os.path.join(self.install_dir, self.basename)42 self.outputs = [self.target]43 44 def run(self):45 target = self.target46 if os.path.isdir(target) and not os.path.islink(target):47 dir_util.remove_tree(target)48 elif os.path.exists(target):49 self.execute(os.unlink, (self.target,), "Removing " + target)50 elif not os.path.isdir(self.install_dir):51 self.execute(52 os.makedirs, (self.install_dir,), "Creating " + self.install_dir53 )54 log.info("Writing %s", target)55 with open(target, 'w', encoding='UTF-8') as f:56 self.distribution.metadata.write_pkg_file(f)57 58 def get_outputs(self):59 return self.outputs60 61 62# The following routines are taken from setuptools' pkg_resources module and63# can be replaced by importing them from pkg_resources once it is included64# in the stdlib.65 66 67def safe_name(name):68 """Convert an arbitrary string to a standard distribution name69 70 Any runs of non-alphanumeric/. characters are replaced with a single '-'.71 """72 return re.sub('[^A-Za-z0-9.]+', '-', name)73 74 75def safe_version(version):76 """Convert an arbitrary string to a standard version string77 78 Spaces become dots, and all other non-alphanumeric characters become79 dashes, with runs of multiple dashes condensed to a single dash.80 """81 version = version.replace(' ', '.')82 return re.sub('[^A-Za-z0-9.]+', '-', version)83 84 85def to_filename(name):86 """Convert a project or version name to its filename-escaped form87 88 Any '-' characters are currently replaced with '_'.89 """90 return name.replace('-', '_')91 