1
|
1 |
|
import logging |
2
|
1 |
|
import os |
3
|
1 |
|
import platform |
4
|
|
|
|
5
|
1 |
|
log = logging.getLogger(__name__) |
6
|
|
|
|
7
|
1 |
|
BITS_MAP = { |
8
|
|
|
'32bit': 'i386', |
9
|
|
|
'64bit': 'x86_64' |
10
|
|
|
} |
11
|
|
|
|
12
|
1 |
|
MACHINE_MAP = { |
13
|
|
|
('32bit', 'i686'): 'i686' |
14
|
|
|
} |
15
|
|
|
|
16
|
1 |
|
NAME_MAP = { |
17
|
|
|
'Darwin': 'MacOSX' |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
|
21
|
1 |
|
class SystemHelper(object): |
22
|
1 |
|
@classmethod |
23
|
|
|
def architecture(cls): |
24
|
|
|
"""Retrieve system architecture (i386, i686, x86_64)""" |
25
|
|
|
|
26
|
1 |
|
bits, _ = platform.architecture() |
27
|
1 |
|
machine = platform.machine() |
28
|
|
|
|
29
|
|
|
# Check for ARM machine |
30
|
1 |
|
if bits == '32bit' and machine.startswith('armv'): |
31
|
|
|
return cls.arm(machine) |
32
|
|
|
|
33
|
|
|
# Check (bits, machine) map |
34
|
1 |
|
machine_key = (bits, machine) |
35
|
|
|
|
36
|
1 |
|
if machine_key in MACHINE_MAP: |
37
|
|
|
return MACHINE_MAP[machine_key] |
38
|
|
|
|
39
|
|
|
# Check (bits) map |
40
|
1 |
|
if bits in BITS_MAP: |
41
|
1 |
|
return BITS_MAP[bits] |
42
|
|
|
|
43
|
|
|
log.info('Unable to determine system architecture - bits: %r, machine: %r', bits, machine) |
44
|
|
|
return None |
45
|
|
|
|
46
|
1 |
|
@classmethod |
47
|
|
|
def name(cls): |
48
|
|
|
"""Retrieve system name (Windows, Linux, FreeBSD, MacOSX)""" |
49
|
|
|
|
50
|
1 |
|
system = platform.system() |
51
|
|
|
|
52
|
|
|
# Apply system map |
53
|
1 |
|
if system in NAME_MAP: |
54
|
|
|
system = NAME_MAP[system] |
55
|
|
|
|
56
|
1 |
|
return system |
57
|
|
|
|
58
|
1 |
|
@classmethod |
59
|
|
|
def arm(cls, machine): |
60
|
|
|
# Determine floating-point type |
61
|
|
|
float_type = cls.arm_float_type() |
62
|
|
|
|
63
|
|
|
if float_type is None: |
64
|
|
|
log.warn('Unable to use ARM libraries, unsupported floating-point type?') |
65
|
|
|
return None |
66
|
|
|
|
67
|
|
|
# Determine ARM version |
68
|
|
|
version = cls.arm_version() |
69
|
|
|
|
70
|
|
|
if version is None: |
71
|
|
|
log.warn('Unable to use ARM libraries, unsupported ARM version (%r)?' % machine) |
|
|
|
|
72
|
|
|
return None |
73
|
|
|
|
74
|
|
|
return '%s_%s' % (version, float_type) |
75
|
|
|
|
76
|
1 |
|
@classmethod |
77
|
1 |
|
def arm_version(cls, machine=None): |
78
|
|
|
# Read `machine` name if not provided |
79
|
|
|
if machine is None: |
80
|
|
|
machine = platform.machine() |
81
|
|
|
|
82
|
|
|
# Ensure `machine` is valid |
83
|
|
|
if not machine: |
84
|
|
|
return None |
85
|
|
|
|
86
|
|
|
# ARMv6 |
87
|
|
|
if machine.startswith('armv6'): |
88
|
|
|
return 'armv6' |
89
|
|
|
|
90
|
|
|
# ARMv7 |
91
|
|
|
if machine.startswith('armv7'): |
92
|
|
|
return 'armv7' |
93
|
|
|
|
94
|
|
|
return None |
95
|
|
|
|
96
|
1 |
|
@classmethod |
97
|
|
|
def arm_float_type(cls): |
98
|
|
|
if os.path.exists('/lib/arm-linux-gnueabihf'): |
99
|
|
|
return 'hf' |
100
|
|
|
|
101
|
|
|
if os.path.exists('/lib/arm-linux-gnueabi'): |
102
|
|
|
return 'sf' |
103
|
|
|
|
104
|
|
|
return None |
105
|
|
|
|