annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/setuptools/namespaces.py @ 68:5028fdace37b

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