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