🔨 Install 'heatshrink' if needed (#25896)

This commit is contained in:
Scott Lahteine 2023-05-29 19:00:09 -05:00 committed by GitHub
parent d926d4dea4
commit 47616c7dfa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 23 deletions

View file

@ -11,12 +11,15 @@ import sys
import datetime import datetime
import random import random
try: try:
import heatshrink import heatshrink2 as heatshrink
heatshrink_exists = True heatshrink_exists = True
except ImportError: except ImportError:
try:
import heatshrink
heatshrink_exists = True
except ImportError:
heatshrink_exists = False heatshrink_exists = False
def millis(): def millis():
return time.perf_counter() * 1000 return time.perf_counter() * 1000
@ -393,18 +396,19 @@ class FileTransferProtocol(object):
def copy(self, filename, dest_filename, compression, dummy): def copy(self, filename, dest_filename, compression, dummy):
self.connect() self.connect()
compression_support = heatshrink_exists and self.compression['algorithm'] == 'heatshrink' and compression has_heatshrink = heatshrink_exists and self.compression['algorithm'] == 'heatshrink'
if compression and (not heatshrink_exists or not self.compression['algorithm'] == 'heatshrink'): if compression and not has_heatshrink:
print("Compression not supported by client") hs = '2' if sys.version_info[0] > 2 else ''
#compression_support = False print("Compression not supported by client. Use 'pip install heatshrink%s' to fix." % hs)
compression = False
data = open(filename, "rb").read() data = open(filename, "rb").read()
filesize = len(data) filesize = len(data)
self.open(dest_filename, compression_support, dummy) self.open(dest_filename, compression, dummy)
block_size = self.protocol.block_size block_size = self.protocol.block_size
if compression_support: if compression:
data = heatshrink.encode(data, window_sz2=self.compression['window'], lookahead_sz2=self.compression['lookahead']) data = heatshrink.encode(data, window_sz2=self.compression['window'], lookahead_sz2=self.compression['lookahead'])
cratio = filesize / len(data) cratio = filesize / len(data)
@ -419,17 +423,17 @@ class FileTransferProtocol(object):
self.write(data[start:end]) self.write(data[start:end])
kibs = (( (i+1) * block_size) / 1024) / (millis() + 1 - start_time) * 1000 kibs = (( (i+1) * block_size) / 1024) / (millis() + 1 - start_time) * 1000
if (i / blocks) >= dump_pctg: if (i / blocks) >= dump_pctg:
print("\r{0:2.0f}% {1:4.2f}KiB/s {2} Errors: {3}".format((i / blocks) * 100, kibs, "[{0:4.2f}KiB/s]".format(kibs * cratio) if compression_support else "", self.protocol.errors), end='') print("\r{0:2.0f}% {1:4.2f}KiB/s {2} Errors: {3}".format((i / blocks) * 100, kibs, "[{0:4.2f}KiB/s]".format(kibs * cratio) if compression else "", self.protocol.errors), end='')
dump_pctg += 0.1 dump_pctg += 0.1
if self.protocol.errors > 0: if self.protocol.errors > 0:
# Dump last status (errors may not be visible) # Dump last status (errors may not be visible)
print("\r{0:2.0f}% {1:4.2f}KiB/s {2} Errors: {3} - Aborting...".format((i / blocks) * 100, kibs, "[{0:4.2f}KiB/s]".format(kibs * cratio) if compression_support else "", self.protocol.errors), end='') print("\r{0:2.0f}% {1:4.2f}KiB/s {2} Errors: {3} - Aborting...".format((i / blocks) * 100, kibs, "[{0:4.2f}KiB/s]".format(kibs * cratio) if compression else "", self.protocol.errors), end='')
print("") # New line to break the transfer speed line print("") # New line to break the transfer speed line
self.close() self.close()
print("Transfer aborted due to protocol errors") print("Transfer aborted due to protocol errors")
#raise Exception("Transfer aborted due to protocol errors") #raise Exception("Transfer aborted due to protocol errors")
return False; return False;
print("\r{0:2.0f}% {1:4.2f}KiB/s {2} Errors: {3}".format(100, kibs, "[{0:4.2f}KiB/s]".format(kibs * cratio) if compression_support else "", self.protocol.errors)) # no one likes transfers finishing at 99.8% print("\r{0:2.0f}% {1:4.2f}KiB/s {2} Errors: {3}".format(100, kibs, "[{0:4.2f}KiB/s]".format(kibs * cratio) if compression else "", self.protocol.errors)) # no one likes transfers finishing at 99.8%
if not self.close(): if not self.close():
print("Transfer failed") print("Transfer failed")

View file

@ -7,17 +7,6 @@ import serial
Import("env") Import("env")
# Needed (only) for compression, but there are problems with pip install heatshrink
#try:
# import heatshrink
#except ImportError:
# # Install heatshrink
# print("Installing 'heatshrink' python module...")
# env.Execute(env.subst("$PYTHONEXE -m pip install heatshrink"))
#
# Not tested: If it's safe to install python libraries in PIO python try:
# env.Execute(env.subst("$PYTHONEXE -m pip install https://github.com/p3p/pyheatshrink/releases/download/0.3.3/pyheatshrink-pip.zip"))
import MarlinBinaryProtocol import MarlinBinaryProtocol
#-----------------# #-----------------#
@ -191,6 +180,21 @@ def Upload(source, target, env):
# "upload_random_name": generate a random 8.3 firmware filename to upload # "upload_random_name": generate a random 8.3 firmware filename to upload
upload_random_filename = upload_delete_old_bins and not marlin_long_filename_host_support upload_random_filename = upload_delete_old_bins and not marlin_long_filename_host_support
# Heatshrink module is needed (only) for compression
if upload_compression:
if sys.version_info[0] > 2:
try:
import heatshrink2
except ImportError:
print("Installing 'heatshrink2' python module...")
env.Execute(env.subst("$PYTHONEXE -m pip install heatshrink2"))
else:
try:
import heatshrink
except ImportError:
print("Installing 'heatshrink' python module...")
env.Execute(env.subst("$PYTHONEXE -m pip install heatshrink"))
try: try:
# Start upload job # Start upload job