1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 This file contains functions used for telemetry.
14 import mozpack
.path
as mozpath
17 def cpu_brand_linux():
19 Read the CPU brand string out of /proc/cpuinfo on Linux.
21 with
open("/proc/cpuinfo", "r") as f
:
23 if line
.startswith("model name"):
24 _
, brand
= line
.split(": ", 1)
30 def cpu_brand_windows():
32 Read the CPU brand string from the registry on Windows.
37 import winreg
as _winreg
41 _winreg
.HKEY_LOCAL_MACHINE
,
42 r
"HARDWARE\DESCRIPTION\System\CentralProcessor\0",
44 (brand
, ty
) = _winreg
.QueryValueEx(h
, "ProcessorNameString")
45 if ty
== _winreg
.REG_SZ
:
54 Get the CPU brand string via sysctl on macos.
59 libc
= ctypes
.cdll
.LoadLibrary(ctypes
.util
.find_library("c"))
60 # First, find the required buffer size.
61 bufsize
= ctypes
.c_size_t(0)
62 result
= libc
.sysctlbyname(
63 b
"machdep.cpu.brand_string", None, ctypes
.byref(bufsize
), None, 0
68 buf
= ctypes
.create_string_buffer(bufsize
.value
)
69 # Now actually get the value.
70 result
= libc
.sysctlbyname(
71 b
"machdep.cpu.brand_string", buf
, ctypes
.byref(bufsize
), None, 0
76 return buf
.value
.decode()
81 Get the CPU brand string as returned by CPUID.
84 "Linux": cpu_brand_linux
,
85 "Windows": cpu_brand_windows
,
86 "Darwin": cpu_brand_mac
,
87 }.get(platform
.system(), lambda: None)()
90 def get_psutil_stats():
91 """Return whether psutil exists and its associated stats.
93 @returns (bool, int, int, int) whether psutil exists, the logical CPU count,
94 physical CPU count, and total number of bytes of memory.
102 psutil
.cpu_count(logical
=False),
103 psutil
.virtual_memory().total
,
106 return False, None, None, None
109 def filter_args(command
, argv
, topsrcdir
, topobjdir
, cwd
=None):
111 Given the full list of command-line arguments, remove anything up to and including `command`,
112 and attempt to filter absolute pathnames out of any arguments after that.
117 # Each key is a pathname and the values are replacement sigils
119 topsrcdir
: "$topsrcdir/",
120 topobjdir
: "$topobjdir/",
121 mozpath
.normpath(os
.path
.expanduser("~")): "$HOME/",
122 # This might override one of the existing entries, that's OK.
123 # We don't use a sigil here because we treat all arguments as potentially relative
124 # paths, so we'd like to get them back as they were specified.
125 mozpath
.normpath(cwd
): "",
135 p
= mozpath
.abspath(p
)
136 base
= mozpath
.basedir(p
, paths
.keys())
138 return paths
[base
] + mozpath
.relpath(p
, base
)
140 return "<path omitted>"
142 return [filter_path(arg
) for arg
in args
]
145 def get_distro_and_version():
146 if sys
.platform
.startswith("linux"):
147 dist
, version
, _
= distro
.linux_distribution(full_distribution_name
=False)
149 elif sys
.platform
.startswith("darwin"):
150 return "macos", platform
.mac_ver()[0]
151 elif sys
.platform
.startswith("win32") or sys
.platform
.startswith("msys"):
152 ver
= sys
.getwindowsversion()
153 return "windows", "%s.%s.%s" % (ver
.major
, ver
.minor
, ver
.build
)
155 return sys
.platform
, ""
158 def get_shell_info():
159 """Returns if the current shell was opened by vscode and if it's a SSH connection"""
162 True if "vscode" in os
.getenv("TERM_PROGRAM", "") else False,
163 bool(os
.getenv("SSH_CLIENT", False)),
167 def get_vscode_running():
168 """Return if the vscode is currently running."""
172 for proc
in psutil
.process_iter():
174 # On Windows we have "Code.exe"
175 # On MacOS we have "Code Helper (Renderer)"
176 # On Linux we have ""
178 proc
.name
== "Code.exe"
179 or proc
.name
== "Code Helper (Renderer)"
180 or proc
.name
== "code"
184 # may not be able to access process info for all processes
187 # On some platforms, sometimes, the generator throws an
188 # exception preventing us to enumerate.