]> jfr.im git - dlqueue.git/blob - venv/lib/python3.11/site-packages/setuptools/_distutils/command/install_lib.py
init: venv aand flask
[dlqueue.git] / venv / lib / python3.11 / site-packages / setuptools / _distutils / command / install_lib.py
1 """distutils.command.install_lib
2
3 Implements the Distutils 'install_lib' command
4 (install all Python modules)."""
5
6 import os
7 import importlib.util
8 import sys
9
10 from ..core import Command
11 from ..errors import DistutilsOptionError
12
13
14 # Extension for Python source files.
15 PYTHON_SOURCE_EXTENSION = ".py"
16
17
18 class install_lib(Command):
19 description = "install all Python modules (extensions and pure Python)"
20
21 # The byte-compilation options are a tad confusing. Here are the
22 # possible scenarios:
23 # 1) no compilation at all (--no-compile --no-optimize)
24 # 2) compile .pyc only (--compile --no-optimize; default)
25 # 3) compile .pyc and "opt-1" .pyc (--compile --optimize)
26 # 4) compile "opt-1" .pyc only (--no-compile --optimize)
27 # 5) compile .pyc and "opt-2" .pyc (--compile --optimize-more)
28 # 6) compile "opt-2" .pyc only (--no-compile --optimize-more)
29 #
30 # The UI for this is two options, 'compile' and 'optimize'.
31 # 'compile' is strictly boolean, and only decides whether to
32 # generate .pyc files. 'optimize' is three-way (0, 1, or 2), and
33 # decides both whether to generate .pyc files and what level of
34 # optimization to use.
35
36 user_options = [
37 ('install-dir=', 'd', "directory to install to"),
38 ('build-dir=', 'b', "build directory (where to install from)"),
39 ('force', 'f', "force installation (overwrite existing files)"),
40 ('compile', 'c', "compile .py to .pyc [default]"),
41 ('no-compile', None, "don't compile .py files"),
42 (
43 'optimize=',
44 'O',
45 "also compile with optimization: -O1 for \"python -O\", "
46 "-O2 for \"python -OO\", and -O0 to disable [default: -O0]",
47 ),
48 ('skip-build', None, "skip the build steps"),
49 ]
50
51 boolean_options = ['force', 'compile', 'skip-build']
52 negative_opt = {'no-compile': 'compile'}
53
54 def initialize_options(self):
55 # let the 'install' command dictate our installation directory
56 self.install_dir = None
57 self.build_dir = None
58 self.force = 0
59 self.compile = None
60 self.optimize = None
61 self.skip_build = None
62
63 def finalize_options(self):
64 # Get all the information we need to install pure Python modules
65 # from the umbrella 'install' command -- build (source) directory,
66 # install (target) directory, and whether to compile .py files.
67 self.set_undefined_options(
68 'install',
69 ('build_lib', 'build_dir'),
70 ('install_lib', 'install_dir'),
71 ('force', 'force'),
72 ('compile', 'compile'),
73 ('optimize', 'optimize'),
74 ('skip_build', 'skip_build'),
75 )
76
77 if self.compile is None:
78 self.compile = True
79 if self.optimize is None:
80 self.optimize = False
81
82 if not isinstance(self.optimize, int):
83 try:
84 self.optimize = int(self.optimize)
85 if self.optimize not in (0, 1, 2):
86 raise AssertionError
87 except (ValueError, AssertionError):
88 raise DistutilsOptionError("optimize must be 0, 1, or 2")
89
90 def run(self):
91 # Make sure we have built everything we need first
92 self.build()
93
94 # Install everything: simply dump the entire contents of the build
95 # directory to the installation directory (that's the beauty of
96 # having a build directory!)
97 outfiles = self.install()
98
99 # (Optionally) compile .py to .pyc
100 if outfiles is not None and self.distribution.has_pure_modules():
101 self.byte_compile(outfiles)
102
103 # -- Top-level worker functions ------------------------------------
104 # (called from 'run()')
105
106 def build(self):
107 if not self.skip_build:
108 if self.distribution.has_pure_modules():
109 self.run_command('build_py')
110 if self.distribution.has_ext_modules():
111 self.run_command('build_ext')
112
113 def install(self):
114 if os.path.isdir(self.build_dir):
115 outfiles = self.copy_tree(self.build_dir, self.install_dir)
116 else:
117 self.warn(
118 "'%s' does not exist -- no Python modules to install" % self.build_dir
119 )
120 return
121 return outfiles
122
123 def byte_compile(self, files):
124 if sys.dont_write_bytecode:
125 self.warn('byte-compiling is disabled, skipping.')
126 return
127
128 from ..util import byte_compile
129
130 # Get the "--root" directory supplied to the "install" command,
131 # and use it as a prefix to strip off the purported filename
132 # encoded in bytecode files. This is far from complete, but it
133 # should at least generate usable bytecode in RPM distributions.
134 install_root = self.get_finalized_command('install').root
135
136 if self.compile:
137 byte_compile(
138 files,
139 optimize=0,
140 force=self.force,
141 prefix=install_root,
142 dry_run=self.dry_run,
143 )
144 if self.optimize > 0:
145 byte_compile(
146 files,
147 optimize=self.optimize,
148 force=self.force,
149 prefix=install_root,
150 verbose=self.verbose,
151 dry_run=self.dry_run,
152 )
153
154 # -- Utility methods -----------------------------------------------
155
156 def _mutate_outputs(self, has_any, build_cmd, cmd_option, output_dir):
157 if not has_any:
158 return []
159
160 build_cmd = self.get_finalized_command(build_cmd)
161 build_files = build_cmd.get_outputs()
162 build_dir = getattr(build_cmd, cmd_option)
163
164 prefix_len = len(build_dir) + len(os.sep)
165 outputs = []
166 for file in build_files:
167 outputs.append(os.path.join(output_dir, file[prefix_len:]))
168
169 return outputs
170
171 def _bytecode_filenames(self, py_filenames):
172 bytecode_files = []
173 for py_file in py_filenames:
174 # Since build_py handles package data installation, the
175 # list of outputs can contain more than just .py files.
176 # Make sure we only report bytecode for the .py files.
177 ext = os.path.splitext(os.path.normcase(py_file))[1]
178 if ext != PYTHON_SOURCE_EXTENSION:
179 continue
180 if self.compile:
181 bytecode_files.append(
182 importlib.util.cache_from_source(py_file, optimization='')
183 )
184 if self.optimize > 0:
185 bytecode_files.append(
186 importlib.util.cache_from_source(
187 py_file, optimization=self.optimize
188 )
189 )
190
191 return bytecode_files
192
193 # -- External interface --------------------------------------------
194 # (called by outsiders)
195
196 def get_outputs(self):
197 """Return the list of files that would be installed if this command
198 were actually run. Not affected by the "dry-run" flag or whether
199 modules have actually been built yet.
200 """
201 pure_outputs = self._mutate_outputs(
202 self.distribution.has_pure_modules(),
203 'build_py',
204 'build_lib',
205 self.install_dir,
206 )
207 if self.compile:
208 bytecode_outputs = self._bytecode_filenames(pure_outputs)
209 else:
210 bytecode_outputs = []
211
212 ext_outputs = self._mutate_outputs(
213 self.distribution.has_ext_modules(),
214 'build_ext',
215 'build_lib',
216 self.install_dir,
217 )
218
219 return pure_outputs + bytecode_outputs + ext_outputs
220
221 def get_inputs(self):
222 """Get the list of files that are input to this command, ie. the
223 files that get installed as they are named in the build tree.
224 The files in this list correspond one-to-one to the output
225 filenames returned by 'get_outputs()'.
226 """
227 inputs = []
228
229 if self.distribution.has_pure_modules():
230 build_py = self.get_finalized_command('build_py')
231 inputs.extend(build_py.get_outputs())
232
233 if self.distribution.has_ext_modules():
234 build_ext = self.get_finalized_command('build_ext')
235 inputs.extend(build_ext.get_outputs())
236
237 return inputs