/
pip.py
70 lines (61 loc) · 2.63 KB
/
pip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import absolute_import
import os
import os.path as op
from conda._vendor.auxlib.compat import Utf8NamedTemporaryFile
from conda.gateways.connection.session import CONDA_SESSION_SCHEMES
from conda_env.pip_util import pip_subprocess, get_pip_installed_packages
from conda.common.io import Spinner
from conda.base.context import context
from logging import getLogger
log = getLogger(__name__)
def _pip_install_via_requirements(prefix, specs, args, *_, **kwargs):
"""
Installs the pip dependencies in specs using a temporary pip requirements file.
Args
----
prefix: string
The path to the python and pip executables.
specs: iterable of strings
Each element should be a valid pip dependency.
See: https://pip.pypa.io/en/stable/user_guide/#requirements-files
https://pip.pypa.io/en/stable/reference/pip_install/#requirements-file-format
"""
url_scheme = args.file.split("://", 1)[0]
if url_scheme in CONDA_SESSION_SCHEMES:
pip_workdir = None
else:
try:
pip_workdir = op.dirname(op.abspath(args.file))
except AttributeError:
pip_workdir = None
requirements = None
try:
# Generate the temporary requirements file
requirements = Utf8NamedTemporaryFile(mode='w',
prefix='condaenv.',
suffix='.requirements.txt',
dir=pip_workdir,
delete=False)
requirements.write('\n'.join(specs))
requirements.close()
# pip command line...
pip_cmd = ['install', '-U', '-r', requirements.name]
stdout, stderr = pip_subprocess(pip_cmd, prefix, cwd=pip_workdir)
finally:
# Win/Appveyor does not like it if we use context manager + delete=True.
# So we delete the temporary file in a finally block.
if requirements is not None and op.isfile(requirements.name):
if 'CONDA_TEST_SAVE_TEMPS' not in os.environ:
os.remove(requirements.name)
else:
log.warning('CONDA_TEST_SAVE_TEMPS :: retaining pip requirements.txt {}'
.format(requirements.name))
return get_pip_installed_packages(stdout)
def install(*args, **kwargs):
with Spinner("Installing pip dependencies",
not context.verbosity and not context.quiet,
context.json):
return _pip_install_via_requirements(*args, **kwargs)