3
3
from sys import platform
4
4
from subprocess import Popen , PIPE , check_output , CalledProcessError
5
5
6
- # Define platform constants
6
+ # Platform constants
7
+ PLATFORM_LINUX = 'linux2'
7
8
PLATFORM_MACOS = 'darwin'
8
- PLATFORM_UBUNTU = 'linux2 '
9
+ PLATFORM_WINDOWS = 'win32 '
9
10
10
11
# Define error constants
11
12
FTDI_DRIVER_NOT_INSTALLED = 201
12
13
FTDI_DRIVER_NOT_LOADED = 202
13
14
PLATFORM_UNKNOWN = 2
14
15
16
+
15
17
# Enable logging
16
18
__module_logger = logging .getLogger ('blockly' )
17
19
@@ -29,23 +31,25 @@ def init():
29
31
def is_module_installed ():
30
32
if platform == PLATFORM_MACOS :
31
33
return __is_module_installed_macos ()
32
- elif platform == PLATFORM_UBUNTU :
33
- return __is_module_installed_ubuntu ()
34
+ elif platform == PLATFORM_LINUX :
35
+ return __is_module_installed_linux ()
36
+ elif platform == PLATFORM_WINDOWS :
37
+ return __is_module_installed_windows ()
34
38
else :
35
39
return PLATFORM_UNKNOWN
36
40
37
41
38
42
def is_module_loaded ():
39
43
if platform == PLATFORM_MACOS :
40
44
return __is_module_loaded_macos ()
41
- elif platform == PLATFORM_UBUNTU :
42
- return __is_module_loaded_ubuntu ()
45
+ elif platform == PLATFORM_LINUX :
46
+ return __is_module_loaded_linux ()
43
47
else :
44
48
return PLATFORM_UNKNOWN
45
49
46
50
47
51
# Ubuntu implementation
48
- def __is_module_installed_ubuntu ():
52
+ def __is_module_installed_linux ():
49
53
try :
50
54
process = Popen (['cat' , '/proc/modules' ], stdout = PIPE , stderr = PIPE )
51
55
output = check_output (('grep' , 'ftdi' ), stdin = process .stdout )
@@ -57,7 +61,7 @@ def __is_module_installed_ubuntu():
57
61
return FTDI_DRIVER_NOT_INSTALLED
58
62
59
63
60
- def __is_module_loaded_ubuntu ():
64
+ def __is_module_loaded_linux ():
61
65
try :
62
66
process = Popen (['dmesg' , '-H' , '-x' ], stdout = PIPE , stderr = PIPE )
63
67
output = check_output (('grep' , 'ftdi' ), stdin = process .stdout )
@@ -91,4 +95,38 @@ def __is_module_loaded_macos():
91
95
return 0
92
96
except CalledProcessError :
93
97
__module_logger .warning ('No FTDI modules detected.' )
94
- return 1
98
+ return 1
99
+
100
+
101
+ # Windows implementation
102
+ def __is_module_installed_windows ():
103
+ try :
104
+ out , err = Popen ('driverquery.exe -v' , stdout = PIPE ).communicate ()
105
+ position = out .find ('FT' )
106
+ if position > 0 :
107
+ __module_logger .debug ('FTDI module load state' )
108
+ __module_logger .debug (out [position :position + 91 ])
109
+ return 0
110
+ else :
111
+ __module_logger .warning ('No FTDI modules installed.' )
112
+ return FTDI_DRIVER_NOT_INSTALLED
113
+ except CalledProcessError :
114
+ __module_logger .warning ('No FTDI modules detected.' )
115
+ return FTDI_DRIVER_NOT_INSTALLED
116
+
117
+
118
+ def __is_module_loaded_windows ():
119
+ try :
120
+ out , err = Popen ('driverquery.exe -v' , stdout = PIPE ).communicate ()
121
+ position = out .find ('FT' )
122
+ if position > 0 :
123
+ __module_logger .debug ('FTDI module load state' )
124
+ __module_logger .debug (out [position + 84 :position + 91 ])
125
+ return 0
126
+ else :
127
+ __module_logger .warning ('No FTDI modules loaded.' )
128
+ return FTDI_DRIVER_NOT_INSTALLED
129
+ except CalledProcessError :
130
+ __module_logger .warning ('No FTDI modules detected.' )
131
+ return FTDI_DRIVER_NOT_INSTALLED
132
+
0 commit comments