f3e8f3447dc206799a8e124000a81c443adc870f
[SubU] /
1 """
2 distutils.command.install_egg_info
3
4 Implements the Distutils 'install_egg_info' command, for installing
5 a package's PKG-INFO metadata.
6 """
7
8 import os
9 import sys
10 import re
11
12 from ..cmd import Command
13 from .. import dir_util
14 from .._log import log
15
16
17 class 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 = None
27
28     @property
29     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.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):
52             self.execute(
53                 os.makedirs, (self.install_dir,), "Creating " + self.install_dir
54             )
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.outputs
62
63
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
66 # in the stdlib.
67
68
69 def safe_name(name):
70     """Convert an arbitrary string to a standard distribution name
71
72     Any runs of non-alphanumeric/. characters are replaced with a single '-'.
73     """
74     return re.sub('[^A-Za-z0-9.]+', '-', name)
75
76
77 def safe_version(version):
78     """Convert an arbitrary string to a standard version string
79
80     Spaces become dots, and all other non-alphanumeric characters become
81     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
87 def to_filename(name):
88     """Convert a project or version name to its filename-escaped form
89
90     Any '-' characters are currently replaced with '_'.
91     """
92     return name.replace('-', '_')