2022-04-13 20:50:25 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2023-04-06 02:44:37 +00:00
|
|
|
import json
|
2022-04-13 20:50:25 +00:00
|
|
|
import shutil
|
2022-10-12 16:12:05 +00:00
|
|
|
import tarfile
|
2023-04-06 02:44:37 +00:00
|
|
|
import zipfile
|
|
|
|
from os import makedirs, walk
|
|
|
|
from os.path import exists, join, relpath, basename, split
|
|
|
|
|
2022-10-25 22:15:02 +00:00
|
|
|
from ansi.color import fg
|
2023-04-06 02:44:37 +00:00
|
|
|
from flipper.app import App
|
|
|
|
from update import Main as UpdateMain
|
2022-04-13 20:50:25 +00:00
|
|
|
|
|
|
|
|
2022-06-26 12:00:03 +00:00
|
|
|
class ProjectDir:
|
|
|
|
def __init__(self, project_dir):
|
|
|
|
self.dir = project_dir
|
|
|
|
parts = project_dir.split("-")
|
|
|
|
self.target = parts[0]
|
|
|
|
self.project = parts[1]
|
|
|
|
self.flavor = parts[2] if len(parts) > 2 else ""
|
|
|
|
|
|
|
|
|
2022-04-13 20:50:25 +00:00
|
|
|
class Main(App):
|
2022-10-12 16:12:05 +00:00
|
|
|
DIST_FILE_PREFIX = "flipper-z-"
|
2022-10-13 15:05:07 +00:00
|
|
|
DIST_FOLDER_MAX_NAME_LENGTH = 80
|
2022-10-12 16:12:05 +00:00
|
|
|
|
2022-04-13 20:50:25 +00:00
|
|
|
def init(self):
|
|
|
|
self.subparsers = self.parser.add_subparsers(help="sub-command help")
|
|
|
|
|
|
|
|
self.parser_copy = self.subparsers.add_parser(
|
|
|
|
"copy", help="Copy firmware binaries & metadata"
|
|
|
|
)
|
|
|
|
|
2022-06-26 12:00:03 +00:00
|
|
|
self.parser_copy.add_argument("-p", dest="project", nargs="+", required=True)
|
2022-04-13 20:50:25 +00:00
|
|
|
self.parser_copy.add_argument("-s", dest="suffix", required=True)
|
2022-04-19 19:02:37 +00:00
|
|
|
self.parser_copy.add_argument("-r", dest="resources", required=False)
|
2022-04-13 20:50:25 +00:00
|
|
|
self.parser_copy.add_argument(
|
|
|
|
"--bundlever",
|
|
|
|
dest="version",
|
|
|
|
help="If set, bundle update package for self-update",
|
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
self.parser_copy.add_argument(
|
|
|
|
"--noclean",
|
|
|
|
dest="noclean",
|
|
|
|
action="store_true",
|
|
|
|
help="Don't clean output directory",
|
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
self.parser_copy.set_defaults(func=self.copy)
|
|
|
|
|
2022-10-28 15:32:06 +00:00
|
|
|
def get_project_file_name(self, project: ProjectDir, filetype: str) -> str:
|
2022-04-13 21:29:58 +00:00
|
|
|
# Temporary fix
|
2022-06-26 12:00:03 +00:00
|
|
|
project_name = project.project
|
2022-10-28 15:32:06 +00:00
|
|
|
if project_name == "firmware" and filetype != "elf":
|
|
|
|
project_name = "full"
|
2022-10-12 16:12:05 +00:00
|
|
|
|
2023-04-06 02:44:37 +00:00
|
|
|
dist_target_path = self.get_dist_file_name(project_name, filetype)
|
|
|
|
self.note_dist_component(
|
|
|
|
project_name, filetype, self.get_dist_path(dist_target_path)
|
|
|
|
)
|
|
|
|
return dist_target_path
|
|
|
|
|
|
|
|
def note_dist_component(self, component: str, extension: str, srcpath: str) -> None:
|
|
|
|
self._dist_components[f"{component}.{extension}"] = srcpath
|
2022-04-13 20:50:25 +00:00
|
|
|
|
2022-10-28 15:32:06 +00:00
|
|
|
def get_dist_file_name(self, dist_artifact_type: str, filetype: str) -> str:
|
|
|
|
return f"{self.DIST_FILE_PREFIX}{self.target}-{dist_artifact_type}-{self.args.suffix}.{filetype}"
|
|
|
|
|
2023-04-06 02:44:37 +00:00
|
|
|
def get_dist_path(self, filename: str) -> str:
|
2022-04-13 20:50:25 +00:00
|
|
|
return join(self.output_dir_path, filename)
|
|
|
|
|
2022-10-28 15:32:06 +00:00
|
|
|
def copy_single_project(self, project: ProjectDir) -> None:
|
2022-06-26 12:00:03 +00:00
|
|
|
obj_directory = join("build", project.dir)
|
2022-04-13 20:50:25 +00:00
|
|
|
|
|
|
|
for filetype in ("elf", "bin", "dfu", "json"):
|
2022-10-12 16:12:05 +00:00
|
|
|
if exists(src_file := join(obj_directory, f"{project.project}.{filetype}")):
|
|
|
|
shutil.copyfile(
|
|
|
|
src_file,
|
2023-04-06 02:44:37 +00:00
|
|
|
self.get_dist_path(self.get_project_file_name(project, filetype)),
|
2022-10-12 16:12:05 +00:00
|
|
|
)
|
2023-04-06 02:44:37 +00:00
|
|
|
for foldertype in ("sdk_headers", "lib"):
|
2022-10-28 15:32:06 +00:00
|
|
|
if exists(sdk_folder := join(obj_directory, foldertype)):
|
2023-04-06 02:44:37 +00:00
|
|
|
self.note_dist_component(foldertype, "dir", sdk_folder)
|
2022-10-28 15:32:06 +00:00
|
|
|
|
2023-04-06 19:37:12 +00:00
|
|
|
# TODO: remove this after everyone migrates to new uFBT
|
|
|
|
self.create_zip_stub("lib")
|
|
|
|
|
|
|
|
def create_zip_stub(self, foldertype):
|
2022-10-28 15:32:06 +00:00
|
|
|
with zipfile.ZipFile(
|
2023-04-06 02:44:37 +00:00
|
|
|
self.get_dist_path(self.get_dist_file_name(foldertype, "zip")),
|
2022-10-28 15:32:06 +00:00
|
|
|
"w",
|
|
|
|
zipfile.ZIP_DEFLATED,
|
2023-04-06 19:37:12 +00:00
|
|
|
) as _:
|
|
|
|
pass
|
2022-10-28 15:32:06 +00:00
|
|
|
|
|
|
|
def copy(self) -> int:
|
2023-04-06 02:44:37 +00:00
|
|
|
self._dist_components: dict[str, str] = dict()
|
|
|
|
self.projects: dict[str, ProjectDir] = dict(
|
2022-06-26 12:00:03 +00:00
|
|
|
map(
|
|
|
|
lambda pd: (pd.project, pd),
|
|
|
|
map(ProjectDir, self.args.project),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
project_targets = set(map(lambda p: p.target, self.projects.values()))
|
|
|
|
if len(project_targets) != 1:
|
|
|
|
self.logger.error(f"Cannot mix targets {project_targets}!")
|
|
|
|
return 1
|
|
|
|
self.target = project_targets.pop()
|
|
|
|
|
|
|
|
project_flavors = set(map(lambda p: p.flavor, self.projects.values()))
|
|
|
|
if len(project_flavors) != 1:
|
|
|
|
self.logger.error(f"Cannot mix flavors {project_flavors}!")
|
|
|
|
return 2
|
|
|
|
self.flavor = project_flavors.pop()
|
|
|
|
|
|
|
|
dist_dir_components = [self.target]
|
|
|
|
if self.flavor:
|
|
|
|
dist_dir_components.append(self.flavor)
|
|
|
|
|
|
|
|
self.output_dir_path = join("dist", "-".join(dist_dir_components))
|
2022-04-13 20:50:25 +00:00
|
|
|
if exists(self.output_dir_path) and not self.args.noclean:
|
2022-06-26 12:00:03 +00:00
|
|
|
try:
|
|
|
|
shutil.rmtree(self.output_dir_path)
|
|
|
|
except Exception as ex:
|
2023-04-06 02:44:37 +00:00
|
|
|
self.logger.warn(f"Failed to clean output directory: {ex}")
|
2022-04-13 20:50:25 +00:00
|
|
|
|
|
|
|
if not exists(self.output_dir_path):
|
2023-04-06 02:44:37 +00:00
|
|
|
self.logger.debug(f"Creating output directory {self.output_dir_path}")
|
2022-04-13 20:50:25 +00:00
|
|
|
makedirs(self.output_dir_path)
|
|
|
|
|
2023-04-06 02:44:37 +00:00
|
|
|
for folder in ("debug", "scripts"):
|
|
|
|
if exists(folder):
|
|
|
|
self.note_dist_component(folder, "dir", folder)
|
|
|
|
|
2022-06-26 12:00:03 +00:00
|
|
|
for project in self.projects.values():
|
2023-04-06 02:44:37 +00:00
|
|
|
self.logger.debug(f"Copying {project.project} for {project.target}")
|
2022-04-13 20:50:25 +00:00
|
|
|
self.copy_single_project(project)
|
|
|
|
|
|
|
|
self.logger.info(
|
2022-11-02 15:15:40 +00:00
|
|
|
fg.boldgreen(
|
|
|
|
f"Firmware binaries can be found at:\n\t{self.output_dir_path}"
|
|
|
|
)
|
2022-04-13 20:50:25 +00:00
|
|
|
)
|
2022-06-26 12:00:03 +00:00
|
|
|
|
2022-04-13 20:50:25 +00:00
|
|
|
if self.args.version:
|
2023-04-06 02:44:37 +00:00
|
|
|
if bundle_result := self.bundle_update_package():
|
|
|
|
return bundle_result
|
|
|
|
|
|
|
|
required_components = ("firmware.elf", "full.bin", "update.dir")
|
|
|
|
if all(
|
|
|
|
map(
|
|
|
|
lambda c: c in self._dist_components,
|
|
|
|
required_components,
|
|
|
|
)
|
|
|
|
):
|
|
|
|
self.bundle_sdk()
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def bundle_sdk(self):
|
|
|
|
self.logger.info("Bundling SDK")
|
|
|
|
components_paths = dict()
|
|
|
|
|
|
|
|
sdk_components_keys = (
|
|
|
|
"full.bin",
|
|
|
|
"firmware.elf",
|
|
|
|
"update.dir",
|
|
|
|
"sdk_headers.dir",
|
|
|
|
"lib.dir",
|
|
|
|
"debug.dir",
|
|
|
|
"scripts.dir",
|
|
|
|
)
|
|
|
|
|
|
|
|
with zipfile.ZipFile(
|
|
|
|
self.get_dist_path(self.get_dist_file_name("sdk", "zip")),
|
|
|
|
"w",
|
|
|
|
zipfile.ZIP_DEFLATED,
|
|
|
|
) as zf:
|
|
|
|
for component_key in sdk_components_keys:
|
|
|
|
component_path = self._dist_components.get(component_key)
|
|
|
|
|
|
|
|
if component_key.endswith(".dir"):
|
2023-04-10 14:46:22 +00:00
|
|
|
components_paths[component_key] = basename(component_path)
|
2023-04-06 02:44:37 +00:00
|
|
|
for root, dirnames, files in walk(component_path):
|
|
|
|
if "__pycache__" in dirnames:
|
|
|
|
dirnames.remove("__pycache__")
|
|
|
|
for file in files:
|
|
|
|
zf.write(
|
|
|
|
join(root, file),
|
|
|
|
join(
|
|
|
|
components_paths[component_key],
|
|
|
|
relpath(
|
|
|
|
join(root, file),
|
|
|
|
component_path,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
else:
|
2023-04-10 14:46:22 +00:00
|
|
|
# We use fixed names for files to avoid having to regenerate VSCode project
|
|
|
|
components_paths[component_key] = component_key
|
|
|
|
zf.write(component_path, component_key)
|
2023-04-06 02:44:37 +00:00
|
|
|
|
|
|
|
zf.writestr(
|
|
|
|
"components.json",
|
|
|
|
json.dumps(
|
|
|
|
{
|
|
|
|
"meta": {
|
|
|
|
"hw_target": self.target,
|
|
|
|
"flavor": self.flavor,
|
|
|
|
"version": self.args.version,
|
|
|
|
},
|
|
|
|
"components": components_paths,
|
|
|
|
}
|
2022-06-26 12:00:03 +00:00
|
|
|
),
|
2023-04-06 02:44:37 +00:00
|
|
|
)
|
2022-10-12 16:12:05 +00:00
|
|
|
|
2023-04-06 02:44:37 +00:00
|
|
|
def bundle_update_package(self):
|
|
|
|
self.logger.debug(
|
|
|
|
f"Generating update bundle with version {self.args.version} for {self.target}"
|
|
|
|
)
|
|
|
|
bundle_dir_name = f"{self.target}-update-{self.args.suffix}"[
|
|
|
|
: self.DIST_FOLDER_MAX_NAME_LENGTH
|
|
|
|
]
|
|
|
|
bundle_dir = self.get_dist_path(bundle_dir_name)
|
|
|
|
bundle_args = [
|
|
|
|
"generate",
|
|
|
|
"-d",
|
|
|
|
bundle_dir,
|
|
|
|
"-v",
|
|
|
|
self.args.version,
|
|
|
|
"-t",
|
|
|
|
self.target,
|
|
|
|
"--dfu",
|
|
|
|
self.get_dist_path(
|
|
|
|
self.get_project_file_name(self.projects["firmware"], "dfu")
|
|
|
|
),
|
|
|
|
"--stage",
|
|
|
|
self.get_dist_path(
|
|
|
|
self.get_project_file_name(self.projects["updater"], "bin")
|
|
|
|
),
|
|
|
|
]
|
|
|
|
if self.args.resources:
|
|
|
|
bundle_args.extend(
|
|
|
|
(
|
|
|
|
"-r",
|
|
|
|
self.args.resources,
|
2022-10-12 16:12:05 +00:00
|
|
|
)
|
2023-04-06 02:44:37 +00:00
|
|
|
)
|
|
|
|
bundle_args.extend(self.other_args)
|
2022-10-12 16:12:05 +00:00
|
|
|
|
2023-04-06 02:44:37 +00:00
|
|
|
if (bundle_result := UpdateMain(no_exit=True)(bundle_args)) == 0:
|
|
|
|
self.note_dist_component("update", "dir", bundle_dir)
|
|
|
|
self.logger.info(
|
|
|
|
fg.boldgreen(
|
|
|
|
f"Use this directory to self-update your Flipper:\n\t{bundle_dir}"
|
|
|
|
)
|
|
|
|
)
|
2022-04-13 20:50:25 +00:00
|
|
|
|
2023-04-06 02:44:37 +00:00
|
|
|
# Create tgz archive
|
|
|
|
with tarfile.open(
|
|
|
|
join(
|
|
|
|
self.output_dir_path,
|
|
|
|
bundle_tgz := f"{self.DIST_FILE_PREFIX}{bundle_dir_name}.tgz",
|
|
|
|
),
|
|
|
|
"w:gz",
|
|
|
|
compresslevel=9,
|
|
|
|
format=tarfile.USTAR_FORMAT,
|
|
|
|
) as tar:
|
|
|
|
self.note_dist_component(
|
|
|
|
"update", "tgz", self.get_dist_path(bundle_tgz)
|
|
|
|
)
|
|
|
|
tar.add(bundle_dir, arcname=bundle_dir_name)
|
|
|
|
return bundle_result
|
2022-04-13 20:50:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
Main()()
|