From 8ad0e8b9ec1232b5d6bc43b66ebc03524ffe193e Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 13 Dec 2022 14:54:20 +0100 Subject: [PATCH] Change espupload from curl to requests --- .gitignore | 1 + pio-tools/espupload.py | 149 +++++++----------- .../espupload_legacy.py | 0 3 files changed, 59 insertions(+), 91 deletions(-) rename tools/espupload.py => pio-tools/espupload_legacy.py (100%) diff --git a/.gitignore b/.gitignore index 22f113ae2..e7ee0a81d 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ firmware.asm tasmota/tasmota.ino.cpp platformio_override.ini platformio_tasmota_cenv.ini +platformio_tasmota_user_env.ini lib/libesp32/berry/generate/* lib/libesp32/berry/berry diff --git a/pio-tools/espupload.py b/pio-tools/espupload.py index 5e127785f..fa93ba2bc 100755 --- a/pio-tools/espupload.py +++ b/pio-tools/espupload.py @@ -1,108 +1,75 @@ #!/usr/bin/python3 -# -# espupload by Theo Arends - 20170930 -# -# Uploads binary file to OTA server -# -# Execute: espupload -u :/ -f -# -# Needs pycurl -# - pip install pycurl + +""" + espupload.py - for Tasmota + + Copyright (C) 2022 Theo Arends + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Provides: + Uploads binary file to OTA server. + Usually initated from http-uploader.py + +Requirements: + - Python + - pip install requests + +Usage: + ./espupload -u :/ -f +""" import sys import os -import optparse -import logging -import pycurl +import shutil +import argparse +import requests -HOST_URL = "domus1:80/api/upload-arduino.php" - -def upload(hostUrl, filename): - tname = os.path.normpath(os.path.dirname(filename)) - new_filename = tname + os.sep + os.path.basename(tname) + '.bin' - if os.path.exists(new_filename): - os.remove(new_filename) - os.rename(filename, new_filename) - - url = 'http://%s' % (hostUrl) - c = pycurl.Curl() - c.setopt(c.URL, url) - # The "Expect:" is there to suppress "Expect: 100-continue" behaviour that is - # the default in libcurl when posting large bodies (and fails on lighttpd). - c.setopt(c.HTTPHEADER, ["Expect:"]) - c.setopt(c.HTTPPOST, [('file', (c.FORM_FILE, new_filename, )), ]) - c.perform() - c.close() - -def parser(): - parser = optparse.OptionParser( - usage = "%prog [options]", - description = "Upload image to over the air Host server for the esp8266 module with OTA support." - ) - - # destination ip and port - group = optparse.OptionGroup(parser, "Destination") - group.add_option("-u", "--host_url", - dest = "host_url", - action = "store", - help = "Host url", - default = HOST_URL - ) - parser.add_option_group(group) - - # image - group = optparse.OptionGroup(parser, "Image") - group.add_option("-f", "--file", - dest = "image", - help = "Image file.", - metavar = "FILE", - default = None - ) - parser.add_option_group(group) - - # output group - group = optparse.OptionGroup(parser, "Output") - group.add_option("-d", "--debug", - dest = "debug", - help = "Show debug output. And override loglevel with debug.", - action = "store_true", - default = False - ) - parser.add_option_group(group) - - (options, args) = parser.parse_args() - - return options -# end parser +#HOST_URL = "domus1:80/api/upload-arduino.php" +HOST_URL = "otaserver/ota/upload-tasmota.php" def main(args): - # get options - options = parser() +# print(sys.argv[0:]) - # adapt log level - loglevel = logging.WARNING - if (options.debug): - loglevel = logging.DEBUG - # end if - - # logging - logging.basicConfig(level = loglevel, format = '%(asctime)-8s [%(levelname)s]: %(message)s', datefmt = '%H:%M:%S') - - logging.debug("Options: %s", str(options)) - - if (not options.host_url or not options.image): - logging.critical("Not enough arguments.") + # get arguments + parser = argparse.ArgumentParser( + usage = "%prog [arguments]", + description = "Upload image to over the air Host server for the esp8266 or esp32 module with OTA support." + ) + parser.add_argument("-u", "--host_url", dest = "host_url", action = "store", help = "Host url", default = HOST_URL) + parser.add_argument("-f", "--file", dest = "image", help = "Image file.", metavar = "FILE", default = None) + args = parser.parse_args() + if (not args.host_url or not args.image): + print("Not enough arguments.") return 1 # end if - if not os.path.exists(options.image): - logging.critical('Sorry: the file %s does not exist', options.image) - - return 1 + if not os.path.exists(args.image): + print('Sorry: the file %s does not exist', args.image) + return 2 # end if - upload(options.host_url, options.image) + # copy firmware.bin to tasmota.bin or tasmota32.bin + tname = os.path.normpath(os.path.dirname(args.image)) + new_filename = tname + os.sep + os.path.basename(tname) + '.bin' + shutil.copy2(args.image, new_filename) + + url = 'http://%s' % (args.host_url) + files = {'file': open(new_filename, 'rb')} + req = requests.post(url, files=files) + print(req.text) # end main if __name__ == '__main__': diff --git a/tools/espupload.py b/pio-tools/espupload_legacy.py similarity index 100% rename from tools/espupload.py rename to pio-tools/espupload_legacy.py