forked from conda/conda
/
main.py
169 lines (131 loc) · 5.22 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
# (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
=========
package : create a conda package in an environment
Additional help for each command can be accessed by using:
conda <command> -h
"""
from __future__ import absolute_import, division, print_function, unicode_literals
import importlib
import sys
from argparse import SUPPRESS
from logging import CRITICAL, DEBUG, getLogger
from .. import __version__
log = getLogger(__name__)
def generate_parser():
from ..cli import conda_argparse
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' % __version__,
help="Show the conda version number and exit."
)
p.add_argument(
"--debug",
action="store_true",
help=SUPPRESS,
)
p.add_argument(
"--json",
action="store_true",
help=SUPPRESS,
)
sub_parsers = p.add_subparsers(
metavar='command',
dest='cmd',
)
# http://bugs.python.org/issue9253
# http://stackoverflow.com/a/18283730/1599393
sub_parsers.required = True
return p, sub_parsers
def _main(*args):
from ..base.constants import SEARCH_PATH
from ..base.context import context
from ..common.compat import on_win
from ..gateways.logging import set_all_logger_level, set_verbosity
from ..exceptions import CommandNotFoundError
if not args:
args = sys.argv
if not args:
args = sys.argv
log.debug("conda.cli.main called with %s", args)
if len(args) > 1:
argv1 = args[1].strip()
if argv1 in ('..activate', '..deactivate', '..checkenv', '..changeps1'):
import conda.cli.activate as activate
activate.main()
return
if argv1 in ('activate', 'deactivate'):
message = "'%s' is not a conda command.\n" % argv1
if not on_win:
message += ' Did you mean "source %s" ?\n' % ' '.join(args[1:])
raise CommandNotFoundError(argv1, message)
if len(args) == 1:
args.append('-h')
p, sub_parsers = generate_parser()
main_modules = ["info", "help", "list", "search", "create", "install", "update",
"remove", "config", "clean", "package"]
modules = ["conda.cli.main_"+suffix for suffix in main_modules]
for module in modules:
imported = importlib.import_module(module)
imported.configure_parser(sub_parsers)
if "update" in module:
imported.configure_parser(sub_parsers, name='upgrade')
if "remove" in module:
imported.configure_parser(sub_parsers, name='uninstall')
from conda.cli.find_commands import find_commands
def completer(prefix, **kwargs):
return [i for i in list(sub_parsers.choices) + find_commands()
if i.startswith(prefix)]
# when using sys.argv, first argument is generally conda or __main__.py. Ignore it.
if (any(sname in args[0] for sname in ('conda', 'conda.exe',
'__main__.py', 'conda-script.py')) and
(args[1] in list(sub_parsers.choices.keys()) + find_commands() or args[1].startswith('-'))):
log.debug("Ignoring first argument (%s), as it is not a subcommand", args[0])
args = args[1:]
sub_parsers.completer = completer
args = p.parse_args(args)
context.__init__(SEARCH_PATH, 'conda', args)
if getattr(args, 'json', False):
# # Silence logging info to avoid interfering with JSON output
# for logger in Logger.manager.loggerDict:
# if logger not in ('fetch', 'progress'):
# getLogger(logger).setLevel(CRITICAL + 1)
for logger in ('print', 'dotupdate', 'stdoutlog', 'stderrlog'):
getLogger(logger).setLevel(CRITICAL + 1)
if context.debug:
set_all_logger_level(DEBUG)
elif context.verbosity:
set_verbosity(context.verbosity)
log.debug("verbosity set to %s", context.verbosity)
exit_code = args.func(args, p)
if isinstance(exit_code, int):
return exit_code
def main(*args):
from ..exceptions import conda_exception_handler
return conda_exception_handler(_main, *args)
if __name__ == '__main__':
main()