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 sys11 12from .. import dir_util13from .._log import log14from ..cmd import Command15 16 17class install_egg_info(Command):18 """Install an .egg-info file for the package"""19 20 description = "Install package's PKG-INFO metadata as an .egg-info file"21 user_options = [22 ('install-dir=', 'd', "directory to install to"),23 ]24 25 def initialize_options(self):26 self.install_dir = None27 28 @property29 def basename(self):30 """31 Allow basename to be overridden by child class.32 Ref pypa/distutils#2.33 """34 return "%s-%s-py%d.%d.egg-info" % (35 to_filename(safe_name(self.distribution.get_name())),36 to_filename(safe_version(self.distribution.get_version())),37 *sys.version_info[:2],38 )39 40 def finalize_options(self):41 self.set_undefined_options('install_lib', ('install_dir', 'install_dir'))42 self.target = os.path.join(self.install_dir, self.basename)43 self.outputs = [self.target]44 45 def run(self):46 target = self.target47 if os.path.isdir(target) and not os.path.islink(target):48 dir_util.remove_tree(target, dry_run=self.dry_run)49 elif os.path.exists(target):50 self.execute(os.unlink, (self.target,), "Removing " + target)51 elif not os.path.isdir(self.install_dir):52 self.execute(53 os.makedirs, (self.install_dir,), "Creating " + self.install_dir54 )55 log.info("Writing %s", target)56 if not self.dry_run:57 with open(target, 'w', encoding='UTF-8') as f:58 self.distribution.metadata.write_pkg_file(f)59 60 def get_outputs(self):61 return self.outputs62 63 64# The following routines are taken from setuptools' pkg_resources module and65# can be replaced by importing them from pkg_resources once it is included66# in the stdlib.67 68 69def safe_name(name):70 """Convert an arbitrary string to a standard distribution name71 72 Any runs of non-alphanumeric/. characters are replaced with a single '-'.73 """74 return re.sub('[^A-Za-z0-9.]+', '-', name)75 76 77def safe_version(version):78 """Convert an arbitrary string to a standard version string79 80 Spaces become dots, and all other non-alphanumeric characters become81 dashes, with runs of multiple dashes condensed to a single dash.82 """83 version = version.replace(' ', '.')84 return re.sub('[^A-Za-z0-9.]+', '-', version)85 86 87def to_filename(name):88 """Convert a project or version name to its filename-escaped form89 90 Any '-' characters are currently replaced with '_'.91 """92 return name.replace('-', '_')93 