Skip to content

Commit

Permalink
🔨 Install 'heatshrink' if needed (MarlinFirmware#25896)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored and Tracy Spiva committed Jun 2, 2023
1 parent 0775cfe commit 25856aa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
28 changes: 16 additions & 12 deletions buildroot/share/scripts/MarlinBinaryProtocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
import datetime
import random
try:
import heatshrink
import heatshrink2 as heatshrink
heatshrink_exists = True
except ImportError:
heatshrink_exists = False

try:
import heatshrink
heatshrink_exists = True
except ImportError:
heatshrink_exists = False

def millis():
return time.perf_counter() * 1000
Expand Down Expand Up @@ -393,18 +396,19 @@ def abort(self):
def copy(self, filename, dest_filename, compression, dummy):
self.connect()

compression_support = heatshrink_exists and self.compression['algorithm'] == 'heatshrink' and compression
if compression and (not heatshrink_exists or not self.compression['algorithm'] == 'heatshrink'):
print("Compression not supported by client")
#compression_support = False
has_heatshrink = heatshrink_exists and self.compression['algorithm'] == 'heatshrink'
if compression and not has_heatshrink:
hs = '2' if sys.version_info[0] > 2 else ''
print("Compression not supported by client. Use 'pip install heatshrink%s' to fix." % hs)
compression = False

data = open(filename, "rb").read()
filesize = len(data)

self.open(dest_filename, compression_support, dummy)
self.open(dest_filename, compression, dummy)

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'])

cratio = filesize / len(data)
Expand All @@ -419,17 +423,17 @@ def copy(self, filename, dest_filename, compression, dummy):
self.write(data[start:end])
kibs = (( (i+1) * block_size) / 1024) / (millis() + 1 - start_time) * 1000
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
if self.protocol.errors > 0:
# 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
self.close()
print("Transfer aborted due to protocol errors")
#raise Exception("Transfer aborted due to protocol errors")
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():
print("Transfer failed")
Expand Down
26 changes: 15 additions & 11 deletions buildroot/share/scripts/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@

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

#-----------------#
Expand Down Expand Up @@ -191,6 +180,21 @@ def _RollbackUpload(FirmwareFile):
# "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

# 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:

# Start upload job
Expand Down

0 comments on commit 25856aa

Please sign in to comment.