[FL-2052] New build system based on scons (#1269)

This commit is contained in:
hedger
2022-06-26 15:00:03 +03:00
committed by GitHub
parent c79fb61909
commit f3b1475ede
179 changed files with 3986 additions and 5196 deletions

View File

@@ -106,4 +106,5 @@ class Copro:
address=f"0x{stack_addr:X}",
)
# Save manifest to
json.dump(manifest, open(manifest_file, "w"))
with open(manifest_file, "w", newline="\n") as file:
json.dump(manifest, file)

View File

@@ -227,12 +227,19 @@ class DolphinBubbleAnimation:
(frame, os.path.join(animation_directory, f"frame_{index}.bm"))
)
pool = multiprocessing.Pool()
pool.map(_convert_image_to_bm, to_pack)
if ImageTools.is_processing_slow():
pool = multiprocessing.Pool()
pool.map(_convert_image_to_bm, to_pack)
else:
for image in to_pack:
_convert_image_to_bm(image)
def process(self):
pool = multiprocessing.Pool()
self.frames = pool.map(_convert_image, self.frames)
if ImageTools.is_processing_slow():
pool = multiprocessing.Pool()
self.frames = pool.map(_convert_image, self.frames)
else:
self.frames = list(_convert_image(frame) for frame in self.frames)
class DolphinManifest:
@@ -295,7 +302,8 @@ class DolphinManifest:
def _renderTemplate(self, template_filename: str, output_filename: str, **kwargs):
template = Templite(filename=template_filename)
output = template.render(**kwargs)
open(output_filename, "w").write(output)
with open(output_filename, "w", newline="\n") as file:
file.write(output)
def save2code(self, output_directory: str, symbol_name: str):
# Process frames

View File

@@ -15,8 +15,13 @@ class Image:
self.data = data
def write(self, filename):
file = open(filename, "wb")
file.write(self.data)
with open(filename, "wb") as file:
file.write(self.data)
def data_as_carray(self):
return (
"{" + "".join("0x{:02x},".format(img_byte) for img_byte in self.data) + "}"
)
def is_file_an_icon(filename):
@@ -24,8 +29,62 @@ def is_file_an_icon(filename):
return extension in ICONS_SUPPORTED_FORMATS
class ImageTools:
__pil_unavailable = False
__hs2_unavailable = False
@staticmethod
def is_processing_slow():
try:
from PIL import Image, ImageOps
import heatshrink2
return False
except ImportError as e:
return True
def __init__(self):
self.logger = logging.getLogger()
def png2xbm(self, file):
if self.__pil_unavailable:
return subprocess.check_output(["convert", file, "xbm:-"])
try:
from PIL import Image, ImageOps
except ImportError as e:
self.__pil_unavailable = True
self.logger.info("pillow module is missing, using convert cli util")
return self.png2xbm(file)
with Image.open(file) as im:
with io.BytesIO() as output:
bw = im.convert("1")
bw = ImageOps.invert(bw)
bw.save(output, format="XBM")
return output.getvalue()
def xbm2hs(self, data):
if self.__hs2_unavailable:
return subprocess.check_output(
["heatshrink", "-e", "-w8", "-l4"], input=data
)
try:
import heatshrink2
except ImportError as e:
self.__hs2_unavailable = True
self.logger.info("heatshrink2 module is missing, using heatshrink cli util")
return self.xbm2hs(data)
return heatshrink2.compress(data, window_sz2=8, lookahead_sz2=4)
__tools = ImageTools()
def file2image(file):
output = subprocess.check_output(["convert", file, "xbm:-"])
output = __tools.png2xbm(file)
assert output
# Extract data from text
@@ -38,9 +97,7 @@ def file2image(file):
data_bin = bytearray.fromhex(data_str)
# Encode icon data with LZSS
data_encoded_str = subprocess.check_output(
["heatshrink", "-e", "-w8", "-l4"], input=data_bin
)
data_encoded_str = __tools.xbm2hs(data_bin)
assert data_encoded_str

View File

