Skip to content

Commit 8f1273d

Browse files
vstinnermiss-islington
authored andcommitted
pythongh-151929: Get boot ID, machine ID and uptime in test.pythoninfo (pythonGH-152127)
(cherry picked from commit 3db3bba) Co-authored-by: Victor Stinner <vstinner@python.org> GHA: Run test.pythoninfo on the "Cross build Linux" job.
1 parent ab8434a commit 8f1273d

2 files changed

Lines changed: 59 additions & 8 deletions

File tree

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,9 @@ jobs:
648648
run: ./configure --prefix="$BUILD_DIR/cross-python" --with-build-python="$BUILD_DIR/host-python/bin/python3"
649649
- name: Install cross Python
650650
run: make -j8 install
651+
- name: Display build info
652+
run: |
653+
"$BUILD_DIR/cross-python/bin/python3" -m test.pythoninfo
651654
- name: Run test subset with host build
652655
run: |
653656
"$BUILD_DIR/cross-python/bin/python3" -m test test_sysconfig test_site test_embed

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 = {}
@@ -1015,14 +1024,9 @@ def collect_fips(info_add):
10151024
if _hashlib is not None:
10161025
call_func(info_add, 'fips.openssl_fips_mode', _hashlib, 'get_fips_mode')
10171026

1018-
try:
1019-
with open("/proc/sys/crypto/fips_enabled", encoding="utf-8") as fp:
1020-
line = fp.readline().rstrip()
1021-
1022-
if line:
1023-
info_add('fips.linux_crypto_fips_enabled', line)
1024-
except OSError:
1025-
pass
1027+
fips_enabled = read_first_line("/proc/sys/crypto/fips_enabled")
1028+
if fips_enabled:
1029+
info_add('fips.linux_crypto_fips_enabled', fips_enabled)
10261030

10271031

10281032
def collect_tempfile(info_add):
@@ -1040,6 +1044,49 @@ def collect_libregrtest_utils(info_add):
10401044
info_add('libregrtests.build_info', ' '.join(utils.get_build_info()))
10411045

10421046

1047+
def linux_get_uptime():
1048+
# Use CLOCK_BOOTTIME if available
1049+
import time
1050+
try:
1051+
return time.clock_gettime(time.CLOCK_BOOTTIME)
1052+
except (AttributeError, OSError):
1053+
pass
1054+
1055+
# Otherwise, parse the first member of /proc/uptime
1056+
uptime = read_first_line("/proc/uptime")
1057+
if not uptime:
1058+
return
1059+
try:
1060+
parts = uptime.split()
1061+
if not parts:
1062+
return
1063+
return float(parts[0])
1064+
except ValueError:
1065+
return
1066+
1067+
1068+
def collect_linux(info_add):
1069+
boot_id = read_first_line("/proc/sys/kernel/random/boot_id")
1070+
if boot_id:
1071+
info_add('linux.boot_id', boot_id)
1072+
1073+
# https://www.freedesktop.org/software/systemd/man/latest/machine-id.html
1074+
machine_id = read_first_line("/etc/machine-id")
1075+
if machine_id:
1076+
info_add('linux.machine_id', machine_id)
1077+
1078+
uptime = linux_get_uptime()
1079+
if uptime is not None:
1080+
# truncate microseconds
1081+
uptime = int(uptime)
1082+
try:
1083+
import datetime
1084+
uptime = str(datetime.timedelta(seconds=uptime))
1085+
except ImportError:
1086+
uptime = f'{uptime} sec'
1087+
info_add('linux.uptime', uptime)
1088+
1089+
10431090
def collect_info(info):
10441091
error = False
10451092
info_add = info.add
@@ -1081,6 +1128,7 @@ def collect_info(info):
10811128
collect_zlib,
10821129
collect_zstd,
10831130
collect_libregrtest_utils,
1131+
collect_linux,
10841132

10851133
# Collecting from tests should be last as they have side effects.
10861134
collect_test_socket,

0 commit comments

Comments
 (0)