Skip to content

Commit 7ca8f36

Browse files
authored
[3.13] gh-151929: Get boot ID, machine ID and uptime in test.pythoninfo (#152127) (#152137)
gh-151929: Get boot ID, machine ID and uptime in test.pythoninfo (#152127) GHA: Run test.pythoninfo on the "Cross build Linux" job. (cherry picked from commit 3db3bba)
1 parent f74dbc8 commit 7ca8f36

1 file changed

Lines changed: 56 additions & 8 deletions

File tree

Lib/test/pythoninfo.py

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ def normalize_text(text):
1616
return text.strip()
1717

1818

19+
def read_first_line(filename):
20+
# Get the first line of a text file and strip trailing spaces
21+
try:
22+
with open(filename, encoding="utf-8") as fp:
23+
return fp.readline().rstrip()
24+
except OSError:
25+
return ''
26+
27+
1928
class PythonInfo:
2029
def __init__(self):
2130
self.info = {}
@@ -994,14 +1003,9 @@ def collect_fips(info_add):
9941003
if _hashlib is not None:
9951004
call_func(info_add, 'fips.openssl_fips_mode', _hashlib, 'get_fips_mode')
9961005

997-
try:
998-
with open("/proc/sys/crypto/fips_enabled", encoding="utf-8") as fp:
999-
line = fp.readline().rstrip()
1000-
1001-
if line:
1002-
info_add('fips.linux_crypto_fips_enabled', line)
1003-
except OSError:
1004-
pass
1006+
fips_enabled = read_first_line("/proc/sys/crypto/fips_enabled")
1007+
if fips_enabled:
1008+
info_add('fips.linux_crypto_fips_enabled', fips_enabled)
10051009

10061010

10071011
def collect_tempfile(info_add):
@@ -1019,6 +1023,49 @@ def collect_libregrtest_utils(info_add):
10191023
info_add('libregrtests.build_info', ' '.join(utils.get_build_info()))
10201024

10211025

1026+
def linux_get_uptime():
1027+
# Use CLOCK_BOOTTIME if available
1028+
import time
1029+
try:
1030+
return time.clock_gettime(time.CLOCK_BOOTTIME)
1031+
except (AttributeError, OSError):
1032+
pass
1033+
1034+
# Otherwise, parse the first member of /proc/uptime
1035+
uptime = read_first_line("/proc/uptime")
1036+
if not uptime:
1037+
return
1038+
try:
1039+
parts = uptime.split()
1040+
if not parts:
1041+
return
1042+
return float(parts[0])
1043+
except ValueError:
1044+
return
1045+
1046+
1047+
def collect_linux(info_add):
1048+
boot_id = read_first_line("/proc/sys/kernel/random/boot_id")
1049+
if boot_id:
1050+
info_add('linux.boot_id', boot_id)
1051+
1052+
# https://www.freedesktop.org/software/systemd/man/latest/machine-id.html
1053+
machine_id = read_first_line("/etc/machine-id")
1054+
if machine_id:
1055+
info_add('linux.machine_id', machine_id)
1056+
1057+
uptime = linux_get_uptime()
1058+
if uptime is not None:
1059+
# truncate microseconds
1060+
uptime = int(uptime)
1061+
try:
1062+
import datetime
1063+
uptime = str(datetime.timedelta(seconds=uptime))
1064+
except ImportError:
1065+
uptime = f'{uptime} sec'
1066+
info_add('linux.uptime', uptime)
1067+
1068+
10221069
def collect_info(info):
10231070
error = False
10241071
info_add = info.add
@@ -1059,6 +1106,7 @@ def collect_info(info):
10591106
collect_windows,
10601107
collect_zlib,
10611108
collect_libregrtest_utils,
1109+
collect_linux,
10621110

10631111
# Collecting from tests should be last as they have side effects.
10641112
collect_test_socket,

0 commit comments

Comments
 (0)