|
| 1 | +# Copyright (C) 2022, François-Guillaume Fernandez. |
| 2 | + |
| 3 | +# This program is licensed under the Apache License version 2. |
| 4 | +# See LICENSE or go to <https://www.apache.org/licenses/LICENSE-2.0.txt> for full license details. |
| 5 | + |
| 6 | +""" |
| 7 | +Based on https://github.com/pytorch/pytorch/blob/master/torch/utils/collect_env.py |
| 8 | +This script outputs relevant system environment info |
| 9 | +Run it with `python collect_env.py`. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import absolute_import, division, print_function, unicode_literals |
| 13 | + |
| 14 | +import locale |
| 15 | +import re |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | +from collections import namedtuple |
| 19 | + |
| 20 | +PY3 = sys.version_info >= (3, 0) |
| 21 | + |
| 22 | + |
| 23 | +# System Environment Information |
| 24 | +SystemEnv = namedtuple( |
| 25 | + "SystemEnv", |
| 26 | + [ |
| 27 | + "os", |
| 28 | + "python_version", |
| 29 | + ], |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +def run(command): |
| 34 | + """Returns (return-code, stdout, stderr)""" |
| 35 | + p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 36 | + output, err = p.communicate() |
| 37 | + rc = p.returncode |
| 38 | + if PY3: |
| 39 | + enc = locale.getpreferredencoding() |
| 40 | + output = output.decode(enc) |
| 41 | + err = err.decode(enc) |
| 42 | + return rc, output.strip(), err.strip() |
| 43 | + |
| 44 | + |
| 45 | +def run_and_read_all(run_lambda, command): |
| 46 | + """Runs command using run_lambda; reads and returns entire output if rc is 0""" |
| 47 | + rc, out, _ = run_lambda(command) |
| 48 | + if rc != 0: |
| 49 | + return None |
| 50 | + return out |
| 51 | + |
| 52 | + |
| 53 | +def run_and_parse_first_match(run_lambda, command, regex): |
| 54 | + """Runs command using run_lambda, returns the first regex match if it exists""" |
| 55 | + rc, out, _ = run_lambda(command) |
| 56 | + if rc != 0: |
| 57 | + return None |
| 58 | + match = re.search(regex, out) |
| 59 | + if match is None: |
| 60 | + return None |
| 61 | + return match.group(1) |
| 62 | + |
| 63 | + |
| 64 | +def get_platform(): |
| 65 | + if sys.platform.startswith("linux"): |
| 66 | + return "linux" |
| 67 | + elif sys.platform.startswith("win32"): |
| 68 | + return "win32" |
| 69 | + elif sys.platform.startswith("cygwin"): |
| 70 | + return "cygwin" |
| 71 | + elif sys.platform.startswith("darwin"): |
| 72 | + return "darwin" |
| 73 | + else: |
| 74 | + return sys.platform |
| 75 | + |
| 76 | + |
| 77 | +def get_mac_version(run_lambda): |
| 78 | + return run_and_parse_first_match(run_lambda, "sw_vers -productVersion", r"(.*)") |
| 79 | + |
| 80 | + |
| 81 | +def get_windows_version(run_lambda): |
| 82 | + return run_and_read_all(run_lambda, "wmic os get Caption | findstr /v Caption") |
| 83 | + |
| 84 | + |
| 85 | +def get_lsb_version(run_lambda): |
| 86 | + return run_and_parse_first_match(run_lambda, "lsb_release -a", r"Description:\t(.*)") |
| 87 | + |
| 88 | + |
| 89 | +def check_release_file(run_lambda): |
| 90 | + return run_and_parse_first_match(run_lambda, "cat /etc/*-release", r'PRETTY_NAME="(.*)"') |
| 91 | + |
| 92 | + |
| 93 | +def get_os(run_lambda): |
| 94 | + platform = get_platform() |
| 95 | + |
| 96 | + if platform in ("win32", "cygwin"): |
| 97 | + return get_windows_version(run_lambda) |
| 98 | + |
| 99 | + if platform == "darwin": |
| 100 | + version = get_mac_version(run_lambda) |
| 101 | + if version is None: |
| 102 | + return None |
| 103 | + return "Mac OSX {}".format(version) |
| 104 | + |
| 105 | + if platform == "linux": |
| 106 | + # Ubuntu/Debian based |
| 107 | + desc = get_lsb_version(run_lambda) |
| 108 | + if desc is not None: |
| 109 | + return desc |
| 110 | + |
| 111 | + # Try reading /etc/*-release |
| 112 | + desc = check_release_file(run_lambda) |
| 113 | + if desc is not None: |
| 114 | + return desc |
| 115 | + |
| 116 | + return platform |
| 117 | + |
| 118 | + # Unknown platform |
| 119 | + return platform |
| 120 | + |
| 121 | + |
| 122 | +def get_env_info(): |
| 123 | + run_lambda = run |
| 124 | + |
| 125 | + return SystemEnv( |
| 126 | + python_version=".".join(map(str, sys.version_info[:3])), |
| 127 | + os=get_os(run_lambda), |
| 128 | + ) |
| 129 | + |
| 130 | + |
| 131 | +env_info_fmt = """ |
| 132 | +OS: {os} |
| 133 | +
|
| 134 | +Python version: {python_version} |
| 135 | +""".strip() |
| 136 | + |
| 137 | + |
| 138 | +def pretty_str(envinfo): |
| 139 | + def replace_nones(dct, replacement="Could not collect"): |
| 140 | + for key in dct.keys(): |
| 141 | + if dct[key] is not None: |
| 142 | + continue |
| 143 | + dct[key] = replacement |
| 144 | + return dct |
| 145 | + |
| 146 | + def replace_bools(dct, true="Yes", false="No"): |
| 147 | + for key in dct.keys(): |
| 148 | + if dct[key] is True: |
| 149 | + dct[key] = true |
| 150 | + elif dct[key] is False: |
| 151 | + dct[key] = false |
| 152 | + return dct |
| 153 | + |
| 154 | + def maybe_start_on_next_line(string): |
| 155 | + # If `string` is multiline, prepend a \n to it. |
| 156 | + if string is not None and len(string.split("\n")) > 1: |
| 157 | + return "\n{}\n".format(string) |
| 158 | + return string |
| 159 | + |
| 160 | + mutable_dict = envinfo._asdict() |
| 161 | + |
| 162 | + # Replace True with Yes, False with No |
| 163 | + mutable_dict = replace_bools(mutable_dict) |
| 164 | + |
| 165 | + # Replace all None objects with 'Could not collect' |
| 166 | + mutable_dict = replace_nones(mutable_dict) |
| 167 | + |
| 168 | + return env_info_fmt.format(**mutable_dict) |
| 169 | + |
| 170 | + |
| 171 | +def get_pretty_env_info(): |
| 172 | + """Collects environment information for debugging purposes |
| 173 | +
|
| 174 | + Returns: |
| 175 | + str: environment information |
| 176 | + """ |
| 177 | + return pretty_str(get_env_info()) |
| 178 | + |
| 179 | + |
| 180 | +def main(): |
| 181 | + print("Collecting environment information...") |
| 182 | + output = get_pretty_env_info() |
| 183 | + print(output) |
| 184 | + |
| 185 | + |
| 186 | +if __name__ == "__main__": |
| 187 | + main() |
0 commit comments