Passed
Push — develop ( fc562f...b9b638 )
by Dean
02:34
created

SystemHelper.vcr_version()   A

Complexity

Conditions 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 9.762
Metric Value
cc 3
dl 0
loc 17
ccs 1
cts 11
cp 0.0909
crap 9.762
rs 9.4285
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
MSVCR_MAP = {
17
    'msvcr120.dll': 'vc12',
18
    'msvcr130.dll': 'vc14'
19
}
20
21 1
NAME_MAP = {
22
    'Darwin': 'MacOSX'
23
}
24
25
26 1
class SystemHelper(object):
27 1
    @classmethod
28
    def architecture(cls):
29
        """Retrieve system architecture (i386, i686, x86_64)"""
30
31 1
        bits, _ = platform.architecture()
32 1
        machine = platform.machine()
33
34
        # Check for ARM machine
35 1
        if bits == '32bit' and machine.startswith('armv'):
36
            return cls.arm(machine)
37
38
        # Check (bits, machine) map
39 1
        machine_key = (bits, machine)
40
41 1
        if machine_key in MACHINE_MAP:
42
            return MACHINE_MAP[machine_key]
43
44
        # Check (bits) map
45 1
        if bits in BITS_MAP:
46 1
            return BITS_MAP[bits]
47
48
        log.info('Unable to determine system architecture - bits: %r, machine: %r', bits, machine)
49
        return None
50
51 1
    @classmethod
52
    def name(cls):
53
        """Retrieve system name (Windows, Linux, FreeBSD, MacOSX)"""
54
55 1
        system = platform.system()
56
57
        # Apply system map
58 1
        if system in NAME_MAP:
59
            system = NAME_MAP[system]
60
61 1
        return system
62
63 1
    @classmethod
64
    def arm(cls, machine):
65
        # Determine floating-point type
66
        float_type = cls.arm_float_type()
67
68
        if float_type is None:
69
            log.warn('Unable to use ARM libraries, unsupported floating-point type?')
70
            return None
71
72
        # Determine ARM version
73
        version = cls.arm_version()
74
75
        if version is None:
76
            log.warn('Unable to use ARM libraries, unsupported ARM version (%r)?' % machine)
0 ignored issues
show
Coding Style Best Practice introduced by
Specify string format arguments as logging function parameters
Loading history...
77
            return None
78
79
        return '%s_%s' % (version, float_type)
80
81 1
    @classmethod
82 1
    def arm_version(cls, machine=None):
83
        # Read `machine` name if not provided
84
        if machine is None:
85
            machine = platform.machine()
86
87
        # Ensure `machine` is valid
88
        if not machine:
89
            return None
90
91
        # ARMv6
92
        if machine.startswith('armv6'):
93
            return 'armv6'
94
95
        # ARMv7
96
        if machine.startswith('armv7'):
97
            return 'armv7'
98
99
        return None
100
101 1
    @classmethod
102
    def arm_float_type(cls):
103
        if os.path.exists('/lib/arm-linux-gnueabihf'):
104
            return 'hf'
105
106
        if os.path.exists('/lib/arm-linux-gnueabi'):
107
            return 'sf'
108
109
        return None
110
111 1
    @classmethod
112
    def vcr_version(cls):
113
        try:
114
            import ctypes.util
115
116
            # Retrieve linked msvcr dll
117
            name = ctypes.util.find_msvcrt()
118
119
            # Return VC++ version from map
120
            if name not in MSVCR_MAP:
121
                log.error('Unknown VC++ runtime: %r', name)
122
                return None
123
124
            return MSVCR_MAP[name]
125
        except Exception:
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
126
            log.error('Unable to retrieve VC++ runtime version', exc_info=True)
127
            return None
128