Skip to content

Commit fe4d57d

Browse files
committed
Rename sources to sources_hash
Use blake2 over md5
1 parent ea22521 commit fe4d57d

5 files changed

Lines changed: 20 additions & 20 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ out
55
__pycache__/
66
.config
77
.config.old
8+
.sources
89
klippy/.version
9-
klippy/.sources
1010
.history/
1111
.DS_Store
1212
ci_build/

klippy/mcu.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,7 @@ def _mcu_identify(self):
11911191
raise error(str(e))
11921192
if get_danger_options().log_startup_info:
11931193
logging.info(self._log_info())
1194+
pconfig = self._printer.lookup_object("configfile")
11941195
ppins = self._printer.lookup_object("pins")
11951196
pin_resolver = ppins.get_pin_resolver(self._name)
11961197
for cname, value in self.get_constants().items():
@@ -1214,25 +1215,25 @@ def _mcu_identify(self):
12141215
)
12151216
app = msgparser.get_app_info()
12161217
version, build_versions = msgparser.get_version_info()
1217-
sources = msgparser.get_sources_hash()
1218+
sources_hash = msgparser.get_sources_hash()
12181219
self._get_status_info["app"] = app
12191220
self._get_status_info["mcu_version"] = version
12201221
self._get_status_info["mcu_build_versions"] = build_versions
1221-
self._get_status_info["mcu_sources"] = sources
1222+
self._get_status_info["mcu_sources_hash"] = sources_hash
12221223
self._get_status_info["mcu_constants"] = msgparser.get_constants()
12231224
if app in ("Klipper", "Danger-Klipper"):
1224-
pconfig = self._printer.lookup_object("configfile")
12251225
pconfig.runtime_warning(
12261226
f"MCU {self._name!r} currently has firmware compiled for {app} (version {version})."
12271227
f" It is recommended to re-flash for best compatiblity with Kalico"
12281228
)
12291229
elif (
12301230
get_danger_options().warn_on_mismatched_firmware_sources
1231-
and sources != self._printer.get_start_args().get("sources_hash")
1231+
and sources_hash
1232+
!= self._printer.get_start_args().get("sources_hash")
12321233
):
1233-
pconfig = self._printer.lookup_object("configfile")
12341234
pconfig.runtime_warning(
1235-
f"MCU {self._name!r} firmware is out of date, it will still work but an update is recommended"
1235+
f"MCU {self._name!r} firmware is out of date and may not function as intended."
1236+
f" Please re-flash as soon as possible"
12361237
)
12371238
self.register_response(self._handle_shutdown, "shutdown")
12381239
self.register_response(self._handle_shutdown, "is_shutdown")

klippy/msgproto.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ def process_identify(self, data, decompress=True):
488488
self.app = data.get("app", "")
489489
self.version = data.get("version", "")
490490
self.build_versions = data.get("build_versions", "")
491-
self.sources = data.get("sources", "unknown")
491+
self.sources_hash = data.get("sources_hash", "unknown")
492492
except error as e:
493493
raise
494494
except Exception as e:
@@ -505,7 +505,7 @@ def get_version_info(self):
505505
return self.version, self.build_versions
506506

507507
def get_sources_hash(self):
508-
return self.sources
508+
return self.sources_hash
509509

510510
def get_messages(self):
511511
return list(self.messages)

klippy/util.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
#
55
# This file may be distributed under the terms of the GNU GPLv3 license.
66
import fcntl
7+
import hashlib
78
import json
89
import logging
910
import os
11+
import pathlib
1012
import pty
1113
import signal
1214
import subprocess
1315
import termios
1416
import time
1517
import traceback
16-
import pathlib
17-
import hashlib
1818

1919
######################################################################
2020
# Low-level Unix commands
@@ -257,20 +257,19 @@ def get_git_version(from_file=True):
257257

258258

259259
def get_firmware_hash():
260-
klippy_dir = pathlib.Path(__file__).parent
261-
root_dir = klippy_dir.parent
262-
hash_cache = klippy_dir / ".sources"
263-
sources = sorted(
260+
root_dir = pathlib.Path(__file__).parent.parent
261+
hash_cache = root_dir / ".sources"
262+
source_files = sorted(
264263
file
265264
for path in (root_dir / "src", root_dir / "lib")
266265
for file in path.glob("**/*")
267266
if file.is_file()
268267
)
269-
last_modified = max(file.stat().st_mtime for file in sources)
268+
last_modified = max(file.stat().st_mtime for file in source_files)
270269
if hash_cache.is_file() and hash_cache.stat().st_mtime >= last_modified:
271270
return hash_cache.read_text()
272-
hash = hashlib.md5()
273-
for file in sources:
271+
hash = hashlib.blake2b(digest_size=16, usedforsecurity=False)
272+
for file in source_files:
274273
hash.update(b"\x00" + bytes(file.relative_to(root_dir)) + b"\x00")
275274
hash.update(file.read_bytes())
276275
digest = hash.hexdigest()

scripts/buildcommands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,14 +652,14 @@ def update_data_dictionary(self, data):
652652
data["build_versions"] = self.toolstr
653653
data["app"] = "Kalico"
654654
data["license"] = "GNU GPLv3"
655-
data["sources"] = self.sources
655+
data["sources_hash"] = self.sources
656656

657657
def generate_code(self, options):
658658
cleanbuild, self.toolstr = tool_versions(options.tools)
659659
self.version = build_version(options.extra, cleanbuild)
660660
self.sources = get_firmware_hash()
661661
sys.stdout.write("Version: %s\n" % (self.version,))
662-
sys.stdout.write("Sources: %s\n" % (self.sources,))
662+
sys.stdout.write("Sources Hash: %s\n" % (self.sources,))
663663
return "\n// version: %s\n// build_versions: %s\n// sources: %s\n" % (
664664
self.version,
665665
self.toolstr,

0 commit comments

Comments
 (0)