annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/setuptools/namespaces.py @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 import itertools
jpayne@69 2 import os
jpayne@69 3
jpayne@69 4 from .compat import py312
jpayne@69 5
jpayne@69 6 from distutils import log
jpayne@69 7
jpayne@69 8 flatten = itertools.chain.from_iterable
jpayne@69 9
jpayne@69 10
jpayne@69 11 class Installer:
jpayne@69 12 nspkg_ext = '-nspkg.pth'
jpayne@69 13
jpayne@69 14 def install_namespaces(self):
jpayne@69 15 nsp = self._get_all_ns_packages()
jpayne@69 16 if not nsp:
jpayne@69 17 return
jpayne@69 18 filename = self._get_nspkg_file()
jpayne@69 19 self.outputs.append(filename)
jpayne@69 20 log.info("Installing %s", filename)
jpayne@69 21 lines = map(self._gen_nspkg_line, nsp)
jpayne@69 22
jpayne@69 23 if self.dry_run:
jpayne@69 24 # always generate the lines, even in dry run
jpayne@69 25 list(lines)
jpayne@69 26 return
jpayne@69 27
jpayne@69 28 with open(filename, 'wt', encoding=py312.PTH_ENCODING) as f:
jpayne@69 29 # Python<3.13 requires encoding="locale" instead of "utf-8"
jpayne@69 30 # See: python/cpython#77102
jpayne@69 31 f.writelines(lines)
jpayne@69 32
jpayne@69 33 def uninstall_namespaces(self):
jpayne@69 34 filename = self._get_nspkg_file()
jpayne@69 35 if not os.path.exists(filename):
jpayne@69 36 return
jpayne@69 37 log.info("Removing %s", filename)
jpayne@69 38 os.remove(filename)
jpayne@69 39
jpayne@69 40 def _get_nspkg_file(self):
jpayne@69 41 filename, _ = os.path.splitext(self._get_target())
jpayne@69 42 return filename + self.nspkg_ext
jpayne@69 43
jpayne@69 44 def _get_target(self):
jpayne@69 45 return self.target
jpayne@69 46
jpayne@69 47 _nspkg_tmpl = (
jpayne@69 48 "import sys, types, os",
jpayne@69 49 "p = os.path.join(%(root)s, *%(pth)r)",
jpayne@69 50 "importlib = __import__('importlib.util')",
jpayne@69 51 "__import__('importlib.machinery')",
jpayne@69 52 (
jpayne@69 53 "m = "
jpayne@69 54 "sys.modules.setdefault(%(pkg)r, "
jpayne@69 55 "importlib.util.module_from_spec("
jpayne@69 56 "importlib.machinery.PathFinder.find_spec(%(pkg)r, "
jpayne@69 57 "[os.path.dirname(p)])))"
jpayne@69 58 ),
jpayne@69 59 ("m = m or sys.modules.setdefault(%(pkg)r, types.ModuleType(%(pkg)r))"),
jpayne@69 60 "mp = (m or []) and m.__dict__.setdefault('__path__',[])",
jpayne@69 61 "(p not in mp) and mp.append(p)",
jpayne@69 62 )
jpayne@69 63 "lines for the namespace installer"
jpayne@69 64
jpayne@69 65 _nspkg_tmpl_multi = ('m and setattr(sys.modules[%(parent)r], %(child)r, m)',)
jpayne@69 66 "additional line(s) when a parent package is indicated"
jpayne@69 67
jpayne@69 68 def _get_root(self):
jpayne@69 69 return "sys._getframe(1).f_locals['sitedir']"
jpayne@69 70
jpayne@69 71 def _gen_nspkg_line(self, pkg):
jpayne@69 72 pth = tuple(pkg.split('.'))
jpayne@69 73 root = self._get_root()
jpayne@69 74 tmpl_lines = self._nspkg_tmpl
jpayne@69 75 parent, sep, child = pkg.rpartition('.')
jpayne@69 76 if parent:
jpayne@69 77 tmpl_lines += self._nspkg_tmpl_multi
jpayne@69 78 return ';'.join(tmpl_lines) % locals() + '\n'
jpayne@69 79
jpayne@69 80 def _get_all_ns_packages(self):
jpayne@69 81 """Return sorted list of all package namespaces"""
jpayne@69 82 pkgs = self.distribution.namespace_packages or []
jpayne@69 83 return sorted(set(flatten(map(self._pkg_names, pkgs))))
jpayne@69 84
jpayne@69 85 @staticmethod
jpayne@69 86 def _pkg_names(pkg):
jpayne@69 87 """
jpayne@69 88 Given a namespace package, yield the components of that
jpayne@69 89 package.
jpayne@69 90
jpayne@69 91 >>> names = Installer._pkg_names('a.b.c')
jpayne@69 92 >>> set(names) == set(['a', 'a.b', 'a.b.c'])
jpayne@69 93 True
jpayne@69 94 """
jpayne@69 95 parts = pkg.split('.')
jpayne@69 96 while parts:
jpayne@69 97 yield '.'.join(parts)
jpayne@69 98 parts.pop()
jpayne@69 99
jpayne@69 100
jpayne@69 101 class DevelopInstaller(Installer):
jpayne@69 102 def _get_root(self):
jpayne@69 103 return repr(str(self.egg_path))
jpayne@69 104
jpayne@69 105 def _get_target(self):
jpayne@69 106 return self.egg_link