Skip to content

Commit

Permalink
replace LinuxPlatform._os() by distro
Browse files Browse the repository at this point in the history
  • Loading branch information
predat committed Sep 13, 2020
1 parent 4bbd9b1 commit c52ac3c
Showing 1 changed file with 4 additions and 102 deletions.
106 changes: 4 additions & 102 deletions src/rez/utils/platform_.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,108 +183,10 @@ class LinuxPlatform(_UnixPlatform):
name = "linux"

def _os(self):
distributor = None
release = None

def _str(s):
if (s.startswith("'") and s.endswith("'")) \
or (s.startswith('"') and s.endswith('"')):
return s[1:-1]
else:
return s

def _os():
if distributor and release:
return "%s-%s" % (distributor, release)
else:
return None

def _parse(txt, distributor_key, release_key):
distributor_ = None
release_ = None
lines = txt.strip().split('\n')
for line in lines:
if line.startswith(distributor_key):
s = line[len(distributor_key):].strip()
distributor_ = _str(s)
elif line.startswith(release_key):
s = line[len(release_key):].strip()
release_ = _str(s)
return distributor_, release_

# first try parsing the /etc/lsb-release file
file = "/etc/lsb-release"
if os.path.isfile(file):
with open(file) as f:
txt = f.read()
distributor, release = _parse(txt,
"DISTRIB_ID=",
"DISTRIB_RELEASE=")
result = _os()
if result:
return result

# next, try getting the output of the lsb_release program
import subprocess

p = Popen(
['/usr/bin/env', 'lsb_release', '-a'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
)
txt = p.communicate()[0]

if not p.returncode:
distributor_, release_ = _parse(txt,
"Distributor ID:",
"Release:")
if distributor_ and not distributor:
distributor = distributor_
if release_ and not release:
release = release_

result = _os()
if result:
return result

# try to read the /etc/os-release file
# this file contains OS specific data on linux
# distributions
# see https://www.freedesktop.org/software/systemd/man/os-release.html
os_release = '/etc/os-release'
if os.path.isfile(os_release):
with open(os_release, 'r') as f:
txt = f.read()
distributor_, release_ = _parse(txt,
"ID=",
"VERSION_ID=")
if distributor_ and not distributor:
distributor = distributor_
if release_ and not release:
release = release_

result = _os()
if result:
return result

# last, use python's dist detection. It is known to return incorrect
# info on some systems though
distributor_, release_, _ = distro.linux_distribution()

if distributor_ and not distributor:
distributor = distributor_
if release_ and not release:
release = release_

result = _os()
if result:
return result

# last resort, accept missing release
if distributor:
return distributor

# give up
raise RezSystemError("cannot detect operating system")
parts = distro.linux_distribution(full_distribution_name=False)
if parts[0] == '':
raise RezSystemError("cannot detect operating system")
return '-'.join(parts[:2])

def _terminal_emulator_command(self):
term = which("x-terminal-emulator", "xterm", "konsole")
Expand Down

0 comments on commit c52ac3c

Please sign in to comment.