2 distutils.command.install_egg_info
4 Implements the Distutils 'install_egg_info' command, for installing
5 a package's PKG-INFO metadata.
12 from ..cmd import Command
13 from .. import dir_util
14 from .._log import log
17 class install_egg_info(Command):
18 """Install an .egg-info file for the package"""
20 description = "Install package's PKG-INFO metadata as an .egg-info file"
22 ('install-dir=', 'd', "directory to install to"),
25 def initialize_options(self):
26 self.install_dir = None
31 Allow basename to be overridden by child class.
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],
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]
47 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):
53 os.makedirs, (self.install_dir,), "Creating " + self.install_dir
55 log.info("Writing %s", target)
57 with open(target, 'w', encoding='UTF-8') as f:
58 self.distribution.metadata.write_pkg_file(f)
60 def get_outputs(self):
64 # The following routines are taken from setuptools' pkg_resources module and
65 # can be replaced by importing them from pkg_resources once it is included
70 """Convert an arbitrary string to a standard distribution name
72 Any runs of non-alphanumeric/. characters are replaced with a single '-'.
74 return re.sub('[^A-Za-z0-9.]+', '-', name)
77 def safe_version(version):
78 """Convert an arbitrary string to a standard version string
80 Spaces become dots, and all other non-alphanumeric characters become
81 dashes, with runs of multiple dashes condensed to a single dash.
83 version = version.replace(' ', '.')
84 return re.sub('[^A-Za-z0-9.]+', '-', version)
87 def to_filename(name):
88 """Convert a project or version name to its filename-escaped form
90 Any '-' characters are currently replaced with '_'.
92 return name.replace('-', '_')