Skip to content

Commit 9470ad7

Browse files
authored
Merge pull request #74862 from swiftlang/egorzhdan/linux-os-version
[test] Retrieve Linux distro and version from `/etc/os-release`
2 parents c8acccf + b13d560 commit 9470ad7

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

test/lit.cfg

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2883,8 +2883,30 @@ def linux_get_lsb_release():
28832883
return (distributor, release)
28842884
return ('', '')
28852885

2886+
def linux_get_os_release():
2887+
try:
2888+
os_release_path = '/etc/os-release'
2889+
os_release_file = open(os_release_path)
2890+
except FileNotFoundError:
2891+
return '', ''
2892+
os_id = ''
2893+
os_release = ''
2894+
for line in os_release_file:
2895+
line = line.rstrip()
2896+
if not line or line.startswith('#'):
2897+
continue
2898+
if line.startswith('ID='):
2899+
os_id = line[len('ID='):].strip('\"')
2900+
if line.startswith('VERSION_ID='):
2901+
os_release = line[len('VERSION_ID='):].strip('\"')
2902+
return os_id, os_release
2903+
28862904
if platform.system() == 'Linux':
2887-
(distributor, release) = linux_get_lsb_release()
2905+
# Retrieve the Linux distro and version from `/etc/os-release`.
2906+
(distributor, release) = linux_get_os_release()
2907+
if distributor == '' or release == '':
2908+
# If `/etc/os-release` does not provide full results, fallback to `lsb_release`.
2909+
(distributor, release) = linux_get_lsb_release()
28882910
if distributor != '' and release != '':
28892911
config.available_features.add("LinuxDistribution=" + distributor + '-' + release)
28902912
lit_config.note('Running tests on %s-%s' % (distributor, release))

0 commit comments

Comments
 (0)