OLD | NEW |
(Empty) | |
| 1 from distutils import log, dir_util |
| 2 import os |
| 3 |
| 4 from setuptools import Command |
| 5 from setuptools.archive_util import unpack_archive |
| 6 import pkg_resources |
| 7 |
| 8 |
| 9 class install_egg_info(Command): |
| 10 """Install an .egg-info directory for the package""" |
| 11 |
| 12 description = "Install an .egg-info directory for the package" |
| 13 |
| 14 user_options = [ |
| 15 ('install-dir=', 'd', "directory to install to"), |
| 16 ] |
| 17 |
| 18 def initialize_options(self): |
| 19 self.install_dir = None |
| 20 |
| 21 def finalize_options(self): |
| 22 self.set_undefined_options('install_lib', |
| 23 ('install_dir', 'install_dir')) |
| 24 ei_cmd = self.get_finalized_command("egg_info") |
| 25 basename = pkg_resources.Distribution( |
| 26 None, None, ei_cmd.egg_name, ei_cmd.egg_version |
| 27 ).egg_name() + '.egg-info' |
| 28 self.source = ei_cmd.egg_info |
| 29 self.target = os.path.join(self.install_dir, basename) |
| 30 self.outputs = [self.target] |
| 31 |
| 32 def run(self): |
| 33 self.run_command('egg_info') |
| 34 if os.path.isdir(self.target) and not os.path.islink(self.target): |
| 35 dir_util.remove_tree(self.target, dry_run=self.dry_run) |
| 36 elif os.path.exists(self.target): |
| 37 self.execute(os.unlink, (self.target,), "Removing " + self.target) |
| 38 if not self.dry_run: |
| 39 pkg_resources.ensure_directory(self.target) |
| 40 self.execute( |
| 41 self.copytree, (), "Copying %s to %s" % (self.source, self.target) |
| 42 ) |
| 43 self.install_namespaces() |
| 44 |
| 45 def get_outputs(self): |
| 46 return self.outputs |
| 47 |
| 48 def copytree(self): |
| 49 # Copy the .egg-info tree to site-packages |
| 50 def skimmer(src, dst): |
| 51 # filter out source-control directories; note that 'src' is always |
| 52 # a '/'-separated path, regardless of platform. 'dst' is a |
| 53 # platform-specific path. |
| 54 for skip in '.svn/', 'CVS/': |
| 55 if src.startswith(skip) or '/' + skip in src: |
| 56 return None |
| 57 self.outputs.append(dst) |
| 58 log.debug("Copying %s to %s", src, dst) |
| 59 return dst |
| 60 |
| 61 unpack_archive(self.source, self.target, skimmer) |
| 62 |
| 63 def install_namespaces(self): |
| 64 nsp = self._get_all_ns_packages() |
| 65 if not nsp: |
| 66 return |
| 67 filename, ext = os.path.splitext(self.target) |
| 68 filename += '-nspkg.pth' |
| 69 self.outputs.append(filename) |
| 70 log.info("Installing %s", filename) |
| 71 lines = map(self._gen_nspkg_line, nsp) |
| 72 |
| 73 if self.dry_run: |
| 74 # always generate the lines, even in dry run |
| 75 list(lines) |
| 76 return |
| 77 |
| 78 with open(filename, 'wt') as f: |
| 79 f.writelines(lines) |
| 80 |
| 81 _nspkg_tmpl = ( |
| 82 "import sys, types, os", |
| 83 "p = os.path.join(sys._getframe(1).f_locals['sitedir'], *%(pth)r)", |
| 84 "ie = os.path.exists(os.path.join(p,'__init__.py'))", |
| 85 "m = not ie and " |
| 86 "sys.modules.setdefault(%(pkg)r, types.ModuleType(%(pkg)r))", |
| 87 "mp = (m or []) and m.__dict__.setdefault('__path__',[])", |
| 88 "(p not in mp) and mp.append(p)", |
| 89 ) |
| 90 "lines for the namespace installer" |
| 91 |
| 92 _nspkg_tmpl_multi = ( |
| 93 'm and setattr(sys.modules[%(parent)r], %(child)r, m)', |
| 94 ) |
| 95 "additional line(s) when a parent package is indicated" |
| 96 |
| 97 @classmethod |
| 98 def _gen_nspkg_line(cls, pkg): |
| 99 # ensure pkg is not a unicode string under Python 2.7 |
| 100 pkg = str(pkg) |
| 101 pth = tuple(pkg.split('.')) |
| 102 tmpl_lines = cls._nspkg_tmpl |
| 103 parent, sep, child = pkg.rpartition('.') |
| 104 if parent: |
| 105 tmpl_lines += cls._nspkg_tmpl_multi |
| 106 return ';'.join(tmpl_lines) % locals() + '\n' |
| 107 |
| 108 def _get_all_ns_packages(self): |
| 109 """Return sorted list of all package namespaces""" |
| 110 nsp = set() |
| 111 for pkg in self.distribution.namespace_packages or []: |
| 112 pkg = pkg.split('.') |
| 113 while pkg: |
| 114 nsp.add('.'.join(pkg)) |
| 115 pkg.pop() |
| 116 return sorted(nsp) |
OLD | NEW |