@@ -1,6 +1,8 @@
import datetime
import logging
import os
import posixpath
from pathlib import Path
from flipper.utils import *
from flipper.utils.fstree import *
@@ -112,20 +114,19 @@ class Manifest:
self.logger = logging.getLogger(self.__class__.__name__)
def load(self, filename):
manifest = open(filename, "r")
for line in manifest.readlines():
line = line.strip()
if len(line) == 0:
continue
tag, line = line.split(":", 1)
record = MANIFEST_TAGS_RECORDS[tag].fromLine(line)
self.records.append(record)
with open(filename, "r") as manifest:
for line in manifest.readlines():
line = line.strip()
if len(line) == 0:
continue
tag, line = line.split(":", 1)
record = MANIFEST_TAGS_RECORDS[tag].fromLine(line)
self.records.append(record)
def save(self, filename):
manifest = open(filename, "w+")
for record in self.records:
manifest.write(record.toLine())
manifest.close()
with open(filename, "w+", newline="\n") as manifest:
for record in self.records:
manifest.write(record.toLine())
def addDirectory(self, path):
self.records.append(ManifestRecordDirectory(path))
@@ -138,20 +139,22 @@ class Manifest:
dirs.sort()
files.sort()
relative_root = root.replace(directory_path, "", 1)
if relative_root:
relative_root = Path(relative_root).as_posix()
if relative_root.startswith("/"):
relative_root = relative_root[1:]
# process directories
for dir in dirs:
relative_dir_path = os.path.join(relative_root, dir)
for dirname in dirs:
relative_dir_path = posixpath.join(relative_root, dirname)
self.logger.debug(f'Adding directory: "{relative_dir_path}"')
self.addDirectory(relative_dir_path)
# Process files
for file in files:
relative_file_path = os.path.join(relative_root, file)
relative_file_path = posixpath.join(relative_root, file)
if file in ignore_files:
self.logger.info(f'Skipping file "{relative_file_path}"')
continue
full_file_path = os.path.join(root, file)
full_file_path = posixpath.join(root, file)
self.logger.debug(f'Adding file: "{relative_file_path}"')
self.addFile(
relative_file_path,

View File

@@ -189,33 +189,33 @@ class FlipperStorage:
"""Send file from local device to Flipper"""
self.remove(filename_to)
file = open(filename_from, "rb")
filesize = os.fstat(file.fileno()).st_size
with open(filename_from, "rb") as file:
filesize = os.fstat(file.fileno()).st_size
buffer_size = 512
while True:
filedata = file.read(buffer_size)
size = len(filedata)
if size == 0:
break
buffer_size = 512
while True:
filedata = file.read(buffer_size)
size = len(filedata)
if size == 0:
break
self.send_and_wait_eol(f'storage write_chunk "{filename_to}" {size}\r')
answer = self.read.until(self.CLI_EOL)
if self.has_error(answer):
self.last_error = self.get_error(answer)
self.send_and_wait_eol(f'storage write_chunk "{filename_to}" {size}\r')
answer = self.read.until(self.CLI_EOL)
if self.has_error(answer):
self.last_error = self.get_error(answer)
self.read.until(self.CLI_PROMPT)
return False
self.port.write(filedata)
self.read.until(self.CLI_PROMPT)
file.close()
return False
self.port.write(filedata)
self.read.until(self.CLI_PROMPT)
percent = str(math.ceil(file.tell() / filesize * 100))
total_chunks = str(math.ceil(filesize / buffer_size))
current_chunk = str(math.ceil(file.tell() / buffer_size))
sys.stdout.write(f"\r{percent}%, chunk {current_chunk} of {total_chunks}")
sys.stdout.flush()
file.close()
percent = str(math.ceil(file.tell() / filesize * 100))
total_chunks = str(math.ceil(filesize / buffer_size))
current_chunk = str(math.ceil(file.tell() / buffer_size))
sys.stdout.write(
f"\r{percent}%, chunk {current_chunk} of {total_chunks}"
)
sys.stdout.flush()
print()
return True

View File

@@ -8,14 +8,14 @@ def timestamp():
def file_hash(path: str, algo: str, block_size: int = 4096):
fd = open(path, "rb")
h = hashlib.new(algo)
while True:
data = fd.read(block_size)
if len(data) > 0:
h.update(data)
else:
break
with open(path, "rb") as fd:
while True:
data = fd.read(block_size)
if len(data) > 0:
h.update(data)
else:
break
return h.hexdigest()

View File

@@ -99,5 +99,5 @@ class FlipperFormatFile:
self.lines = file.readlines()
def save(self, filename: str):
with open(filename, "w") as file:
with open(filename, "w", newline="\n") as file:
file.write("\n".join(self.lines))