• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""distutils.command.install_egg_info
2
3Implements the Distutils 'install_egg_info' command, for installing
4a package's PKG-INFO metadata."""
5
6
7from distutils.cmd import Command
8from distutils import log, dir_util
9import os, sys, re
10
11class install_egg_info(Command):
12    """Install an .egg-info file for the package"""
13
14    description = "Install package's PKG-INFO metadata as an .egg-info file"
15    user_options = [
16        ('install-dir=', 'd', "directory to install to"),
17    ]
18
19    def initialize_options(self):
20        self.install_dir = None
21
22    @property
23    def basename(self):
24        """
25        Allow basename to be overridden by child class.
26        Ref pypa/distutils#2.
27        """
28        return "%s-%s-py%d.%d.egg-info" % (
29            to_filename(safe_name(self.distribution.get_name())),
30            to_filename(safe_version(self.distribution.get_version())),
31            *sys.version_info[:2]
32        )
33
34    def finalize_options(self):
35        self.set_undefined_options('install_lib',('install_dir','install_dir'))
36        self.target = os.path.join(self.install_dir, self.basename)
37        self.outputs = [self.target]
38
39    def run(self):
40        target = self.target
41        if os.path.isdir(target) and not os.path.islink(target):
42            dir_util.remove_tree(target, dry_run=self.dry_run)
43        elif os.path.exists(target):
44            self.execute(os.unlink,(self.target,),"Removing "+target)
45        elif not os.path.isdir(self.install_dir):
46            self.execute(os.makedirs, (self.install_dir,),
47                         "Creating "+self.install_dir)
48        log.info("Writing %s", target)
49        if not self.dry_run:
50            with open(target, 'w', encoding='UTF-8') as f:
51                self.distribution.metadata.write_pkg_file(f)
52
53    def get_outputs(self):
54        return self.outputs
55
56
57# The following routines are taken from setuptools' pkg_resources module and
58# can be replaced by importing them from pkg_resources once it is included
59# in the stdlib.
60
61def safe_name(name):
62    """Convert an arbitrary string to a standard distribution name
63
64    Any runs of non-alphanumeric/. characters are replaced with a single '-'.
65    """
66    return re.sub('[^A-Za-z0-9.]+', '-', name)
67
68
69def safe_version(version):
70    """Convert an arbitrary string to a standard version string
71
72    Spaces become dots, and all other non-alphanumeric characters become
73    dashes, with runs of multiple dashes condensed to a single dash.
74    """
75    version = version.replace(' ','.')
76    return re.sub('[^A-Za-z0-9.]+', '-', version)
77
78
79def to_filename(name):
80    """Convert a project or version name to its filename-escaped form
81
82    Any '-' characters are currently replaced with '_'.
83    """
84    return name.replace('-','_')
85