Skip to content

improved detection of distro_name() #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
45 changes: 37 additions & 8 deletions patchwork/info.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from fabric.api import run, hide
from fabric.contrib.files import exists
from patchwork.environment import has_binary


def distro_name():
"""
Return simple Linux distribution name identifier, e.g. ``"fedora"``.

Uses tools like ``/etc/issue``, and ``lsb_release`` and fits the remote
system into one of the following:
Uses files like ``/etc/os-release`` (or ``/etc/*-release``) and
tools like ``/etc/issue``, and ``lsb_release``, trying to identify
short id of the system. Examples:

* ``fedora``
* ``rhel``
Expand All @@ -15,11 +18,37 @@ def distro_name():
* ``debian``
* ``other``
"""
sentinel_files = {
'fedora': ('fedora-release',),
'centos': ('centos-release',),
}
for name, sentinels in sentinel_files.iteritems():
with hide('running', 'stdout'):
if has_binary('lsb_release'):
distro_id = run('lsb_release -s -i').strip().lower()
if distro_id:
return distro_id

if exists('/etc/lsb-release'):
distro_id = run('''awk -F '=' '$1 == "DISTRIB_ID" \
{print $2; exit }' \
/etc/lsb-release ''',
shell=False).strip().lower()
if distro_id:
return distro_id

if exists('/etc/os-release'):
distro_id = run('''awk -F '=' '$1 == "ID" \
{print $2; exit }' \
/etc/os-release''',
shell=False).strip().lower()
if distro_id:
return distro_id

# and now the fallback method (guess by existing files)
sentinel_files = (
('fedora', ('fedora-release',)),
('centos', ('centos-release',)),
Copy link
Member

Choose a reason for hiding this comment

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

Spun up a Centos 5.7 box today and noticed it only has /etc/redhat-release, no centos-release. (The file does contain "CentOS" though.)

Choose a reason for hiding this comment

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

That's true that I saw this file on CentOS 6.2 and havn't thinked to look at 5.X.

In all cases, RHEL and CentOS are similar and should works identically (and if it's a problem, I guess it must read redhat-release and parse it.

('debian', ('debian_version',)),
('gentoo', ('gentoo-release',)),
)

for name, sentinels in sentinel_files:
for sentinel in sentinels:
if exists('/etc/%s' % sentinel):
return name
Expand All @@ -34,7 +63,7 @@ def distro_family():

* ``debian``
* ``redhat``

If the system falls outside these categories, its specific family or
release name will be returned instead.
"""
Expand Down