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    def finalize_options(self):
23        self.set_undefined_options('install_lib',('install_dir','install_dir'))
24        basename = "%s-%s-py%s.egg-info" % (
25            to_filename(safe_name(self.distribution.get_name())),
26            to_filename(safe_version(self.distribution.get_version())),
27            sys.version[:3]
28        )
29        self.target = os.path.join(self.install_dir, basename)
30        self.outputs = [self.target]
31
32    def run(self):
33        target = self.target
34        if os.path.isdir(target) and not os.path.islink(target):
35            dir_util.remove_tree(target, dry_run=self.dry_run)
36        elif os.path.exists(target):
37            self.execute(os.unlink,(self.target,),"Removing "+target)
38        elif not os.path.isdir(self.install_dir):
39            self.execute(os.makedirs, (self.install_dir,),
40                         "Creating "+self.install_dir)
41        log.info("Writing %s", target)
42        if not self.dry_run:
43            f = open(target, 'w')
44            self.distribution.metadata.write_pkg_file(f)
45            f.close()
46
47    def get_outputs(self):
48        return self.outputs
49
50
51# The following routines are taken from setuptools' pkg_resources module and
52# can be replaced by importing them from pkg_resources once it is included
53# in the stdlib.
54
55def safe_name(name):
56    """Convert an arbitrary string to a standard distribution name
57
58    Any runs of non-alphanumeric/. characters are replaced with a single '-'.
59    """
60    return re.sub('[^A-Za-z0-9.]+', '-', name)
61
62
63def safe_version(version):
64    """Convert an arbitrary string to a standard version string
65
66    Spaces become dots, and all other non-alphanumeric characters become
67    dashes, with runs of multiple dashes condensed to a single dash.
68    """
69    version = version.replace(' ','.')
70    return re.sub('[^A-Za-z0-9.]+', '-', version)
71
72
73def to_filename(name):
74    """Convert a project or version name to its filename-escaped form
75
76    Any '-' characters are currently replaced with '_'.
77    """
78    return name.replace('-','_')
79