Use /etc/os-release if lsb_release is missing (#175)

This commit is contained in:
Tim Byrne 2019-11-30 10:52:18 -06:00
parent 96839a5743
commit 6bf0852609
No known key found for this signature in database
GPG Key ID: 14DB4FC2465A4B12
3 changed files with 34 additions and 16 deletions

View File

@ -1,26 +1,30 @@
"""Unit tests: query_distro""" """Unit tests: query_distro"""
import pytest
def test_lsb_release_present(runner, yadm, tst_distro): @pytest.mark.parametrize(
'condition', ['lsb_release', 'os-release', 'missing'])
def test_query_distro(runner, yadm, tst_distro, tmp_path, condition):
"""Match lsb_release -si when present""" """Match lsb_release -si when present"""
test_release = 'testrelease'
lsb_release = ''
os_release = tmp_path.joinpath('os-release')
if condition == 'os-release':
os_release.write_text(f"testing\nID={test_release}\nrelease")
if condition != 'lsb_release':
lsb_release = 'LSB_RELEASE_PROGRAM="missing_lsb_release"'
script = f""" script = f"""
YADM_TEST=1 source {yadm} YADM_TEST=1 source {yadm}
{lsb_release}
OS_RELEASE="{os_release}"
query_distro query_distro
""" """
run = runner(command=['bash'], inp=script) run = runner(command=['bash'], inp=script)
assert run.success assert run.success
assert run.err == '' assert run.err == ''
assert run.out.rstrip() == tst_distro if condition == 'lsb_release':
assert run.out.rstrip() == tst_distro
elif condition == 'os-release':
def test_lsb_release_missing(runner, yadm): assert run.out.rstrip() == test_release
"""Empty value when missing""" else:
script = f""" assert run.out.rstrip() == ''
YADM_TEST=1 source {yadm}
LSB_RELEASE_PROGRAM="missing_lsb_release"
query_distro
"""
run = runner(command=['bash'], inp=script)
assert run.success
assert run.err == ''
assert run.out.rstrip() == ''

8
yadm
View File

@ -45,6 +45,7 @@ J2CLI_PROGRAM="j2"
ENVTPL_PROGRAM="envtpl" ENVTPL_PROGRAM="envtpl"
LSB_RELEASE_PROGRAM="lsb_release" LSB_RELEASE_PROGRAM="lsb_release"
OS_RELEASE="/etc/os-release"
PROC_VERSION="/proc/version" PROC_VERSION="/proc/version"
OPERATING_SYSTEM="Unknown" OPERATING_SYSTEM="Unknown"
@ -1219,6 +1220,13 @@ function query_distro() {
distro="" distro=""
if command -v "$LSB_RELEASE_PROGRAM" >/dev/null 2>&1; then if command -v "$LSB_RELEASE_PROGRAM" >/dev/null 2>&1; then
distro=$($LSB_RELEASE_PROGRAM -si 2>/dev/null) distro=$($LSB_RELEASE_PROGRAM -si 2>/dev/null)
elif [ -f "$OS_RELEASE" ]; then
while IFS='' read -r line || [ -n "$line" ]; do
if [[ "$line" = ID=* ]]; then
distro="${line#ID=}"
break
fi
done < "$OS_RELEASE"
fi fi
echo "$distro" echo "$distro"
} }

8
yadm.1
View File

@ -477,7 +477,9 @@ Current user is calculated by running
.BR distro , " d .BR distro , " d
Valid if the value matches the distro. Valid if the value matches the distro.
Distro is calculated by running Distro is calculated by running
.BR "lsb_release -si" . .B "lsb_release -si"
or by inspecting the ID from
.BR "/etc/os-release" .
.TP .TP
.BR os , " o .BR os , " o
Valid if the value matches the OS. Valid if the value matches the OS.
@ -638,6 +640,10 @@ During processing, the following variables are available in the template:
The OS for "Windows Subsystem for Linux" is reported as "WSL", even The OS for "Windows Subsystem for Linux" is reported as "WSL", even
though uname identifies as "Linux". though uname identifies as "Linux".
.BR NOTE :
If lsb_release is not available, DISTRO will be the ID specified in
/etc/os-release.
Examples: Examples:
.I whatever##template .I whatever##template