-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-env
More file actions
executable file
·107 lines (83 loc) · 3.33 KB
/
python-env
File metadata and controls
executable file
·107 lines (83 loc) · 3.33 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import logging
import os
import sys
from builtins import AttributeError, Exception
from python_env import create_env
PYTHON_VERSION = 'python{}'.format(sys.version_info.major)
def _add_commands(parser):
commands = parser.add_subparsers(title='Available Commands', help='command help')
# crawl command
crawl_command = commands.add_parser('setup', help='Setup your projects virtual env')
crawl_command.set_defaults(main=create_env.main)
def _add_global_options(parser):
global_options = parser.add_argument_group("Global Options")
verbosity_group = global_options.add_mutually_exclusive_group()
verbosity_group.add_argument(
'-q', '--quiet',
dest='verbosity',
action='store_const',
const=0,
default=1,
help='Only show errors.'
)
verbosity_group.add_argument(
'-v', '--verbose',
dest='verbosity',
action='count',
help='Increase verbosity. -v for INFO and -vv for DEBUG. Default: WARNING'
)
global_options.add_argument('-d', '--dir',
metavar='DIR',
default=os.environ.get('VIRTUAL_ENV_DIR', '.venv'),
help='The desired directory name for your virtual env. Default: "%(default)s"')
global_options.add_argument('-p', '--python',
metavar='PYTHON_EXE',
default=PYTHON_VERSION,
help="""
The Python interpreter to use, e.g.,
--python=python3.5 will use the python3.5 interpreter
to create the new environment. The default is the
interpreter that this tool is running with ({})
""".format(PYTHON_VERSION))
global_options.add_argument('-ni', '--non-interactive',
action='store_true',
help='Non-interactive mode. Will silently use all defaults. Default: False')
def _run_command(parser, logger, args):
try:
# noinspection PyProtectedMember
returncode = args.main(args)
except AttributeError as e:
parser.print_help()
return 1
except Exception as e:
logger.error('{}: {}'.format(e.__class__.__name__, str(e)))
returncode = 2
return returncode if returncode else 0
def main(argv=None):
argv = argv or sys.argv[1:]
parser = argparse.ArgumentParser()
_add_global_options(parser)
_add_commands(parser)
# Parse input
args = parser.parse_args(args=argv)
# Setup logging
logger = logging.getLogger()
assert args.verbosity >= 0, 'verbosity seems to be non-positive'
levels = [
logging.ERROR,
logging.WARNING,
logging.INFO,
logging.DEBUG,
]
level = logging.NOTSET
if args.verbosity < len(levels):
level = levels[args.verbosity]
_format = "%(asctime)s.%(msecs)03d [%(process)d]: %(name)s %(levelname)s: %(message)s"
logging.basicConfig(format=_format, level=level, datefmt='%Y-%m-%d %H:%M:%S', stream=sys.stderr)
# Execute the command
return _run_command(parser, logger, args)
if __name__ == '__main__':
sys.exit(main())