Statistics
| Branch: | Revision:

root / env / lib / python2.7 / site-packages / distribute-0.6.19-py2.7.egg / setuptools / command / install_egg_info.py @ 1a305335

History | View | Annotate | Download (3.64 KB)

1
from setuptools import Command
2
from setuptools.archive_util import unpack_archive
3
from distutils import log, dir_util
4
import os, shutil, pkg_resources
5

    
6
class install_egg_info(Command):
7
    """Install an .egg-info directory for the package"""
8

    
9
    description = "Install an .egg-info directory for the package"
10

    
11
    user_options = [
12
        ('install-dir=', 'd', "directory to install to"),
13
    ]
14

    
15
    def initialize_options(self):
16
        self.install_dir = None
17

    
18
    def finalize_options(self):
19
        self.set_undefined_options('install_lib',('install_dir','install_dir'))
20
        ei_cmd = self.get_finalized_command("egg_info")
21
        basename = pkg_resources.Distribution(
22
            None, None, ei_cmd.egg_name, ei_cmd.egg_version
23
        ).egg_name()+'.egg-info'
24
        self.source = ei_cmd.egg_info
25
        self.target = os.path.join(self.install_dir, basename)
26
        self.outputs = [self.target]
27

    
28
    def run(self):
29
        self.run_command('egg_info')
30
        target = self.target
31
        if os.path.isdir(self.target) and not os.path.islink(self.target):
32
            dir_util.remove_tree(self.target, dry_run=self.dry_run)
33
        elif os.path.exists(self.target):
34
            self.execute(os.unlink,(self.target,),"Removing "+self.target)
35
        if not self.dry_run:
36
            pkg_resources.ensure_directory(self.target)
37
        self.execute(self.copytree, (),
38
            "Copying %s to %s" % (self.source, self.target)
39
        )
40
        self.install_namespaces()
41

    
42
    def get_outputs(self):
43
        return self.outputs
44

    
45
    def copytree(self):
46
        # Copy the .egg-info tree to site-packages
47
        def skimmer(src,dst):
48
            # filter out source-control directories; note that 'src' is always
49
            # a '/'-separated path, regardless of platform.  'dst' is a
50
            # platform-specific path.
51
            for skip in '.svn/','CVS/':
52
                if src.startswith(skip) or '/'+skip in src:
53
                    return None
54
            self.outputs.append(dst)
55
            log.debug("Copying %s to %s", src, dst)
56
            return dst
57
        unpack_archive(self.source, self.target, skimmer)
58

    
59

    
60

    
61

    
62

    
63

    
64

    
65

    
66

    
67

    
68

    
69

    
70

    
71

    
72

    
73

    
74

    
75

    
76

    
77

    
78

    
79

    
80

    
81

    
82

    
83
    def install_namespaces(self):
84
        nsp = self._get_all_ns_packages()
85
        if not nsp: return
86
        filename,ext = os.path.splitext(self.target)
87
        filename += '-nspkg.pth'; self.outputs.append(filename)
88
        log.info("Installing %s",filename)
89
        if not self.dry_run:
90
            f = open(filename,'wt')
91
            for pkg in nsp:
92
                pth = tuple(pkg.split('.'))
93
                trailer = '\n'
94
                if '.' in pkg:
95
                    trailer = (
96
                        "; m and setattr(sys.modules[%r], %r, m)\n"
97
                        % ('.'.join(pth[:-1]), pth[-1])
98
                    )
99
                f.write(
100
                    "import sys,types,os; "
101
                    "p = os.path.join(sys._getframe(1).f_locals['sitedir'], "
102
                        "*%(pth)r); "
103
                    "ie = os.path.exists(os.path.join(p,'__init__.py')); "
104
                    "m = not ie and "
105
                        "sys.modules.setdefault(%(pkg)r,types.ModuleType(%(pkg)r)); "
106
                    "mp = (m or []) and m.__dict__.setdefault('__path__',[]); "
107
                    "(p not in mp) and mp.append(p)%(trailer)s"
108
                    % locals()
109
                )
110
            f.close()
111

    
112
    def _get_all_ns_packages(self):
113
        nsp = {}
114
        for pkg in self.distribution.namespace_packages or []:
115
            pkg = pkg.split('.')
116
            while pkg:
117
                nsp['.'.join(pkg)] = 1
118
                pkg.pop()
119
        nsp=list(nsp)
120
        nsp.sort()  # set up shorter names first
121
        return nsp
122

    
123