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

Commit

Permalink
version bump. breaking changes
Browse files Browse the repository at this point in the history
* No more python 2.7
* Targeting SDK 3.0 releases of NodeMCU
  • Loading branch information
kmpm committed Dec 12, 2019
1 parent ea3321e commit 6eeb7e3
Show file tree
Hide file tree
Showing 17 changed files with 374 additions and 133 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ themselves that are having issues.
Call for maintainers
--------------------
Hi,
This project is in need of maintenance and I (kmpm) do not have the time the project deserves.
Look at https://github.com/kmpm/nodemcu-uploader/issues/90 for more information on what to do about it.
This project is in need of maintenance and I (kmpm) do not have the time the
project deserves. Look at https://github.com/kmpm/nodemcu-uploader/issues/90
for more information on what to do about it or email [email protected]


Installation
Expand Down Expand Up @@ -52,9 +53,11 @@ python easy_install pyserial

Usage
-----
Download NodeMCU firmware from http://nodemcu-build.com/ .

Since version v0.4.0 of the tool you will need a recent (june/july 2016) version
of the firmware for nodemcu. The default baudrate was changed in firmware from
9600 to 115200 and this tool was changed as well. Download from http://nodemcu-build.com/ .
9600 to 115200 and this tool was changed as well.

If you are using an older firmware you MUST use the option `--start-baud 9600`
to the device to be recognized. Otherwise you will get a
Expand Down
8 changes: 7 additions & 1 deletion nodemcu_uploader/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ class VerificationError(Exception):


class PathLengthException(Exception):
pass
pass


class ValidationException(Exception):
def __init__(self, message, key, value):
message = "Validation Exception. {key} was {message}. '{value}'".format(message=message, key=key, value=value)
super(ValidationException, self).__init__(message)
37 changes: 20 additions & 17 deletions nodemcu_uploader/luacode.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

# Copyright (C) 2015-2019 Peter Magnusson <[email protected]>
# pylint: disable=C0301
# flake8: noqa


LUA_FUNCTIONS = ['recv_block', 'recv_name', 'recv', 'shafile', 'send_block', 'send_file', 'send']
Expand All @@ -12,29 +13,27 @@

PRINT_FILE = "file.open('{filename}') print('---{filename}---') print(file.read()) file.close() print('---')"

INFO_GROUP = "for key,value in pairs(node.info('{group}')) do k=tostring(key) print(k .. string.rep(' ', 20 - #k), tostring(value)) end"

LIST_FILES = 'for key,value in pairs(file.list()) do print(key,value) end'
#NUL = \000, ACK = \006
# NUL = \000, ACK = \006
RECV_LUA = \
r"""
function recv_block(d)
if string.byte(d, 1) == 1 then
size = string.byte(d, 2)
uart.write(0,'\006')
if size > 0 then
file.write(string.sub(d, 3, 3+size-1))
else
file.close()
uart.on('data')
function recv()
local on,w,ack,nack=uart.on,uart.write,'\6','\21'
local fd
local function recv_block(d)
local t,l = d:byte(1,2)
if t ~= 1 then w(0, nack); fd:close(); return on('data') end
if l >= 0 then fd:write(d:sub(3, l+2)); end
if l == 0 then fd:close(); w(0, ack); return on('data') else w(0, ack) end
end
else
uart.write(0, '\021' .. d)
uart.on('data')
local function recv_name(d) d = d:gsub('%z.*', '') file.remove(d) fd=file.open(d, 'w') on('data', 130, recv_block, 0) w(0, ack) end
on('data', '\0', recv_name, 0)
w(0, 'C')
end
end
function recv_name(d) d = d:gsub('%z.*', '') file.remove(d) file.open(d, 'w') uart.on('data', 130, recv_block, 0) uart.write(0, '\006') end
function recv() uart.on('data', '\000', recv_name, 0) uart.write(0, 'C') end
function shafile(f) print(crypto.toHex(crypto.fhash('sha1', f))) end
"""
""" # noqa: E122

SEND_LUA = \
r"""
Expand All @@ -49,3 +48,7 @@
"""

UART_SETUP = 'uart.setup(0,{baud},8,0,1,1)'

REMOVE_ALL_FILES = r"""
for key,value in pairs(file.list()) do file.remove(key) end
"""
54 changes: 38 additions & 16 deletions nodemcu_uploader/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
import argparse
import logging
import os
import sys
import glob
from .uploader import Uploader
from .term import terminal
from serial import VERSION as serialversion
from .version import __version__


log = logging.getLogger(__name__) # pylint: disable=C0103
from .version import __version__
log = logging.getLogger(__name__) # pylint: disable=C0103


def destination_from_source(sources, use_glob=True):
"""
Expand Down Expand Up @@ -47,14 +49,18 @@ def destination_from_source(sources, use_glob=True):

