parse_os_release: Use /etc/os-release always; remove DISTRIB_FAMILY.

To replace DISTRIB_FAMILY, there’s now an os_families function using
the standard ID and ID_LIKE information in /etc/os-release.

Fixes #13070; fixes #13071.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
This commit is contained in:
Anders Kaseorg
2019-08-29 15:14:43 -07:00
committed by Tim Abbott
parent 875002108f
commit 096ef1445f
4 changed files with 55 additions and 89 deletions

View File

@@ -8,7 +8,7 @@ ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__f
if ZULIP_PATH not in sys.path:
sys.path.append(ZULIP_PATH)
from scripts.lib.zulip_tools import overwrite_symlink, run, parse_os_release
from scripts.lib.zulip_tools import os_families, overwrite_symlink, run, parse_os_release
from scripts.lib.setup_venv import (
setup_virtualenv, VENV_DEPENDENCIES, REDHAT_VENV_DEPENDENCIES,
FEDORA_VENV_DEPENDENCIES
@@ -20,18 +20,16 @@ args = parser.parse_args()
# install dependencies for setting up the virtualenv
distro_info = parse_os_release()
vendor = distro_info['ID']
family = distro_info['DISTRIB_FAMILY']
if family == 'debian':
if "debian" in os_families():
run(["apt-get", "-y", "install"] + VENV_DEPENDENCIES)
elif family == 'redhat':
if vendor in ["CentOS", "RedHat"]:
elif "fedora" in os_families():
if "rhel" in os_families():
_VENV_DEPS = REDHAT_VENV_DEPENDENCIES
elif vendor == "Fedora":
else:
_VENV_DEPS = FEDORA_VENV_DEPENDENCIES
run(["yum", "-y", "install"] + _VENV_DEPS)
else:
print("Unsupported platform: {}".format(family))
print("Unsupported platform: {}".format(distro_info['ID']))
sys.exit(1)
python_version = sys.version_info[0]

View File

@@ -8,7 +8,7 @@ ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__f
if ZULIP_PATH not in sys.path:
sys.path.append(ZULIP_PATH)
from scripts.lib.zulip_tools import run, parse_os_release
from scripts.lib.zulip_tools import os_families, run, parse_os_release
from scripts.lib.setup_venv import (
setup_virtualenv, THUMBOR_VENV_DEPENDENCIES, YUM_THUMBOR_VENV_DEPENDENCIES
)
@@ -19,13 +19,12 @@ args = parser.parse_args()
# install dependencies for setting up the virtualenv
distro_info = parse_os_release()
family = distro_info['DISTRIB_FAMILY']
if family == 'debian':
if "debian" in os_families():
run(["apt-get", "-y", "install"] + THUMBOR_VENV_DEPENDENCIES)
elif family == 'redhat':
elif "fedora" in os_families():
run(["yum", "-y", "install"] + YUM_THUMBOR_VENV_DEPENDENCIES)
else:
print("Unsupported platform: {}".format(family))
print("Unsupported platform: {}".format(distro_info['ID']))
sys.exit(1)
venv_name = "zulip-thumbor-venv"

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env python3
import argparse
import datetime
import functools
import hashlib
import logging
import os
@@ -337,12 +338,12 @@ def may_be_perform_purging(dirs_to_purge, dirs_to_keep, dir_type, dry_run, verbo
if verbose:
print("Keeping used %s: %s" % (dir_type, directory))
@functools.lru_cache(None)
def parse_os_release():
# type: () -> Dict[str, str]
"""
Example of the useful subset of the data:
{
'DISTRIB_FAMILY': 'debian'
'ID': 'ubuntu',
'VERSION_ID': '18.04',
'NAME': 'Ubuntu',
@@ -354,26 +355,6 @@ def parse_os_release():
we avoid using it, as it is not available on RHEL-based platforms.
"""
distro_info = {} # type: Dict[str, str]
if os.path.exists("/etc/redhat-release"):
with open('/etc/redhat-release', 'r') as fp:
info = fp.read().strip().split(' ')
vendor = info[0]
if vendor == 'CentOS':
# E.g. "CentOS Linux release 7.5.1804 (Core)"
os_version = vendor.lower() + info[3][0]
elif vendor == 'Fedora':
# E.g. "Fedora release 29 (Twenty Nine)"
os_version = vendor.lower() + info[2]
elif vendor == 'Red':
# E.g. "Red Hat Enterprise Linux Server release 7.6 (Maipo)"
vendor = 'RedHat'
os_version = 'rhel' + info[6][0] # 7
distro_info = dict(
VERSION_ID=os_version,
ID=vendor,
DISTRIB_FAMILY='redhat',
)
return distro_info
with open('/etc/os-release', 'r') as fp:
for line in fp:
line = line.strip()
@@ -383,9 +364,21 @@ def parse_os_release():
continue
k, v = line.split('=', 1)
[distro_info[k]] = shlex.split(v)
distro_info['DISTRIB_FAMILY'] = 'debian'
return distro_info
@functools.lru_cache(None)
def os_families() -> Set[str]:
"""
Known families:
debian (includes: debian, ubuntu)
ubuntu (includes: ubuntu)
fedora (includes: fedora, rhel, centos)
rhel (includes: rhel, centos)
centos (includes: centos)
"""
distro_info = parse_os_release()
return {distro_info["ID"], *distro_info.get("ID_LIKE", "").split()}
def file_or_package_hash_updated(paths, hash_name, is_force, package_versions=[]):
# type: (List[str], str, bool, List[str]) -> bool
# Check whether the files or package_versions passed as arguments