ยปCore Development>Code coverage>Lib/packaging/command/install_dist.py

Python code coverage for Lib/packaging/command/install_dist.py

#countcontent
1n/a"""Main install command, which calls the other install_* commands."""
2n/a
3n/aimport sys
4n/aimport os
5n/a
6n/aimport sysconfig
7n/afrom sysconfig import get_config_vars, get_paths, get_path, get_config_var
8n/a
9n/afrom packaging import logger
10n/afrom packaging.command.cmd import Command
11n/afrom packaging.errors import PackagingPlatformError
12n/afrom packaging.util import write_file
13n/afrom packaging.util import convert_path, change_root, get_platform
14n/afrom packaging.errors import PackagingOptionError
15n/a
16n/a
17n/aclass install_dist(Command):
18n/a
19n/a description = "install everything from build directory"
20n/a
21n/a user_options = [
22n/a # Select installation scheme and set base director(y|ies)
23n/a ('prefix=', None,
24n/a "installation prefix"),
25n/a ('exec-prefix=', None,
26n/a "(Unix only) prefix for platform-specific files"),
27n/a ('user', None,
28n/a "install in user site-packages directory [%s]" %
29n/a get_path('purelib', '%s_user' % os.name)),
30n/a ('home=', None,
31n/a "(Unix only) home directory to install under"),
32n/a
33n/a # Or just set the base director(y|ies)
34n/a ('install-base=', None,
35n/a "base installation directory (instead of --prefix or --home)"),
36n/a ('install-platbase=', None,
37n/a "base installation directory for platform-specific files " +
38n/a "(instead of --exec-prefix or --home)"),
39n/a ('root=', None,
40n/a "install everything relative to this alternate root directory"),
41n/a
42n/a # Or explicitly set the installation scheme
43n/a ('install-purelib=', None,
44n/a "installation directory for pure Python module distributions"),
45n/a ('install-platlib=', None,
46n/a "installation directory for non-pure module distributions"),
47n/a ('install-lib=', None,
48n/a "installation directory for all module distributions " +
49n/a "(overrides --install-purelib and --install-platlib)"),
50n/a
51n/a ('install-headers=', None,
52n/a "installation directory for C/C++ headers"),
53n/a ('install-scripts=', None,
54n/a "installation directory for Python scripts"),
55n/a ('install-data=', None,
56n/a "installation directory for data files"),
57n/a
58n/a # Byte-compilation options -- see install_lib for details
59n/a ('compile', 'c', "compile .py to .pyc [default]"),
60n/a ('no-compile', None, "don't compile .py files"),
61n/a ('optimize=', 'O',
62n/a 'also compile with optimization: -O1 for "python -O", '
63n/a '-O2 for "python -OO", and -O0 to disable [default: -O0]'),
64n/a
65n/a # Miscellaneous control options
66n/a ('force', 'f',
67n/a "force installation (overwrite any existing files)"),
68n/a ('skip-build', None,
69n/a "skip rebuilding everything (for testing/debugging)"),
70n/a
71n/a # Where to install documentation (eventually!)
72n/a #('doc-format=', None, "format of documentation to generate"),
73n/a #('install-man=', None, "directory for Unix man pages"),
74n/a #('install-html=', None, "directory for HTML documentation"),
75n/a #('install-info=', None, "directory for GNU info files"),
76n/a
77n/a # XXX use a name that makes clear this is the old format
78n/a ('record=', None,
79n/a "filename in which to record a list of installed files "
80n/a "(not PEP 376-compliant)"),
81n/a ('resources=', None,
82n/a "data files mapping"),
83n/a
84n/a # .dist-info related arguments, read by install_dist_info
85n/a ('no-distinfo', None,
86n/a "do not create a .dist-info directory"),
87n/a ('installer=', None,
88n/a "the name of the installer"),
89n/a ('requested', None,
90n/a "generate a REQUESTED file (i.e."),
91n/a ('no-requested', None,
92n/a "do not generate a REQUESTED file"),
93n/a ('no-record', None,
94n/a "do not generate a RECORD file"),
95n/a ]
96n/a
97n/a boolean_options = ['compile', 'force', 'skip-build', 'no-distinfo',
98n/a 'requested', 'no-record', 'user']
99n/a
100n/a negative_opt = {'no-compile': 'compile', 'no-requested': 'requested'}
101n/a
102n/a def initialize_options(self):
103n/a # High-level options: these select both an installation base
104n/a # and scheme.
105n/a self.prefix = None
106n/a self.exec_prefix = None
107n/a self.home = None
108n/a self.user = False
109n/a
110n/a # These select only the installation base; it's up to the user to
111n/a # specify the installation scheme (currently, that means supplying
112n/a # the --install-{platlib,purelib,scripts,data} options).
113n/a self.install_base = None
114n/a self.install_platbase = None
115n/a self.root = None
116n/a
117n/a # These options are the actual installation directories; if not
118n/a # supplied by the user, they are filled in using the installation
119n/a # scheme implied by prefix/exec-prefix/home and the contents of
120n/a # that installation scheme.
121n/a self.install_purelib = None # for pure module distributions
122n/a self.install_platlib = None # non-pure (dists w/ extensions)
123n/a self.install_headers = None # for C/C++ headers
124n/a self.install_lib = None # set to either purelib or platlib
125n/a self.install_scripts = None
126n/a self.install_data = None
127n/a self.install_userbase = get_config_var('userbase')
128n/a self.install_usersite = get_path('purelib', '%s_user' % os.name)
129n/a
130n/a self.compile = None
131n/a self.optimize = None
132n/a
133n/a # These two are for putting non-packagized distributions into their
134n/a # own directory and creating a .pth file if it makes sense.
135n/a # 'extra_path' comes from the setup file; 'install_path_file' can
136n/a # be turned off if it makes no sense to install a .pth file. (But
137n/a # better to install it uselessly than to guess wrong and not
138n/a # install it when it's necessary and would be used!) Currently,
139n/a # 'install_path_file' is always true unless some outsider meddles
140n/a # with it.
141n/a self.extra_path = None
142n/a self.install_path_file = True
143n/a
144n/a # 'force' forces installation, even if target files are not
145n/a # out-of-date. 'skip_build' skips running the "build" command,
146n/a # handy if you know it's not necessary. 'warn_dir' (which is *not*
147n/a # a user option, it's just there so the bdist_* commands can turn
148n/a # it off) determines whether we warn about installing to a
149n/a # directory not in sys.path.
150n/a self.force = False
151n/a self.skip_build = False
152n/a self.warn_dir = True
153n/a
154n/a # These are only here as a conduit from the 'build' command to the
155n/a # 'install_*' commands that do the real work. ('build_base' isn't
156n/a # actually used anywhere, but it might be useful in future.) They
157n/a # are not user options, because if the user told the install
158n/a # command where the build directory is, that wouldn't affect the
159n/a # build command.
160n/a self.build_base = None
161n/a self.build_lib = None
162n/a
163n/a # Not defined yet because we don't know anything about
164n/a # documentation yet.
165n/a #self.install_man = None
166n/a #self.install_html = None
167n/a #self.install_info = None
168n/a
169n/a self.record = None
170n/a self.resources = None
171n/a
172n/a # .dist-info related options
173n/a self.no_distinfo = None
174n/a self.installer = None
175n/a self.requested = None
176n/a self.no_record = None
177n/a
178n/a # -- Option finalizing methods -------------------------------------
179n/a # (This is rather more involved than for most commands,
180n/a # because this is where the policy for installing third-
181n/a # party Python modules on various platforms given a wide
182n/a # array of user input is decided. Yes, it's quite complex!)
183n/a
184n/a def finalize_options(self):
185n/a # This method (and its pliant slaves, like 'finalize_unix()',
186n/a # 'finalize_other()', and 'select_scheme()') is where the default
187n/a # installation directories for modules, extension modules, and
188n/a # anything else we care to install from a Python module
189n/a # distribution. Thus, this code makes a pretty important policy
190n/a # statement about how third-party stuff is added to a Python
191n/a # installation! Note that the actual work of installation is done
192n/a # by the relatively simple 'install_*' commands; they just take
193n/a # their orders from the installation directory options determined
194n/a # here.
195n/a
196n/a # Check for errors/inconsistencies in the options; first, stuff
197n/a # that's wrong on any platform.
198n/a
199n/a if ((self.prefix or self.exec_prefix or self.home) and
200n/a (self.install_base or self.install_platbase)):
201n/a raise PackagingOptionError(
202n/a "must supply either prefix/exec-prefix/home or "
203n/a "install-base/install-platbase -- not both")
204n/a
205n/a if self.home and (self.prefix or self.exec_prefix):
206n/a raise PackagingOptionError(
207n/a "must supply either home or prefix/exec-prefix -- not both")
208n/a
209n/a if self.user and (self.prefix or self.exec_prefix or self.home or
210n/a self.install_base or self.install_platbase):
211n/a raise PackagingOptionError(
212n/a "can't combine user with prefix/exec_prefix/home or "
213n/a "install_base/install_platbase")
214n/a
215n/a # Next, stuff that's wrong (or dubious) only on certain platforms.
216n/a if os.name != "posix":
217n/a if self.exec_prefix:
218n/a logger.warning(
219n/a '%s: exec-prefix option ignored on this platform',
220n/a self.get_command_name())
221n/a self.exec_prefix = None
222n/a
223n/a # Now the interesting logic -- so interesting that we farm it out
224n/a # to other methods. The goal of these methods is to set the final
225n/a # values for the install_{lib,scripts,data,...} options, using as
226n/a # input a heady brew of prefix, exec_prefix, home, install_base,
227n/a # install_platbase, user-supplied versions of
228n/a # install_{purelib,platlib,lib,scripts,data,...}, and the
229n/a # INSTALL_SCHEME dictionary above. Phew!
230n/a
231n/a self.dump_dirs("pre-finalize_{unix,other}")
232n/a
233n/a if os.name == 'posix':
234n/a self.finalize_unix()
235n/a else:
236n/a self.finalize_other()
237n/a
238n/a self.dump_dirs("post-finalize_{unix,other}()")
239n/a
240n/a # Expand configuration variables, tilde, etc. in self.install_base
241n/a # and self.install_platbase -- that way, we can use $base or
242n/a # $platbase in the other installation directories and not worry
243n/a # about needing recursive variable expansion (shudder).
244n/a
245n/a py_version = '%s.%s' % sys.version_info[:2]
246n/a prefix, exec_prefix, srcdir, projectbase = get_config_vars(
247n/a 'prefix', 'exec_prefix', 'srcdir', 'projectbase')
248n/a
249n/a metadata = self.distribution.metadata
250n/a self.config_vars = {
251n/a 'dist_name': metadata['Name'],
252n/a 'dist_version': metadata['Version'],
253n/a 'dist_fullname': metadata.get_fullname(),
254n/a 'py_version': py_version,
255n/a 'py_version_short': py_version[:3],
256n/a 'py_version_nodot': py_version[:3:2],
257n/a 'sys_prefix': prefix,
258n/a 'prefix': prefix,
259n/a 'sys_exec_prefix': exec_prefix,
260n/a 'exec_prefix': exec_prefix,
261n/a 'srcdir': srcdir,
262n/a 'projectbase': projectbase,
263n/a 'userbase': self.install_userbase,
264n/a 'usersite': self.install_usersite,
265n/a }
266n/a
267n/a self.expand_basedirs()
268n/a
269n/a self.dump_dirs("post-expand_basedirs()")
270n/a
271n/a # Now define config vars for the base directories so we can expand
272n/a # everything else.
273n/a self.config_vars['base'] = self.install_base
274n/a self.config_vars['platbase'] = self.install_platbase
275n/a
276n/a # Expand "~" and configuration variables in the installation
277n/a # directories.
278n/a self.expand_dirs()
279n/a
280n/a self.dump_dirs("post-expand_dirs()")
281n/a
282n/a # Create directories under USERBASE
283n/a if self.user:
284n/a self.create_user_dirs()
285n/a
286n/a # Pick the actual directory to install all modules to: either
287n/a # install_purelib or install_platlib, depending on whether this
288n/a # module distribution is pure or not. Of course, if the user
289n/a # already specified install_lib, use their selection.
290n/a if self.install_lib is None:
291n/a if self.distribution.ext_modules: # has extensions: non-pure
292n/a self.install_lib = self.install_platlib
293n/a else:
294n/a self.install_lib = self.install_purelib
295n/a
296n/a # Convert directories from Unix /-separated syntax to the local
297n/a # convention.
298n/a self.convert_paths('lib', 'purelib', 'platlib', 'scripts',
299n/a 'data', 'headers', 'userbase', 'usersite')
300n/a
301n/a # Well, we're not actually fully completely finalized yet: we still
302n/a # have to deal with 'extra_path', which is the hack for allowing
303n/a # non-packagized module distributions (hello, Numerical Python!) to
304n/a # get their own directories.
305n/a self.handle_extra_path()
306n/a self.install_libbase = self.install_lib # needed for .pth file
307n/a self.install_lib = os.path.join(self.install_lib, self.extra_dirs)
308n/a
309n/a # If a new root directory was supplied, make all the installation
310n/a # dirs relative to it.
311n/a if self.root is not None:
312n/a self.change_roots('libbase', 'lib', 'purelib', 'platlib',
313n/a 'scripts', 'data', 'headers')
314n/a
315n/a self.dump_dirs("after prepending root")
316n/a
317n/a # Find out the build directories, ie. where to install from.
318n/a self.set_undefined_options('build', 'build_base', 'build_lib')
319n/a
320n/a # Punt on doc directories for now -- after all, we're punting on
321n/a # documentation completely!
322n/a
323n/a if self.no_distinfo is None:
324n/a self.no_distinfo = False
325n/a
326n/a def finalize_unix(self):
327n/a """Finalize options for posix platforms."""
328n/a if self.install_base is not None or self.install_platbase is not None:
329n/a if ((self.install_lib is None and
330n/a self.install_purelib is None and
331n/a self.install_platlib is None) or
332n/a self.install_headers is None or
333n/a self.install_scripts is None or
334n/a self.install_data is None):
335n/a raise PackagingOptionError(
336n/a "install-base or install-platbase supplied, but "
337n/a "installation scheme is incomplete")
338n/a return
339n/a
340n/a if self.user:
341n/a if self.install_userbase is None:
342n/a raise PackagingPlatformError(
343n/a "user base directory is not specified")
344n/a self.install_base = self.install_platbase = self.install_userbase
345n/a self.select_scheme("posix_user")
346n/a elif self.home is not None:
347n/a self.install_base = self.install_platbase = self.home
348n/a self.select_scheme("posix_home")
349n/a else:
350n/a if self.prefix is None:
351n/a if self.exec_prefix is not None:
352n/a raise PackagingOptionError(
353n/a "must not supply exec-prefix without prefix")
354n/a
355n/a self.prefix = os.path.normpath(sys.prefix)
356n/a self.exec_prefix = os.path.normpath(sys.exec_prefix)
357n/a
358n/a else:
359n/a if self.exec_prefix is None:
360n/a self.exec_prefix = self.prefix
361n/a
362n/a self.install_base = self.prefix
363n/a self.install_platbase = self.exec_prefix
364n/a self.select_scheme("posix_prefix")
365n/a
366n/a def finalize_other(self):
367n/a """Finalize options for non-posix platforms"""
368n/a if self.user:
369n/a if self.install_userbase is None:
370n/a raise PackagingPlatformError(
371n/a "user base directory is not specified")
372n/a self.install_base = self.install_platbase = self.install_userbase
373n/a self.select_scheme(os.name + "_user")
374n/a elif self.home is not None:
375n/a self.install_base = self.install_platbase = self.home
376n/a self.select_scheme("posix_home")
377n/a else:
378n/a if self.prefix is None:
379n/a self.prefix = os.path.normpath(sys.prefix)
380n/a
381n/a self.install_base = self.install_platbase = self.prefix
382n/a try:
383n/a self.select_scheme(os.name)
384n/a except KeyError:
385n/a raise PackagingPlatformError(
386n/a "no support for installation on '%s'" % os.name)
387n/a
388n/a def dump_dirs(self, msg):
389n/a """Dump the list of user options."""
390n/a logger.debug(msg + ":")
391n/a for opt in self.user_options:
392n/a opt_name = opt[0]
393n/a if opt_name[-1] == "=":
394n/a opt_name = opt_name[0:-1]
395n/a if opt_name in self.negative_opt:
396n/a opt_name = self.negative_opt[opt_name]
397n/a opt_name = opt_name.replace('-', '_')
398n/a val = not getattr(self, opt_name)
399n/a else:
400n/a opt_name = opt_name.replace('-', '_')
401n/a val = getattr(self, opt_name)
402n/a logger.debug(" %s: %s", opt_name, val)
403n/a
404n/a def select_scheme(self, name):
405n/a """Set the install directories by applying the install schemes."""
406n/a # it's the caller's problem if they supply a bad name!
407n/a scheme = get_paths(name, expand=False)
408n/a for key, value in scheme.items():
409n/a if key == 'platinclude':
410n/a key = 'headers'
411n/a value = os.path.join(value, self.distribution.metadata['Name'])
412n/a attrname = 'install_' + key
413n/a if hasattr(self, attrname):
414n/a if getattr(self, attrname) is None:
415n/a setattr(self, attrname, value)
416n/a
417n/a def _expand_attrs(self, attrs):
418n/a for attr in attrs:
419n/a val = getattr(self, attr)
420n/a if val is not None:
421n/a if os.name == 'posix' or os.name == 'nt':
422n/a val = os.path.expanduser(val)
423n/a # see if we want to push this work in sysconfig XXX
424n/a val = sysconfig._subst_vars(val, self.config_vars)
425n/a setattr(self, attr, val)
426n/a
427n/a def expand_basedirs(self):
428n/a """Call `os.path.expanduser` on install_{base,platbase} and root."""
429n/a self._expand_attrs(['install_base', 'install_platbase', 'root'])
430n/a
431n/a def expand_dirs(self):
432n/a """Call `os.path.expanduser` on install dirs."""
433n/a self._expand_attrs(['install_purelib', 'install_platlib',
434n/a 'install_lib', 'install_headers',
435n/a 'install_scripts', 'install_data'])
436n/a
437n/a def convert_paths(self, *names):
438n/a """Call `convert_path` over `names`."""
439n/a for name in names:
440n/a attr = "install_" + name
441n/a setattr(self, attr, convert_path(getattr(self, attr)))
442n/a
443n/a def handle_extra_path(self):
444n/a """Set `path_file` and `extra_dirs` using `extra_path`."""
445n/a if self.extra_path is None:
446n/a self.extra_path = self.distribution.extra_path
447n/a
448n/a if self.extra_path is not None:
449n/a if isinstance(self.extra_path, str):
450n/a self.extra_path = self.extra_path.split(',')
451n/a
452n/a if len(self.extra_path) == 1:
453n/a path_file = extra_dirs = self.extra_path[0]
454n/a elif len(self.extra_path) == 2:
455n/a path_file, extra_dirs = self.extra_path
456n/a else:
457n/a raise PackagingOptionError(
458n/a "'extra_path' option must be a list, tuple, or "
459n/a "comma-separated string with 1 or 2 elements")
460n/a
461n/a # convert to local form in case Unix notation used (as it
462n/a # should be in setup scripts)
463n/a extra_dirs = convert_path(extra_dirs)
464n/a else:
465n/a path_file = None
466n/a extra_dirs = ''
467n/a
468n/a # XXX should we warn if path_file and not extra_dirs? (in which
469n/a # case the path file would be harmless but pointless)
470n/a self.path_file = path_file
471n/a self.extra_dirs = extra_dirs
472n/a
473n/a def change_roots(self, *names):
474n/a """Change the install direcories pointed by name using root."""
475n/a for name in names:
476n/a attr = "install_" + name
477n/a setattr(self, attr, change_root(self.root, getattr(self, attr)))
478n/a
479n/a def create_user_dirs(self):
480n/a """Create directories under USERBASE as needed."""
481n/a home = convert_path(os.path.expanduser("~"))
482n/a for name, path in self.config_vars.items():
483n/a if path.startswith(home) and not os.path.isdir(path):
484n/a os.makedirs(path, 0o700)
485n/a
486n/a # -- Command execution methods -------------------------------------
487n/a
488n/a def run(self):
489n/a """Runs the command."""
490n/a # Obviously have to build before we can install
491n/a if not self.skip_build:
492n/a self.run_command('build')
493n/a # If we built for any other platform, we can't install.
494n/a build_plat = self.distribution.get_command_obj('build').plat_name
495n/a # check warn_dir - it is a clue that the 'install_dist' is happening
496n/a # internally, and not to sys.path, so we don't check the platform
497n/a # matches what we are running.
498n/a if self.warn_dir and build_plat != get_platform():
499n/a raise PackagingPlatformError("Can't install when "
500n/a "cross-compiling")
501n/a
502n/a # Run all sub-commands (at least those that need to be run)
503n/a for cmd_name in self.get_sub_commands():
504n/a self.run_command(cmd_name)
505n/a
506n/a if self.path_file:
507n/a self.create_path_file()
508n/a
509n/a # write list of installed files, if requested.
510n/a if self.record:
511n/a outputs = self.get_outputs()
512n/a if self.root: # strip any package prefix
513n/a root_len = len(self.root)
514n/a for counter in range(len(outputs)):
515n/a outputs[counter] = outputs[counter][root_len:]
516n/a self.execute(write_file,
517n/a (self.record, outputs),
518n/a "writing list of installed files to '%s'" %
519n/a self.record)
520n/a
521n/a normpath, normcase = os.path.normpath, os.path.normcase
522n/a sys_path = [normcase(normpath(p)) for p in sys.path]
523n/a install_lib = normcase(normpath(self.install_lib))
524n/a if (self.warn_dir and
525n/a not (self.path_file and self.install_path_file) and
526n/a install_lib not in sys_path):
527n/a logger.debug(("modules installed to '%s', which is not in "
528n/a "Python's module search path (sys.path) -- "
529n/a "you'll have to change the search path yourself"),
530n/a self.install_lib)
531n/a
532n/a def create_path_file(self):
533n/a """Creates the .pth file"""
534n/a filename = os.path.join(self.install_libbase,
535n/a self.path_file + ".pth")
536n/a if self.install_path_file:
537n/a self.execute(write_file,
538n/a (filename, [self.extra_dirs]),
539n/a "creating %s" % filename)
540n/a else:
541n/a logger.warning('%s: path file %r not created',
542n/a self.get_command_name(), filename)
543n/a
544n/a # -- Reporting methods ---------------------------------------------
545n/a
546n/a def get_outputs(self):
547n/a """Assembles the outputs of all the sub-commands."""
548n/a outputs = []
549n/a for cmd_name in self.get_sub_commands():
550n/a cmd = self.get_finalized_command(cmd_name)
551n/a # Add the contents of cmd.get_outputs(), ensuring
552n/a # that outputs doesn't contain duplicate entries
553n/a for filename in cmd.get_outputs():
554n/a if filename not in outputs:
555n/a outputs.append(filename)
556n/a
557n/a if self.path_file and self.install_path_file:
558n/a outputs.append(os.path.join(self.install_libbase,
559n/a self.path_file + ".pth"))
560n/a
561n/a return outputs
562n/a
563n/a def get_inputs(self):
564n/a """Returns the inputs of all the sub-commands"""
565n/a # XXX gee, this looks familiar ;-(
566n/a inputs = []
567n/a for cmd_name in self.get_sub_commands():
568n/a cmd = self.get_finalized_command(cmd_name)
569n/a inputs.extend(cmd.get_inputs())
570n/a
571n/a return inputs
572n/a
573n/a # -- Predicates for sub-command list -------------------------------
574n/a
575n/a def has_lib(self):
576n/a """Returns true if the current distribution has any Python
577n/a modules to install."""
578n/a return (self.distribution.has_pure_modules() or
579n/a self.distribution.has_ext_modules())
580n/a
581n/a def has_headers(self):
582n/a """Returns true if the current distribution has any headers to
583n/a install."""
584n/a return self.distribution.has_headers()
585n/a
586n/a def has_scripts(self):
587n/a """Returns true if the current distribution has any scripts to.
588n/a install."""
589n/a return self.distribution.has_scripts()
590n/a
591n/a def has_data(self):
592n/a """Returns true if the current distribution has any data to.
593n/a install."""
594n/a return self.distribution.has_data_files()
595n/a
596n/a # 'sub_commands': a list of commands this command might have to run to
597n/a # get its work done. See cmd.py for more info.
598n/a sub_commands = [('install_lib', has_lib),
599n/a ('install_headers', has_headers),
600n/a ('install_scripts', has_scripts),
601n/a ('install_data', has_data),
602n/a # keep install_distinfo last, as it needs the record
603n/a # with files to be completely generated
604n/a ('install_distinfo', lambda self: not self.no_distinfo),
605n/a ]