Skip to content

Commit 3adcc2c

Browse files
authored
Merge pull request #77 from zfi/demo
Defensive code for propeller-load launch failure in MacOS
2 parents fa759b8 + 71f6669 commit 3adcc2c

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

BlocklyServer.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import base64
22
import os
3+
import sys
34
import tempfile
45
import logging
56

@@ -26,8 +27,13 @@ def __init__(self, version, queue):
2627

2728
self.version = version
2829
self.queue = queue
30+
self.appdir = os.path.dirname(sys.argv[0])
31+
self.logger.debug("Application started from: %s", self.appdir)
32+
2933
queue.put((10, 'INFO', 'Server started'))
3034

35+
propellerLoad = PropellerLoad()
36+
3137
@cherrypy.expose()
3238
@cherrypy.tools.json_out()
3339
@cherrypy.tools.allow(methods=['GET'])
@@ -57,13 +63,13 @@ def ports(self):
5763
for port in ports:
5864
self.logger.debug('Port %s discovered.', port)
5965
if ' bt ' not in port.lower() and 'bluetooth' not in port.lower():
60-
self.logger.debug('Port %2 appended to list.', port)
6166
filtered_ports.append(port)
67+
self.logger.debug("Port %2 appended to list.", port)
6268
return filtered_ports
6369
else:
6470
# No useable ports detected. Need to determine how the browser
6571
# handles an empty list of available ports.
66-
self.logger.debug('No ports detected. Replying with /dev/null')
72+
self.logger.debug("No ports detected. Replying with /dev/null")
6773
return '/dev/null'
6874

6975

@@ -72,20 +78,27 @@ def ports(self):
7278
@cherrypy.tools.allow(methods=['POST'])
7379
def load(self, action, binary, extension, comport=None):
7480
if action is None:
75-
logger = logging.getLogger('blockly.server')
76-
logger.error('Load action is undefined.')
81+
self.logger.error('Load action is undefined.')
82+
return {
83+
'message': 'Load action is undefined',
84+
'success': False
85+
}
7786

7887
cherrypy.response.headers['Access-Control-Allow-Origin'] = '*'
7988

8089
self.logger.debug('Writing program payload to temp file.')
90+
8191
binary_file = tempfile.NamedTemporaryFile(suffix=extension, delete=False)
8292
binary_file.write(base64.b64decode(binary))
8393
binary_file.close()
94+
8495
self.logger.debug('%s saved.', binary_file.name)
8596

8697
self.logger.debug('Loading program to device.')
98+
8799
(success, out, err) = self.propellerLoad.load(action, binary_file, comport)
88100
self.queue.put((10, 'INFO', 'Application loaded (%s)' % action))
101+
89102
self.logger.info('Application load complete.')
90103

91104
os.remove(binary_file.name)
@@ -102,8 +115,6 @@ def serial_socket(self):
102115
self.queue.put((10, 'INFO', 'Serial socket set up'))
103116
handler = cherrypy.request.ws_handler
104117

105-
propellerLoad = PropellerLoad()
106-
107118

108119
def main(port, version, queue):
109120
module_logger.info("Server starting")

PropellerLoad.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,24 @@
77

88
__author__ = 'Michel'
99

10+
module_logger = logging.getLogger('blockly.loader')
1011

11-
class PropellerLoad:
1212

13+
class PropellerLoad:
1314
loading = False
1415
ports = []
1516

1617
def __init__(self):
1718
self.logger = logging.getLogger('blockly.loader')
1819
self.logger.info('Creating loader logger.')
1920

21+
# Find the path to the application launch directory
22+
self.appdir = os.path.dirname(sys.argv[0])
23+
self.logger.debug("Application running from: %s", self.appdir)
24+
25+
# if not self.appdir:
26+
# self.appdir = os.getcwd()
27+
2028
self.propeller_load_executables = {
2129
"Windows": "/propeller-tools/windows/propeller-load.exe",
2230
"Linux": "/propeller-tools/linux/propeller-load",
@@ -34,9 +42,6 @@ def __init__(self):
3442
print("Unsupported", platform.system() + " is currently unsupported")
3543
exit(1)
3644

37-
self.appdir = os.getcwd()
38-
# self.appdir = os.path.dirname(sys.argv[0])
39-
4045
def get_ports(self):
4146
self.logger.info('Getting ports')
4247

@@ -63,9 +68,12 @@ def get_ports(self):
6368
return ports
6469

6570
def load(self, action, file_to_load, com_port):
66-
self.logger.info("Loading code to device")
6771
self.loading = True
6872

73+
# Patch until we figure out why the __init__ is not getting called
74+
if not self.appdir or self.appdir == '':
75+
self.appdir = os.path.dirname(sys.argv[0])
76+
6977
executable = self.appdir + self.propeller_load_executables[platform.system()]
7078
self.logger.debug('Loader executable path is: %s)', executable)
7179

@@ -94,14 +102,17 @@ def load(self, action, file_to_load, com_port):
94102

95103
out, err = process.communicate()
96104
self.logger.info("Load result is: %s", process.returncode)
105+
self.logger.debug("Load error string: %s", err)
106+
self.logger.debug("Load output string: %s", out)
97107

98108
if process.returncode == 0:
99109
success = True
100110
else:
101111
success = False
102112

103113
self.loading = False
104-
return (success, out or '', err or '')
114+
return success, out or '', err or ''
115+
105116
except OSError as ex:
106117
self.logger.error("%s", ex.message)
107118

0 commit comments

Comments
 (0)