Aluode/PerceptionLabPortable
0
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, dry_run=self.dry_run)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 if not self.dry_run:56 with open(target, 'w', encoding='UTF-8') as f:57 self.distribution.metadata.write_pkg_file(f)58 59 def get_outputs(self):60 return self.outputs61 62 63# The following routines are taken from setuptools' pkg_resources module and64# can be replaced by importing them from pkg_resources once it is included65# in the stdlib.66 67 68def safe_name(name):69 """Convert an arbitrary string to a standard distribution name70 71 Any runs of non-alphanumeric/. characters are replaced with a single '-'.72 """73 return re.sub('[^A-Za-z0-9.]+', '-', name)74 75 76def safe_version(version):77 """Convert an arbitrary string to a standard version string78 79 Spaces become dots, and all other non-alphanumeric characters become80 dashes, with runs of multiple dashes condensed to a single dash.81 """82 version = version.replace(' ', '.')83 return re.sub('[^A-Za-z0-9.]+', '-', version)84 85 86def to_filename(name):87 """Convert a project or version name to its filename-escaped form88 89 Any '-' characters are currently replaced with '_'.90 """91 return name.replace('-', '_')92 