def operation_upload(uploader, sources, verify, do_compile, do_file, do_restart):
"""The upload operation"""
if not isinstance(sources, list):
sources = [sources]
sources, destinations = destination_from_source(sources)
if len(destinations) == len(sources):
if uploader.prepare():
for filename, dst in zip(sources, destinations):
if do_compile:
uploader.file_remove(os.path.splitext(dst)[0]+'.lc')
if not os.path.exists(filename) and not os.path.isfile(filename):
raise Exception("File does not exist. {filename}".format(filename=filename))
uploader.write_file(filename, dst, verify)
#init.lua is not allowed to be compiled
# init.lua is not allowed to be compiled
if do_compile and dst != 'init.lua':
uploader.file_compile(dst)
uploader.file_remove(dst)
Expand All @@ -70,27 +76,32 @@ def operation_upload(uploader, sources, verify, do_compile, do_file, do_restart)
if do_restart:
uploader.node_restart()
log.info('All done!')
return destinations


def operation_download(uploader, sources):
def operation_download(uploader, sources, *args, **kwargs):
"""The download operation"""
sources, destinations = destination_from_source(sources, False)
print('sources', sources)
print('destinations', destinations)
# print('sources', sources)
# print('destinations', destinations)
dest = kwargs.pop('dest', '')
if len(destinations) == len(sources):
if uploader.prepare():
for filename, dst in zip(sources, destinations):
dst = os.path.join(dest, dst)
uploader.read_file(filename, dst)
else:
raise Exception('You must specify a destination filename for each file you want to download.')
log.info('All done!')


def operation_list(uploader):
"""List file on target"""
files = uploader.file_list()
for f in files:
log.info("{file:30s} {size}".format(file=f[0], size=f[1]))


def operation_file(uploader, cmd, filename=''):
"""File operations"""
if cmd == 'list':
Expand All @@ -106,7 +117,8 @@ def operation_file(uploader, cmd, filename=''):
elif cmd == 'print':
for path in filename:
uploader.file_print(path)

elif cmd == 'remove_all':
uploader.file_remove_all()


def arg_auto_int(value):
Expand All @@ -127,11 +139,20 @@ def main_func():
action='store_true',
default=False)

parser.add_argument(
'--silent',
help='silent output. Errors and worse',
action='store_true',
default=False)

parser.add_argument(
'--version',
help='prints the version and exists',
action='version',
version='%(prog)s {version} (serial {serialversion})'.format(version=__version__, serialversion=serialversion)
version='%(prog)s {version} (serial {serialversion}, python {pv})'.format(
version=__version__,
serialversion=serialversion,
pv=sys.version)
)

parser.add_argument(
Expand Down Expand Up @@ -173,7 +194,6 @@ def main_func():
help='Backup all the files on the nodemcu board')
backup_parser.add_argument('path', help='Folder where to store the backup')


upload_parser = subparsers.add_parser(
'upload',
help='Path to one or more files to be uploaded. Destination name will be the same as the file name.')
Expand Down Expand Up @@ -224,19 +244,19 @@ def main_func():
'download',
help='Path to one or more files to be downloaded. Destination name will be the same as the file name.')

download_parser.add_argument('filename',
download_parser.add_argument(
'filename',
nargs='+',
help='Lua file to download. Use colon to give alternate destination.')


file_parser = subparsers.add_parser(
'file',
help='File functions')

file_parser.add_argument(
'cmd',
choices=('list', 'do', 'format', 'remove', 'print'),
help="list=list files, do=dofile given path, format=formate file area, remove=remove given path")
choices=('list', 'do', 'format', 'remove', 'print', 'remove_all'),
help="list=list files, do=dofile given path, format=formate file area, remove=remove given path, remove_all=delete all files")

file_parser.add_argument('filename', nargs='*', help='path for cmd')

Expand All @@ -254,15 +274,17 @@ def main_func():
args = parser.parse_args()

default_level = logging.INFO
if args.silent:
default_level = logging.ERROR
if args.verbose:
default_level = logging.DEBUG

#formatter = logging.Formatter('%(message)s')
# formatter = logging.Formatter('%(message)s')

logging.basicConfig(level=default_level, format='%(message)s')

if args.operation == 'terminal':
#uploader can not claim the port
# uploader can not claim the port
terminal(args.port, str(args.start_baud))
return

Expand Down Expand Up @@ -299,5 +321,5 @@ def main_func():
elif args.operation == 'backup':
uploader.backup(args.path)

#no uploader related commands after this point
# no uploader related commands after this point
uploader.close()
Loading

0 comments on commit 6eeb7e3

Please sign in to comment.