Tasmota/pio-tools/espupload.py

87 lines
2.6 KiB
Python
Raw Permalink Normal View History

2020-12-05 10:47:09 +00:00
#!/usr/bin/python3
2022-12-13 13:54:20 +00:00
"""
espupload.py - for Tasmota
2022-12-13 13:54:20 +00:00
Copyright (C) 2022 Theo Arends
2022-12-13 13:54:20 +00:00
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.
2022-12-13 13:54:20 +00:00
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.
2022-12-13 13:54:20 +00:00
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
2022-12-13 13:54:20 +00:00
Provides:
Uploads binary file to OTA server.
Usually initiated from http-uploader.py
2022-12-13 13:54:20 +00:00
Requirements:
- Python
- pip install requests
2022-12-13 13:54:20 +00:00
Usage:
./espupload -u <Host_IP_address>:<Host_port>/<Host_path> -f <sketch.bin>
"""
2022-12-13 13:54:20 +00:00
import sys
import os
import shutil
import argparse
import requests
# Default URL overwritten by [env] and/or [env:tasmota32_base] upload_port
2022-12-13 13:54:20 +00:00
HOST_URL = "otaserver/ota/upload-tasmota.php"
2022-12-13 13:54:20 +00:00
def main(args):
# print(sys.argv[0:])
# 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()
2022-12-13 13:54:20 +00:00
if (not args.host_url or not args.image):
print("Not enough arguments.")
return 1
# end if
2022-12-13 13:54:20 +00:00
if not os.path.exists(args.image):
print('Sorry: the file {} does not exist'.format(args.image))
2022-12-13 13:54:20 +00:00
return 2
# end if
if args.image.find("firmware.bin") != -1:
# Legacy support for $SOURCE
# copy firmware.bin to tasmota.bin or tasmota32.bin
# C:\tmp\.pioenvs\tasmota-theo\firmware.bin
tname = os.path.normpath(os.path.dirname(args.image))
# C:\tmp\.pioenvs\tasmota-theo\tasmota-theo.bin
upload_file = tname + os.sep + os.path.basename(tname) + '.bin'
shutil.copy2(args.image, upload_file)
else:
# Support for bin_file and bin_gz_file
upload_file = args.image
# end if
# print('Debug filename in {}, upload {}'.format(args.image, upload_file))
2022-12-13 13:54:20 +00:00
url = 'http://%s' % (args.host_url)
files = {'file': open(upload_file, 'rb')}
2022-12-13 13:54:20 +00:00
req = requests.post(url, files=files)
print(req.text)
# end main
if __name__ == '__main__':
sys.exit(main(sys.argv))
# end if