/
print_versions.py
executable file
·127 lines (106 loc) · 3.59 KB
/
print_versions.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
# -*- coding: utf-8 -*-
# ======================================================================================
# Copyright (©) 2015-2023 LCS - Laboratoire Catalyse et Spectrochimie, Caen, France.
# CeCILL-B FREE SOFTWARE LICENSE AGREEMENT
# See full LICENSE agreement in the root directory.
# ======================================================================================
"""
Utility functions for printing version information.
"""
import locale
import platform
import struct
import subprocess
import sys
from os import environ
from pathlib import Path
__all__ = ["show_versions"]
def _get_sys_info():
"""Returns system information as a dict"""
# copied from XArray
from spectrochempy.utils.file import pathclean
REPOS = pathclean(__file__).parent.parent.parent
blob = []
# get full commit hash
commit = None
if (REPOS / ".git").is_dir() and REPOS.is_dir():
try:
pipe = subprocess.Popen(
'git log --format="%H" -n 1'.split(" "),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
so, _ = pipe.communicate()
except Exception:
pass
else:
if pipe.returncode == 0:
commit = so
try:
commit = so.decode("utf-8")
except ValueError:
pass
commit = commit.strip().strip('"')
blob.append(("commit", commit))
try:
(sysname, _nodename, release, _version, machine, processor) = platform.uname()
blob.extend(
[
("python", sys.version),
("python-bits", struct.calcsize("P") * 8),
("OS", f"{sysname}"),
("OS-release", f"{release}"),
("machine", f"{machine}"),
("processor", f"{processor}"),
("byteorder", f"{sys.byteorder}"),
("LC_ALL", f'{environ.get("LC_ALL", "None")}'),
("LANG", f'{environ.get("LANG", "None")}'),
("LOCALE", f"{locale.getlocale()}"),
]
)
except Exception:
pass
return blob
def show_versions(file=sys.stdout):
"""print the versions of spectrochempy and its dependencies
Parameters
----------
file : file-like, optional
print to the given file-like object. Defaults to sys.stdout.
"""
from spectrochempy.utils import optional
print("\nINSTALLED VERSIONS", file=file)
print("------------------", file=file)
for key, val in _get_sys_info():
print(f"{key}: {val}", file=file)
print(file=file)
# dependencies
deps = []
try:
# dev installation
reqs = (
Path(__file__).parent.parent.parent
/ "requirements"
/ "requirements_dev.txt"
)
print(" -- dev installation --")
except FileNotFoundError:
reqs = Path(__file__).parent.parent.parent / "requirements" / "requirements.txt"
reqs = reqs.read_text().split("\n")
for req in reqs:
req = req.strip()
if req == "" or req.startswith("#"):
continue
deps.append(req)
for dep in deps:
mod = optional.import_optional_dependency(dep, errors="ignore")
try:
print(
f"{dep}: "
f"{optional.get_module_version(mod) if mod is not None else None}",
file=file,
)
except ImportError:
print(f"{dep}: (Can't determine version string)", file=file)
if __name__ == "__main__": # pragma: no cover
show_versions()