Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/distro/distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,10 +1185,22 @@ def _parse_os_release_content(lines: TextIO) -> Dict[str, str]:
A dictionary containing all information items.
"""
props = {}
lexer = shlex.shlex(lines, posix=True)
lexer.whitespace_split = True
try:
lexer = shlex.shlex(lines, posix=True)
lexer.whitespace_split = True
tokens = list(lexer)
except ValueError:
lines.seek(0)
tokens = []
for line in lines:
line = line.strip()
if not line or line.startswith("#"):
continue
if "=" in line:
k, v = line.split("=", 1)
clean_v = v.strip("'\"")
tokens.append(f"{k}={clean_v}")

tokens = list(lexer)
for token in tokens:
# At this point, all shell-like parsing has been done (i.e.
# comments processed, quotes and backslash escape sequences
Expand Down
8 changes: 8 additions & 0 deletions tests/test_distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -2647,6 +2647,14 @@ def test_kx_02(self) -> None:
assert props.get("key1", None) is None
assert props.get("key2", None) == "value 2"

def test_kx_03_unclosed_quotes(self) -> None:
props = self.distro._parse_os_release_content(
io.StringIO('NAME="Ubuntu\nID=ubuntu\nVERSION_ID="24.04"\n')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
io.StringIO('NAME="Ubuntu\nID=ubuntu\nVERSION_ID="24.04"\n')
io.StringIO(
'NAME="Ubuntu\n'
'ID=ubuntu\n'
'VERSION_ID="24.04"\n'
'# Comment\n'
'MISSING_ASSIGNMENT\n'
)

picky 🎩 on : two more testing lines to keep (new) code coverage 100%

)
assert props.get("name", None) == "Ubuntu"
assert props.get("id", None) == "ubuntu"
assert props.get("version_id", None) == "24.04"


@pytest.mark.skipif(not IS_LINUX, reason="Irrelevant on non-linux")
class TestGlobal:
Expand Down
Loading