Comment 4 for bug 1684600

Revision history for this message
Benjamin Drung (bdrung) wrote :

Code like

name = l.split('=', 1)[1]
if name.startswith('"'):
    name = name[1:-2].strip()

converts l = 'NAME="Debian GNU/Linux"' into name = 'Debian GNU/Linu' (note the missing 'x'). If this quote-stripping would have gone right, following snippet would remove GNU/Linux:

# work around inconsistent "Debian GNU/Linux" in os-release
if name.endswith('GNU/Linux'):
    name = name.split()[0:-1]

but the resulting name variable would be a list of strings then.

Proposed solution: Use shlex to simplify the code to

with open('/etc/os-release') as f:
    for l in f:
        key, value = shlex.split(l)[0].split('=', 1)
        if key == 'NAME':
            name = value[:-9].strip() if value.endswith('GNU/Linux') else value
        if key == 'VERSION_ID':
            version = value