2022-06-28 16:03:49 +03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import pathlib
|
2023-05-03 08:48:49 +03:00
|
|
|
|
|
|
|
from flipper.app import App
|
|
|
|
from flipper.storage import FlipperStorage, FlipperStorageOperations
|
|
|
|
from flipper.utils.cdc import resolve_port
|
2022-06-28 16:03:49 +03:00
|
|
|
|
|
|
|
|
2022-06-30 19:06:12 +03:00
|
|
|
class Main(App):
|
|
|
|
def init(self):
|
2022-06-28 16:03:49 +03:00
|
|
|
self.parser.add_argument("-p", "--port", help="CDC Port", default="auto")
|
|
|
|
|
2022-06-30 19:06:12 +03:00
|
|
|
self.parser.add_argument("manifest_path", help="Manifest path")
|
|
|
|
self.parser.add_argument(
|
2023-05-08 14:11:23 +03:00
|
|
|
"--pkg_dir_name", help="Update dir name", default=None, required=False
|
2022-06-28 16:03:49 +03:00
|
|
|
)
|
2022-06-30 19:06:12 +03:00
|
|
|
self.parser.set_defaults(func=self.install)
|
2022-06-28 16:03:49 +03:00
|
|
|
|
|
|
|
# logging
|
|
|
|
self.logger = logging.getLogger()
|
|
|
|
|
|
|
|
def install(self):
|
2022-06-30 19:06:12 +03:00
|
|
|
if not (port := resolve_port(self.logger, self.args.port)):
|
2022-06-28 16:03:49 +03:00
|
|
|
return 1
|
|
|
|
|
2023-03-14 17:29:28 +03:00
|
|
|
if not os.path.isfile(self.args.manifest_path):
|
|
|
|
self.logger.error("Error: manifest not found")
|
|
|
|
return 2
|
2022-06-28 16:03:49 +03:00
|
|
|
|
2023-03-14 17:29:28 +03:00
|
|
|
manifest_path = pathlib.Path(os.path.abspath(self.args.manifest_path))
|
|
|
|
manifest_name, pkg_name = manifest_path.parts[-1], manifest_path.parts[-2]
|
2022-06-30 19:06:12 +03:00
|
|
|
|
2023-03-14 17:29:28 +03:00
|
|
|
pkg_dir_name = self.args.pkg_dir_name or pkg_name
|
|
|
|
update_root = "/ext/update"
|
|
|
|
flipper_update_path = f"{update_root}/{pkg_dir_name}"
|
2022-06-30 19:06:12 +03:00
|
|
|
|
2023-03-14 17:29:28 +03:00
|
|
|
self.logger.info(f'Installing "{pkg_name}" from {flipper_update_path}')
|
2022-06-30 19:06:12 +03:00
|
|
|
|
2023-03-14 17:29:28 +03:00
|
|
|
try:
|
|
|
|
with FlipperStorage(port) as storage:
|
|
|
|
storage_ops = FlipperStorageOperations(storage)
|
|
|
|
storage_ops.mkpath(update_root)
|
|
|
|
storage_ops.mkpath(flipper_update_path)
|
|
|
|
storage_ops.recursive_send(
|
|
|
|
flipper_update_path, manifest_path.parents[0]
|
|
|
|
)
|
2022-06-30 19:06:12 +03:00
|
|
|
|
|
|
|
storage.send_and_wait_eol(
|
|
|
|
f"update install {flipper_update_path}/{manifest_name}\r"
|
|
|
|
)
|
2022-07-04 16:36:12 +03:00
|
|
|
result = storage.read.until(storage.CLI_EOL)
|
2023-05-03 08:48:49 +03:00
|
|
|
if b"Verifying" not in result:
|
2022-07-04 16:36:12 +03:00
|
|
|
self.logger.error(f"Unexpected response: {result.decode('ascii')}")
|
2023-03-14 17:29:28 +03:00
|
|
|
return 3
|
2022-07-04 16:36:12 +03:00
|
|
|
result = storage.read.until(storage.CLI_EOL)
|
|
|
|
if not result.startswith(b"OK"):
|
|
|
|
self.logger.error(result.decode("ascii"))
|
2023-03-14 17:29:28 +03:00
|
|
|
return 4
|
|
|
|
return 0
|
|
|
|
except Exception as e:
|
|
|
|
self.logger.error(e)
|
|
|
|
return 5
|
2022-06-28 16:03:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
Main()()
|