]> jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/setuptools/depends.py
init: venv aand flask
[dlqueue.git] / venv / lib / python3.11 / site-packages / setuptools / depends.py
1 import sys
2 import marshal
3 import contextlib
4 import dis
5
6 from setuptools.extern.packaging import version
7
8 from ._imp import find_module, PY_COMPILED, PY_FROZEN, PY_SOURCE
9 from . import _imp
10
11
12 __all__ = ['Require', 'find_module', 'get_module_constant', 'extract_constant']
13
14
15 class Require:
16 """A prerequisite to building or installing a distribution"""
17
18 def __init__(
19 self, name, requested_version, module, homepage='', attribute=None, format=None
20 ):
21 if format is None and requested_version is not None:
22 format = version.Version
23
24 if format is not None:
25 requested_version = format(requested_version)
26 if attribute is None:
27 attribute = '__version__'
28
29 self.__dict__.update(locals())
30 del self.self
31
32 def full_name(self):
33 """Return full package/distribution name, w/version"""
34 if self.requested_version is not None:
35 return '%s-%s' % (self.name, self.requested_version)
36 return self.name
37
38 def version_ok(self, version):
39 """Is 'version' sufficiently up-to-date?"""
40 return (
41 self.attribute is None
42 or self.format is None
43 or str(version) != "unknown"
44 and self.format(version) >= self.requested_version
45 )
46
47 def get_version(self, paths=None, default="unknown"):
48 """Get version number of installed module, 'None', or 'default'
49
50 Search 'paths' for module. If not found, return 'None'. If found,
51 return the extracted version attribute, or 'default' if no version
52 attribute was specified, or the value cannot be determined without
53 importing the module. The version is formatted according to the
54 requirement's version format (if any), unless it is 'None' or the
55 supplied 'default'.
56 """
57
58 if self.attribute is None:
59 try:
60 f, p, i = find_module(self.module, paths)
61 if f:
62 f.close()
63 return default
64 except ImportError:
65 return None
66
67 v = get_module_constant(self.module, self.attribute, default, paths)
68
69 if v is not None and v is not default and self.format is not None:
70 return self.format(v)
71
72 return v
73
74 def is_present(self, paths=None):
75 """Return true if dependency is present on 'paths'"""
76 return self.get_version(paths) is not None
77
78 def is_current(self, paths=None):
79 """Return true if dependency is present and up-to-date on 'paths'"""
80 version = self.get_version(paths)
81 if version is None:
82 return False
83 return self.version_ok(str(version))
84
85
86 def maybe_close(f):
87 @contextlib.contextmanager
88 def empty():
89 yield
90 return
91
92 if not f:
93 return empty()
94
95 return contextlib.closing(f)
96
97
98 def get_module_constant(module, symbol, default=-1, paths=None):
99 """Find 'module' by searching 'paths', and extract 'symbol'
100
101 Return 'None' if 'module' does not exist on 'paths', or it does not define
102 'symbol'. If the module defines 'symbol' as a constant, return the
103 constant. Otherwise, return 'default'."""
104
105 try:
106 f, path, (suffix, mode, kind) = info = find_module(module, paths)
107 except ImportError:
108 # Module doesn't exist
109 return None
110
111 with maybe_close(f):
112 if kind == PY_COMPILED:
113 f.read(8) # skip magic & date
114 code = marshal.load(f)
115 elif kind == PY_FROZEN:
116 code = _imp.get_frozen_object(module, paths)
117 elif kind == PY_SOURCE:
118 code = compile(f.read(), path, 'exec')
119 else:
120 # Not something we can parse; we'll have to import it. :(
121 imported = _imp.get_module(module, paths, info)
122 return getattr(imported, symbol, None)
123
124 return extract_constant(code, symbol, default)
125
126
127 def extract_constant(code, symbol, default=-1):
128 """Extract the constant value of 'symbol' from 'code'
129
130 If the name 'symbol' is bound to a constant value by the Python code
131 object 'code', return that value. If 'symbol' is bound to an expression,
132 return 'default'. Otherwise, return 'None'.
133
134 Return value is based on the first assignment to 'symbol'. 'symbol' must
135 be a global, or at least a non-"fast" local in the code block. That is,
136 only 'STORE_NAME' and 'STORE_GLOBAL' opcodes are checked, and 'symbol'
137 must be present in 'code.co_names'.
138 """
139 if symbol not in code.co_names:
140 # name's not there, can't possibly be an assignment
141 return None
142
143 name_idx = list(code.co_names).index(symbol)
144
145 STORE_NAME = 90
146 STORE_GLOBAL = 97
147 LOAD_CONST = 100
148
149 const = default
150
151 for byte_code in dis.Bytecode(code):
152 op = byte_code.opcode
153 arg = byte_code.arg
154
155 if op == LOAD_CONST:
156 const = code.co_consts[arg]
157 elif arg == name_idx and (op == STORE_NAME or op == STORE_GLOBAL):
158 return const
159 else:
160 const = default
161
162
163 def _update_globals():
164 """
165 Patch the globals to remove the objects not available on some platforms.
166
167 XXX it'd be better to test assertions about bytecode instead.
168 """
169
170 if not sys.platform.startswith('java') and sys.platform != 'cli':
171 return
172 incompatible = 'extract_constant', 'get_module_constant'
173 for name in incompatible:
174 del globals()[name]
175 __all__.remove(name)
176
177
178 _update_globals()