forked from RedisGears/RedisGears
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetos.py
executable file
·145 lines (131 loc) · 5.11 KB
/
getos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python2
from __future__ import absolute_import
import platform
import os
import re
class Platform:
class OSRelease():
def __init__(self):
self.defs = {}
with open("/etc/os-release") as f:
for line in f:
try:
k, v = line.rstrip().split("=")
self.defs[k] = v.strip('"').strip("'")
except:
pass
def distname(self):
return self.defs["ID"].lower()
def version(self):
return self.defs["VERSION_ID"]
def osnick(self):
try:
return self.defs["VERSION_CODENAME"]
except:
return ""
def __init__(self, strict=False):
self.os = self.dist = self.os_ver = self.full_os_ver = self.osnick = self.arch = '?'
self.os = platform.system().lower()
if self.os == 'linux':
if False:
dist = platform.linux_distribution()
distname = dist[0].lower()
self.os_ver = self.full_os_ver = dist[1]
else:
try:
os_release = Platform.OSRelease()
distname = os_release.distname()
self.os_ver = self.full_os_ver = os_release.version()
self.osnick = os_release.osnick()
if self.osnick == "":
self.osnick = distname + str(self.os_ver)
except:
if strict:
assert(False), "Cannot determine distribution"
distname = 'unknown'
self.os_ver = self.full_os_ver = 'unknown'
if distname == 'fedora' or distname == 'ubuntu' or distname == 'debian' or distname == 'arch':
pass
elif distname.startswith('centos'):
distname = 'centos'
elif distname.startswith('redhat') or distname == 'rhel':
distname = 'redhat'
elif distname.startswith('suse'):
distname = 'suse'
elif distname.startswith('amzn'):
distname = 'amzn'
self.osnick = 'amzn' + str(os_release.version())
else:
if strict:
assert(False), "Cannot determine distribution"
self.dist = distname
elif self.os == 'darwin':
self.os = 'macosx'
self.dist = ''
mac_ver = platform.mac_ver()
self.full_os_ver = mac_ver[0] # e.g. 10.14, but also 10.5.8
self.os_ver = '.'.join(self.full_os_ver.split('.')[:2]) # major.minor
# self.arch = mac_ver[2] # e.g. x64_64
self.osnick = self.os + str(self.full_os_ver.split('.')[1])
nicks = {
"10.15": "catalina",
"10.14": "mojave",
"10.13": "highsierra",
"10.12": "sierra",
"10.11": "elcapitan",
"10.10": "yosemite",
"10.9": "mavericks",
"10.8": "mountainlion",
"10.7": "lion",
"10.6": "snowleopard",
"10.5": "leopard",
"10.4": "tiger",
"10.3": "panther",
"10.2": "jaguar",
"10.1": "puma",
"10.0": "cheetah"
}
if self.os_ver in nicks:
self.osnick = nicks[self.os_ver]
elif self.os == 'windows':
self.dist = self.os
self.os_ver = platform.release()
self.full_os_ver = os.version()
elif self.os == 'sunos':
self.os = 'solaris'
self.os_ver = ''
self.dist = ''
elif self.os == 'freebsd':
self.dist = ''
ver = sh('freebsd-version')
m = re.search(r'([^-]*)-(.*)', ver)
self.os_ver = self.full_os_ver = m.group(1)
self.osnick = self.os + self.os_ver
else:
if strict:
assert(False), "Cannot determine OS"
self.os_ver = ''
self.dist = ''
self.arch = platform.machine().lower()
if self.arch == 'amd64' or self.arch == 'x86_64':
self.arch = 'x64'
elif self.arch == 'i386' or self.arch == 'i686' or self.arch == 'i86pc':
self.arch = 'x86'
elif self.arch == 'aarch64':
self.arch = 'arm64v8'
elif self.arch == 'armv7l':
self.arch = 'arm32v7'
def triplet(self):
return '-'.join([self.os, self.osnick, self.arch])
def is_debian_compat(self):
return self.dist == 'debian' or self.dist == 'ubuntu' or self.dist == 'linuxmint'
def is_redhat_compat(self):
return self.dist == 'redhat' or self.dist == 'centos' or self.dist == 'amzn'
def is_container(self):
with open('/proc/1/cgroups', 'r') as conf:
for line in conf:
if re.search('docker', line):
return True
return False
platform = Platform()
print("%s-%s-%s" % (platform.os, platform.osnick, platform.arch))