2022-04-13 23:50:25 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2023-04-06 05:44:37 +03:00
|
|
|
import json
|
2022-04-13 23:50:25 +03:00
|
|
|
import shutil
|
2022-10-12 19:12:05 +03:00
|
|
|
import tarfile
|
2023-04-06 05:44:37 +03:00
|
|
|
import zipfile
|
|
|
|
from os import makedirs, walk
|
2023-05-03 08:48:49 +03:00
|
|
|
from os.path import basename, exists, join, relpath
|
2023-04-06 05:44:37 +03:00
|
|
|
|
2022-10-26 01:15:02 +03:00
|
|
|
from ansi.color import fg
|
2023-04-06 05:44:37 +03:00
|
|
|
from flipper.app import App
|
|
|
|
from update import Main as UpdateMain
|
2022-04-13 23:50:25 +03:00
|
|
|
|
|
|
|
|
2022-06-26 15:00:03 +03: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 23:50:25 +03:00
|
|
|
class Main(App):
|
2022-10-12 19:12:05 +03:00
|
|
|
DIST_FILE_PREFIX = "flipper-z-"
|
2022-10-13 18:05:07 +03:00
|
|
|
DIST_FOLDER_MAX_NAME_LENGTH = 80
|
2022-10-12 19:12:05 +03:00
|
|
|
|
2022-04-13 23:50:25 +03: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 15:00:03 +03:00
|
|
|
self.parser_copy.add_argument("-p", dest="project", nargs="+", required=True)
|
2022-04-13 23:50:25 +03:00
|
|
|
self.parser_copy.add_argument("-s", dest="suffix", required=True)
|
2022-04-19 22:02:37 +03:00
|
|
|
self.parser_copy.add_argument("-r", dest="resources", required=False)
|
2022-04-13 23:50:25 +03: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 18:32:06 +03:00
|
|
|
def get_project_file_name(self, project: ProjectDir, filetype: str) -> str:
|
2022-04-14 00:29:58 +03:00
|
|
|
# Temporary fix
|
2022-06-26 15:00:03 +03:00
|
|
|
project_name = project.project
|
2022-10-28 18:32:06 +03:00
|
|
|
if project_name == "firmware" and filetype != "elf":
|
|
|
|
project_name = "full"
|
2022-10-12 19:12:05 +03:00
|
|
|
|
2023-04-06 05:44:37 +03: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:
|
2024-02-16 10:20:45 +03:00
|
|
|
component_key = f"{component}.{extension}"
|
|
|
|
if component_key in self._dist_components:
|
|
|
|
self.logger.debug(
|
|
|
|
f"Skipping duplicate component {component_key} in {srcpath}"
|
|
|
|
)
|
|
|
|
return
|
|
|
|
self._dist_components[component_key] = srcpath
|
2022-04-13 23:50:25 +03:00
|
|
|
|
2022-10-28 18:32:06 +03: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 05:44:37 +03:00
|
|
|
def get_dist_path(self, filename: str) -> str:
|
2022-04-13 23:50:25 +03:00
|
|
|
return join(self.output_dir_path, filename)
|
|
|
|
|
2022-10-28 18:32:06 +03:00
|
|
|
def copy_single_project(self, project: ProjectDir) -> None:
|
2022-06-26 15:00:03 +03:00
|
|
|
obj_directory = join("build", project.dir)
|
2022-04-13 23:50:25 +03:00
|
|
|
|
|
|
|
for filetype in ("elf", "bin", "dfu", "json"):
|
2022-10-12 19:12:05 +03:00
|
|
|
if exists(src_file := join(obj_directory, f"{project.project}.{filetype}")):
|
|
|
|
shutil.copyfile(
|
|
|
|
src_file,
|
2023-04-06 05:44:37 +03:00
|
|
|
self.get_dist_path(self.get_project_file_name(project, filetype)),
|
2022-10-12 19:12:05 +03:00
|
|
|
)
|
2023-04-06 05:44:37 +03:00
|
|
|
for foldertype in ("sdk_headers", "lib"):
|
2022-10-28 18:32:06 +03:00
|
|
|
if exists(sdk_folder := join(obj_directory, foldertype)):
|
2023-04-06 05:44:37 +03:00
|
|
|
self.note_dist_component(foldertype, "dir", sdk_folder)
|
2022-10-28 18:32:06 +03:00
|
|
|
|
|
|
|
def copy(self) -> int:
|
2023-04-06 05:44:37 +03:00
|
|
|
self._dist_components: dict[str, str] = dict()
|
|
|
|
self.projects: dict[str, ProjectDir] = dict(
|
2022-06-26 15:00:03 +03: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 23:50:25 +03:00
|
|
|
if exists(self.output_dir_path) and not self.args.noclean:
|
2022-06-26 15:00:03 +03:00
|
|
|
try:
|
|
|
|
shutil.rmtree(self.output_dir_path)
|
|
|
|
except Exception as ex:
|
2023-04-06 05:44:37 +03:00
|
|
|
self.logger.warn(f"Failed to clean output directory: {ex}")
|
2022-04-13 23:50:25 +03:00
|
|
|
|
|
|
|
if not exists(self.output_dir_path):
|
2023-04-06 05:44:37 +03:00
|
|
|
self.logger.debug(f"Creating output directory {self.output_dir_path}")
|
2022-04-13 23:50:25 +03:00
|
|
|
makedirs(self.output_dir_path)
|
|
|
|
|
2023-04-06 05:44:37 +03:00
|
|
|
for folder in ("debug", "scripts"):
|
|
|
|
if exists(folder):
|
|
|
|
self.note_dist_component(folder, "dir", folder)
|
|
|
|
|
2022-06-26 15:00:03 +03:00
|
|
|
for project in self.projects.values():
|
2023-04-06 05:44:37 +03:00
|
|
|
self.logger.debug(f"Copying {project.project} for {project.target}")
|
2022-04-13 23:50:25 +03:00
|
|
|
self.copy_single_project(project)
|
|
|
|
|
|
|
|
self.logger.info(
|
2022-11-02 18:15:40 +03:00
|
|
|
fg.boldgreen(
|
|
|
|
f"Firmware binaries can be found at:\n\t{self.output_dir_path}"
|
|
|
|
)
|
2022-04-13 23:50:25 +03:00
|
|
|
)
|
2022-06-26 15:00:03 +03:00
|
|
|
|
2022-04-13 23:50:25 +03:00
|
|
|
if self.args.version:
|
2023-04-06 05:44:37 +03: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",
|
|
|
|
"scripts.dir",
|
|
|
|
)
|
|
|
|
|
2024-02-16 10:20:45 +03:00
|
|
|
sdk_bundle_path = self.get_dist_path(self.get_dist_file_name("sdk", "zip"))
|
2023-04-06 05:44:37 +03:00
|
|
|
with zipfile.ZipFile(
|
2024-02-16 10:20:45 +03:00
|
|
|
sdk_bundle_path,
|
2023-04-06 05:44:37 +03:00
|
|
|
"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 17:46:22 +03:00
|
|
|
components_paths[component_key] = basename(component_path)
|
2023-04-06 05:44:37 +03: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 17:46:22 +03: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 05:44:37 +03: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 15:00:03 +03:00
|
|
|
),
|
2023-04-06 05:44:37 +03:00
|
|
|
)
|
2022-10-12 19:12:05 +03:00
|
|
|
|
2024-02-16 10:20:45 +03:00
|
|
|
self.logger.info(
|
|
|
|
fg.boldgreen(f"SDK bundle can be found at:\n\t{sdk_bundle_path}")
|
|
|
|
)
|
|
|
|
|
2023-04-06 05:44:37 +03: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 19:12:05 +03:00
|
|
|
)
|
2023-04-06 05:44:37 +03:00
|
|
|
)
|
|
|
|
bundle_args.extend(self.other_args)
|
2022-10-12 19:12:05 +03:00
|
|
|
|
2023-04-06 05:44:37 +03: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 23:50:25 +03:00
|
|
|
|
2023-04-06 05:44:37 +03: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)
|
|
|
|
)
|
2023-05-22 12:14:18 +03:00
|
|
|
|
|
|
|
# Strip uid and gid in case of overflow
|
|
|
|
def tar_filter(tarinfo):
|
|
|
|
tarinfo.uid = tarinfo.gid = 0
|
2023-05-23 13:51:21 +03:00
|
|
|
tarinfo.mtime = 0
|
|
|
|
tarinfo.uname = tarinfo.gname = "furippa"
|
2023-05-22 12:14:18 +03:00
|
|
|
return tarinfo
|
|
|
|
|
|
|
|
tar.add(bundle_dir, arcname=bundle_dir_name, filter=tar_filter)
|
2023-04-06 05:44:37 +03:00
|
|
|
return bundle_result
|
2022-04-13 23:50:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
Main()()
|