forked from conda/conda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
229 lines (197 loc) · 7.76 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# (c) Continuum Analytics, Inc. / http://continuum.io
# All Rights Reserved
#
# conda is distributed under the terms of the BSD 3-clause license.
# Consult LICENSE.txt or http://opensource.org/licenses/BSD-3-Clause.
'''conda is a tool for managing environments and packages.
conda provides the following commands:
Information
===========
info : display information about the current install
list : list packages linked into a specified environment
search : print information about a specified package
help : display a list of available conda commands and their help
strings
Package Management
==================
create : create a new conda environment from a list of specified
packages
install : install new packages into an existing conda environment
update : update packages in a specified conda environment
Packaging
=========
build : build a package from recipe
package : create a conda package in an environment
index : updates repodata.json in channel directories
Additional help for each command can be accessed by using:
conda <command> -h
'''
from __future__ import print_function, division, absolute_import
import sys
import argparse
from conda.cli import common
from conda.cli import conda_argparse
from conda.cli import main_bundle
from conda.cli import main_create
from conda.cli import main_help
from conda.cli import main_init
from conda.cli import main_info
from conda.cli import main_install
from conda.cli import main_list
from conda.cli import main_remove
from conda.cli import main_package
from conda.cli import main_run
from conda.cli import main_search
from conda.cli import main_update
from conda.cli import main_config
from conda.cli import main_clean
def main():
if len(sys.argv) > 1:
argv1 = sys.argv[1]
if argv1 in ('..activate', '..deactivate', '..activateroot', '..checkenv'):
import conda.cli.activate as activate
activate.main()
return
if argv1 in ('..changeps1'):
import conda.cli.misc as misc
misc.main()
return
if argv1 == 'pip':
sys.exit("""ERROR:
The "conda pip" command has been removed from conda (as of version 1.8) for
the following reasons:
* users get the wrong impression that you *must* use conda pip (instead
of simply pip) when using Anaconda
* there should only be one preferred way to build packages, and that is
the conda build command
* the command did too many things at once, i.e. build a package and
then also install it
* the command is Python centric, whereas conda (from a package management
perspective) is Python agnostic
* packages created with conda pip are not robust, i.e. they will maybe
not work on other people's systems
In short:
* use "conda build" if you want to build a conda package
* use "conda install" if you want to install something
* use "pip" if you want to install something that is on PyPI for which there
isn't a conda package.
""")
if argv1 in ('activate', 'deactivate'):
sys.stderr.write("Error: '%s' is not a conda command.\n" % argv1)
if sys.platform != 'win32':
sys.stderr.write('Did you mean "source %s" ?\n' %
' '.join(sys.argv[1:]))
sys.exit(1)
# for backwards compatibility of conda-api
if sys.argv[1:4] == ['share', '--json', '--prefix']:
import json
from os.path import abspath
from conda.share import old_create_bundle
prefix = sys.argv[4]
path, warnings = old_create_bundle(abspath(prefix))
json.dump(dict(path=path, warnings=warnings),
sys.stdout, indent=2, sort_keys=True)
return
if sys.argv[1:4] == ['clone', '--json', '--prefix']:
import json
from os.path import abspath
from conda.share import old_clone_bundle
prefix, path = sys.argv[4:6]
old_clone_bundle(path, abspath(prefix))
json.dump(dict(warnings=[]), sys.stdout, indent=2)
return
if len(sys.argv) == 1:
sys.argv.append('-h')
import logging
import conda
p = conda_argparse.ArgumentParser(
description='conda is a tool for managing and deploying applications, environments and packages.'
)
p.add_argument(
'-V', '--version',
action = 'version',
version = 'conda %s' % conda.__version__,
)
p.add_argument(
"--debug",
action = "store_true",
help = argparse.SUPPRESS,
)
p.add_argument(
"--json",
action = "store_true",
help = argparse.SUPPRESS,
)
sub_parsers = p.add_subparsers(
metavar = 'command',
dest = 'cmd',
)
main_info.configure_parser(sub_parsers)
main_help.configure_parser(sub_parsers)
main_list.configure_parser(sub_parsers)
main_search.configure_parser(sub_parsers)
main_create.configure_parser(sub_parsers)
main_install.configure_parser(sub_parsers)
main_update.configure_parser(sub_parsers)
main_remove.configure_parser(sub_parsers)
main_remove.configure_parser(sub_parsers, name='uninstall')
main_run.configure_parser(sub_parsers)
main_config.configure_parser(sub_parsers)
main_init.configure_parser(sub_parsers)
main_clean.configure_parser(sub_parsers)
main_package.configure_parser(sub_parsers)
main_bundle.configure_parser(sub_parsers)
try:
import argcomplete
argcomplete.autocomplete(p)
except ImportError:
pass
except AttributeError:
# On Python 3.3, argcomplete can be an empty namespace package when
# we are in the conda-recipes directory.
pass
args = p.parse_args()
if getattr(args, 'json', False):
# Silence logging info to avoid interfering with JSON output
import logging
for logger in logging.Logger.manager.loggerDict:
if logger not in ('fetch', 'progress'):
logging.getLogger(logger).setLevel(logging.CRITICAL + 1)
if args.debug:
logging.disable(logging.NOTSET)
logging.basicConfig(level=logging.DEBUG)
if (not main_init.is_initialized() and
'init' not in sys.argv and 'info' not in sys.argv):
if hasattr(args, 'name') and hasattr(args, 'prefix'):
import conda.config as config
if common.get_prefix(args) == config.root_dir:
sys.exit("""\
Error: This installation of conda is not initialized. Use 'conda create -n
envname' to create a conda environment and 'source activate envname' to
activate it.
# Note that pip installing conda is not the recommended way for setting up your
# system. The recommended way for setting up a conda system is by installing
# Miniconda, see: http://repo.continuum.io/miniconda/index.html""")
args_func(args, p)
def args_func(args, p):
use_json = getattr(args, 'json', False)
try:
args.func(args, p)
except RuntimeError as e:
common.error_and_exit(str(e), json=use_json)
except Exception as e:
if e.__class__.__name__ not in ('ScannerError', 'ParserError'):
message = """\
An unexpected error has occurred, please consider sending the
following traceback to the conda GitHub issue tracker at:
https://github.com/conda/conda/issues
Include the output of the command 'conda info' in your report.
"""
if use_json:
import traceback
common.error_and_exit(message + traceback.format_exc(),
error_type="UnexpectedError", json=True)
print(message)
raise # as if we did not catch it
if __name__ == '__main__':
main()