Skip to content
This repository has been archived by the owner on Jan 26, 2025. It is now read-only.

Commit

Permalink
lint, test and exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Magnusson committed Mar 23, 2016
1 parent 53e549c commit 71d7f0c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
5 changes: 5 additions & 0 deletions nodemcu_uploader/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def __init__(self, message, expected, actual):
self.expected = expected
self.actual = actual

class NoAckException(Exception):
pass

class DeviceNotFoundException(Exception):
pass

class VerificationError(Exception):
pass
35 changes: 27 additions & 8 deletions nodemcu_uploader/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


from .exceptions import CommunicationTimeout, DeviceNotFoundException, \
BadResponseException
BadResponseException, VerificationError, NoAckException
from .utils import default_port, system
from .luacode import RECV_LUA, SEND_LUA, LUA_FUNCTIONS, \
LIST_FILES, UART_SETUP, PRINT_FILE
Expand Down Expand Up @@ -249,12 +249,13 @@ def write_file(self, path, destination='', verify='none'):
res = self.__expect('C> ')
if not res.endswith('C> '):
log.error('Error waiting for esp "%s"', res)
return
raise CommunicationTimeout('Error waiting for device to start receiving', res)

log.debug('sending destination filename "%s"', destination)
self.__write(destination + '\x00', True)
if not self.__got_ack():
log.error('did not ack destination filename')
return
raise NoAckException('Device did not ACK destination filename')

fil = open(path, 'rb')
content = fil.read()
Expand All @@ -272,19 +273,29 @@ def write_file(self, path, destination='', verify='none'):
if not self.__write_chunk(data):
resp = self.__expect()
log.error('Bad chunk response "%s" %s', resp, ':'.join(x.encode('hex') for x in resp))
return
raise BadResponseException('Bad chunk response', ACK, resp)

pos += chunk_size

log.debug('sending zero block')
#zero size block
self.__write_chunk('')
if verify != 'none':
self.verify_file(path, destination, verify)

def verify_file(self, path, destination, verify='none'):
"""Tries to verify if path has same checksum as destination.
Valid options for verify is 'raw', 'sha1' or 'none'
"""
fil = open(path, 'rb')
content = fil.read()
fil.close()
if verify == 'raw':
log.info('Verifying...')
data = self.download_file(destination)
if content != data:
log.error('Verification failed.')
log.error('Raw verification failed.')
raise VerificationError('Verification failed.')
else:
log.info('Verification successfull. Contents are identical.')
elif verify == 'sha1':
Expand All @@ -296,14 +307,16 @@ def write_file(self, path, destination='', verify='none'):
filehashhex = hashlib.sha1(content.encode()).hexdigest()
log.info('Local SHA1: %s', filehashhex)
if data != filehashhex:
log.error('Verification failed.')
log.error('SHA1 verification failed.')
raise VerificationError('SHA1 Verification failed.')
else:
log.info('Verification successfull. Checksums match')

elif verify != 'none':
raise Exception(verify + ' is not a valid verification method.')



def exec_file(self, path):
"""execute the lines in the local file 'path'"""
filename = os.path.basename(path)
Expand Down Expand Up @@ -388,7 +401,13 @@ def file_list(self):
log.info('Listing files')
res = self.__exchange(LIST_FILES)
log.info(res)
return res
res = res.split('\r\n')
#skip first and last lines
res = res[1:-1]
files = []
for line in res:
files.append(line.split('\t'))
return files

def file_do(self, filename):
"""Execute a file on the device using 'do'"""
Expand Down Expand Up @@ -419,7 +438,7 @@ def node_heap(self):
log.info('Heap')
res = self.__exchange('print(node.heap())')
log.info(res)
return res
return int(res.split('\r\n')[1])

def node_restart(self):
"""Restarts device"""
Expand Down
1 change: 1 addition & 0 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ max-line-length=120

[DESIGN]
max-args=6
max-statements=70

[MESSAGES CONTROL]
disable=I0011
Expand Down
12 changes: 12 additions & 0 deletions tests/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,15 @@ def test_upload_strange_file(self):
self.uploader.prepare()
self.uploader.write_file('tests/fixtures/testuploadfail.txt', verify='raw')


def test_file_list(self):
lst = self.uploader.file_list()
self.assertIsInstance(lst, type([]))
self.assertGreaterEqual(len(lst), 1)
self.assertLess(len(lst), 50)


def test_node_heap(self):
size = self.uploader.node_heap()
self.assertGreater(size, 20000)
self.assertLess(size, 60000)

0 comments on commit 71d7f0c

Please sign in to comment.