/
environment.py
128 lines (96 loc) · 3.79 KB
/
environment.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
import getpass
import os
import subprocess
from cpuinfo.cpuinfo import _get_cpu_info_internal
from psutil import virtual_memory
from .subprocess_with_timeout import output_as_str
try:
from urllib.parse import urlparse
except ImportError:
# Python 2.7
from urlparse import urlparse
def _encode_str(out):
as_string = output_as_str(out)
if as_string and as_string[-1] == '\n':
as_string = as_string[:-1]
return as_string
def _exec(cmd):
try:
with open(os.devnull, 'w') as dev_null_f:
out = subprocess.check_output(cmd, stderr=dev_null_f)
except subprocess.CalledProcessError:
return None
return _encode_str(out)
_source = None
def determine_source_details():
global _source # pylint: disable=global-statement
if _source:
return _source
result = dict()
is_git_repo = _exec(['git', 'rev-parse']) is not None
if not is_git_repo:
result['repoURL'] = None
result['branchOrTag'] = None
result['commitId'] = None
result['commitMsg'] = None
result['authorName'] = None
result['committerName'] = None
result['authorEmail'] = None
result['committerEmail'] = None
_source = result
return result
repo_url = _exec(['git', 'ls-remote', '--get-url']) if is_git_repo else None
if repo_url is None:
repo_url = ''
parsed = urlparse(repo_url)
if parsed.password:
# remove password
parsed = parsed._replace(
netloc="{}@{}".format(parsed.username, parsed.hostname))
result['repoURL'] = _encode_str(parsed.geturl())
result['branchOrTag'] = _exec(['git', 'show', '-s', '--format=%D', 'HEAD'])
result['commitId'] = _exec(['git', 'rev-parse', 'HEAD'])
result['commitMsg'] = _exec(['git', 'show', '-s', '--format=%B', 'HEAD'])
result['authorName'] = _exec(['git', 'show', '-s', '--format=%aN', 'HEAD'])
result['committerName'] = _exec(['git', 'show', '-s', '--format=%cN', 'HEAD'])
result['authorEmail'] = _exec(['git', 'show', '-s', '--format=%aE', 'HEAD'])
result['committerEmail'] = _exec(['git', 'show', '-s', '--format=%cE', 'HEAD'])
_source = result
return result
_environment = None
def init_env_for_test():
global _environment # pylint: disable=global-statement
_environment = dict()
_environment['hostName'] = 'test'
_environment['userName'] = 'test'
def init_environment(denoise_result, ui):
result = dict()
result['userName'] = getpass.getuser()
result['manualRun'] = not ('CI' in os.environ and os.environ['CI'] == 'true')
u_name = os.uname()
result['hostName'] = u_name[1]
result['osType'] = u_name[0]
try:
cpu_info = _get_cpu_info_internal()
if cpu_info:
result['cpu'] = cpu_info['brand_raw']
result['clockSpeed'] = (cpu_info['hz_advertised'][0]
* (10 ** cpu_info['hz_advertised'][1]))
except ValueError:
pass
if 'cpu' not in result:
ui.warning('Was not able to determine the type of CPU used and its clock speed.'
+ ' Thus, these details will not be recorded with the data.')
result['memory'] = virtual_memory().total
result['software'] = []
result['software'].append({'name': 'kernel', 'version': u_name[3]})
result['software'].append({'name': 'kernel-release', 'version': u_name[2]})
result['software'].append({'name': 'architecture', 'version': u_name[4]})
result['denoise'] = denoise_result.details
global _environment # pylint: disable=global-statement
_environment = result
def determine_environment():
global _environment # pylint: disable=global-statement
if _environment:
return _environment
raise Exception("Environment was not initialized before accessing it.")