2022-04-13 23:50:25 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
from flipper.app import App
|
|
|
|
from os.path import join, exists
|
|
|
|
from os import makedirs
|
|
|
|
from update import Main as UpdateMain
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
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)
|
|
|
|
|
|
|
|
def get_project_filename(self, project, filetype):
|
2022-04-14 00:29:58 +03:00
|
|
|
# Temporary fix
|
2022-06-26 15:00:03 +03:00
|
|
|
project_name = project.project
|
|
|
|
if project_name == "firmware" and filetype != "elf":
|
|
|
|
project_name = "full"
|
|
|
|
return f"flipper-z-{self.target}-{project_name}-{self.args.suffix}.{filetype}"
|
2022-04-13 23:50:25 +03:00
|
|
|
|
|
|
|
def get_dist_filepath(self, filename):
|
|
|
|
return join(self.output_dir_path, filename)
|
|
|
|
|
|
|
|
def copy_single_project(self, project):
|
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"):
|
|
|
|
shutil.copyfile(
|
2022-06-26 15:00:03 +03:00
|
|
|
join(obj_directory, f"{project.project}.{filetype}"),
|
2022-04-13 23:50:25 +03:00
|
|
|
self.get_dist_filepath(self.get_project_filename(project, filetype)),
|
|
|
|
)
|
|
|
|
|
|
|
|
def copy(self):
|
2022-06-26 15:00:03 +03:00
|
|
|
self.projects = dict(
|
|
|
|
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:
|
|
|
|
pass
|
2022-04-13 23:50:25 +03:00
|
|
|
|
|
|
|
if not exists(self.output_dir_path):
|
|
|
|
makedirs(self.output_dir_path)
|
|
|
|
|
2022-06-26 15:00:03 +03:00
|
|
|
for project in self.projects.values():
|
2022-04-13 23:50:25 +03:00
|
|
|
self.copy_single_project(project)
|
|
|
|
|
|
|
|
self.logger.info(
|
|
|
|
f"Firmware binaries can be found at:\n\t{self.output_dir_path}"
|
|
|
|
)
|
2022-06-26 15:00:03 +03:00
|
|
|
|
2022-04-13 23:50:25 +03:00
|
|
|
if self.args.version:
|
|
|
|
bundle_dir = join(
|
2022-06-26 15:00:03 +03:00
|
|
|
self.output_dir_path, f"{self.target}-update-{self.args.suffix}"
|
2022-04-13 23:50:25 +03:00
|
|
|
)
|
|
|
|
bundle_args = [
|
|
|
|
"generate",
|
|
|
|
"-d",
|
|
|
|
bundle_dir,
|
|
|
|
"-v",
|
|
|
|
self.args.version,
|
|
|
|
"-t",
|
2022-06-26 15:00:03 +03:00
|
|
|
self.target,
|
2022-04-19 22:02:37 +03:00
|
|
|
"--dfu",
|
2022-06-26 15:00:03 +03:00
|
|
|
self.get_dist_filepath(
|
|
|
|
self.get_project_filename(self.projects["firmware"], "dfu")
|
|
|
|
),
|
2022-04-19 22:02:37 +03:00
|
|
|
"--stage",
|
2022-06-26 15:00:03 +03:00
|
|
|
self.get_dist_filepath(
|
|
|
|
self.get_project_filename(self.projects["updater"], "bin")
|
|
|
|
),
|
2022-04-13 23:50:25 +03:00
|
|
|
]
|
2022-04-19 22:02:37 +03:00
|
|
|
if self.args.resources:
|
2022-04-19 11:03:28 +03:00
|
|
|
bundle_args.extend(
|
|
|
|
(
|
2022-04-19 22:02:37 +03:00
|
|
|
"-r",
|
|
|
|
self.args.resources,
|
2022-04-19 11:03:28 +03:00
|
|
|
)
|
|
|
|
)
|
2022-04-27 18:53:48 +03:00
|
|
|
bundle_args.extend(self.other_args)
|
2022-04-13 23:50:25 +03:00
|
|
|
self.logger.info(
|
|
|
|
f"Use this directory to self-update your Flipper:\n\t{bundle_dir}"
|
|
|
|
)
|
|
|
|
return UpdateMain(no_exit=True)(bundle_args)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
Main()()
|