forked from jolly-good-toolbelt/jiratools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
show_env.py
executable file
·111 lines (89 loc) · 3.27 KB
/
show_env.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
#! /usr/bin/env python
"""
A simple module/program to show the behave banner and environment.
When invoked as a program, it shows the behave banner and environment.
When used as a module, it also defines a nice helper function:
execute_command_list - run commands in a list;
on failure, exit w/ helpful message and error status.
"""
import sys
from os import environ
from os.path import exists
from shutil import copyfileobj
from subprocess import CalledProcessError
from subprocess import check_call
# Define this here because it is needed by the other scripts.
def execute_command_list(commands_to_run, verbose=True):
"""
Execute each command in the list.
If any command fails, print a helpful message and exit with that status.
"""
for command in commands_to_run:
readable_command = " ".join(command)
try:
if verbose:
print(readable_command)
check_call(command)
except CalledProcessError as e:
print(
'"{}" - returned status code "{}"'.format(
readable_command, e.returncode
)
)
exit(e.returncode)
except FileNotFoundError as f:
print(
'"{}" - No such file/program: "{}"'.format(readable_command, f.filename)
)
exit(2)
BEHAVE_CONFIG_FILES = [".behaverc", "behave.ini"]
ENV_VAR_PREFIX = "BEHAVE_"
# Split BANNER in half because it has funky ascii that
# will interfere with Python .format() processing :-)
BANNER = r"""
[Given] [When] [Then]
_ ( ( ( \ .... /
___|_|___ ) ) ) / \
__| |__ .......... -- { } --
\ | |--| | |__ \ /
\| | )| | |_ \ / ) ( \
| |_)| | |_| | | |
| |--| | |__/ (----)
\_______/ |________| (----)
=== Behave Test Runner ==="""
BANNER2 = """
========================================================
BEHAVE Environment Configuration
--------------------------------------------------------
{}
{}
Environment variables:"""
END_BANNER = """\
========================================================"""
def show_env():
"""Display environment data."""
print(BANNER)
virtual_env = environ.get("VIRTUAL_ENV")
docker_env = environ.get("PRECONFIGURED_DOCKER")
if virtual_env is not None:
print(
BANNER2.format(
"Running in virtual environment:", environ.get("VIRTUAL_ENV")
)
)
elif docker_env is not None:
print(BANNER2.format("Preconfigured Docker environement", ""))
else:
print(BANNER2.format("No virtual environment!", ""))
for k, v in sorted(environ.items()):
if not k.startswith(ENV_VAR_PREFIX):
continue
print("{}={}".format(k, v))
for config_file in filter(exists, BEHAVE_CONFIG_FILES):
print()
print("Configuration from {}:".format(config_file))
with open(config_file) as f:
copyfileobj(f, sys.stdout)
print(END_BANNER)
if __name__ == "__main__":
show_env()