2022-06-28 13:03:49 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2022-06-30 16:06:12 +00:00
|
|
|
from typing import final
|
|
|
|
from flipper.app import App
|
2022-06-28 13:03:49 +00:00
|
|
|
from flipper.storage import FlipperStorage
|
2022-06-30 16:06:12 +00:00
|
|
|
from flipper.utils.cdc import resolve_port
|
2022-06-28 13:03:49 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import pathlib
|
|
|
|
import serial.tools.list_ports as list_ports
|
|
|
|
|
|
|
|
|
2022-06-30 16:06:12 +00:00
|
|
|
class Main(App):
|
|
|
|
def init(self):
|
2022-06-28 13:03:49 +00:00
|
|
|
self.parser.add_argument("-p", "--port", help="CDC Port", default="auto")
|
|
|
|
|
2022-06-30 16:06:12 +00:00
|
|
|
self.parser.add_argument("manifest_path", help="Manifest path")
|
|
|
|
self.parser.add_argument(
|
2022-06-28 13:03:49 +00:00
|
|
|
"--pkg_dir_name", help="Update dir name", default="pcbundle", required=False
|
|
|
|
)
|
2022-06-30 16:06:12 +00:00
|
|
|
self.parser.set_defaults(func=self.install)
|
2022-06-28 13:03:49 +00:00
|
|
|
|
|
|
|
# logging
|
|
|
|
self.logger = logging.getLogger()
|
|
|
|
|
|
|
|
# make directory with exist check
|
|
|
|
def mkdir_on_storage(self, storage, flipper_dir_path):
|
|
|
|
if not storage.exist_dir(flipper_dir_path):
|
|
|
|
self.logger.debug(f'"{flipper_dir_path}" does not exist, creating')
|
|
|
|
if not storage.mkdir(flipper_dir_path):
|
|
|
|
self.logger.error(f"Error: {storage.last_error}")
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
self.logger.debug(f'"{flipper_dir_path}" already exists')
|
|
|
|
return True
|
|
|
|
|
|
|
|
# send file with exist check and hash check
|
|
|
|
def send_file_to_storage(self, storage, flipper_file_path, local_file_path, force):
|
|
|
|
exists = storage.exist_file(flipper_file_path)
|
|
|
|
do_upload = not exists
|
|
|
|
if exists:
|
|
|
|
hash_local = storage.hash_local(local_file_path)
|
|
|
|
hash_flipper = storage.hash_flipper(flipper_file_path)
|
|
|
|
self.logger.debug(f"hash check: local {hash_local}, flipper {hash_flipper}")
|
|
|
|
do_upload = force or (hash_local != hash_flipper)
|
|
|
|
|
|
|
|
if do_upload:
|
|
|
|
self.logger.info(f'Sending "{local_file_path}" to "{flipper_file_path}"')
|
|
|
|
if not storage.send_file(local_file_path, flipper_file_path):
|
|
|
|
self.logger.error(f"Error: {storage.last_error}")
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def install(self):
|
2022-06-30 16:06:12 +00:00
|
|
|
if not (port := resolve_port(self.logger, self.args.port)):
|
2022-06-28 13:03:49 +00:00
|
|
|
return 1
|
|
|
|
|
2022-08-02 13:46:43 +00:00
|
|
|
storage = FlipperStorage(port)
|
2022-06-28 13:03:49 +00:00
|
|
|
storage.start()
|
|
|
|
|
2022-06-30 16:06:12 +00:00
|
|
|
try:
|
|
|
|
if not os.path.isfile(self.args.manifest_path):
|
|
|
|
self.logger.error("Error: manifest not found")
|
|
|
|
return 2
|
|
|
|
|
|
|
|
manifest_path = pathlib.Path(os.path.abspath(self.args.manifest_path))
|
|
|
|
manifest_name, pkg_name = manifest_path.parts[-1], manifest_path.parts[-2]
|
|
|
|
|
|
|
|
pkg_dir_name = self.args.pkg_dir_name or pkg_name
|
2022-07-04 13:36:12 +00:00
|
|
|
update_root = "/ext/update"
|
|
|
|
flipper_update_path = f"{update_root}/{pkg_dir_name}"
|
2022-06-30 16:06:12 +00:00
|
|
|
|
|
|
|
self.logger.info(f'Installing "{pkg_name}" from {flipper_update_path}')
|
|
|
|
# if not os.path.exists(self.args.manifest_path):
|
|
|
|
# self.logger.error("Error: package not found")
|
2022-07-04 13:36:12 +00:00
|
|
|
if not self.mkdir_on_storage(
|
|
|
|
storage, update_root
|
|
|
|
) or not self.mkdir_on_storage(storage, flipper_update_path):
|
2022-06-30 16:06:12 +00:00
|
|
|
self.logger.error(f"Error: cannot create {storage.last_error}")
|
|
|
|
return -2
|
|
|
|
|
|
|
|
for dirpath, dirnames, filenames in os.walk(manifest_path.parents[0]):
|
|
|
|
for fname in filenames:
|
|
|
|
self.logger.debug(f"Uploading {fname}")
|
|
|
|
local_file_path = os.path.join(dirpath, fname)
|
|
|
|
flipper_file_path = f"{flipper_update_path}/{fname}"
|
|
|
|
if not self.send_file_to_storage(
|
|
|
|
storage, flipper_file_path, local_file_path, False
|
|
|
|
):
|
|
|
|
self.logger.error(f"Error: {storage.last_error}")
|
|
|
|
return -3
|
|
|
|
|
2022-08-02 13:46:43 +00:00
|
|
|
# return -11
|
2022-06-30 16:06:12 +00:00
|
|
|
storage.send_and_wait_eol(
|
|
|
|
f"update install {flipper_update_path}/{manifest_name}\r"
|
|
|
|
)
|
2022-07-04 13:36:12 +00:00
|
|
|
result = storage.read.until(storage.CLI_EOL)
|
|
|
|
if not b"Verifying" in result:
|
|
|
|
self.logger.error(f"Unexpected response: {result.decode('ascii')}")
|
|
|
|
return -4
|
|
|
|
result = storage.read.until(storage.CLI_EOL)
|
|
|
|
if not result.startswith(b"OK"):
|
|
|
|
self.logger.error(result.decode("ascii"))
|
|
|
|
return -5
|
2022-06-30 16:06:12 +00:00
|
|
|
break
|
|
|
|
return 0
|
|
|
|
finally:
|
|
|
|
storage.stop()
|
2022-06-28 13:03:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
Main()()
|