distfap.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python3
  2. import os
  3. import posixpath
  4. from flipper.app import App
  5. from flipper.storage import FlipperStorage, FlipperStorageOperations
  6. from flipper.utils.cdc import resolve_port
  7. class Main(App):
  8. def init(self):
  9. self.parser.add_argument("-p", "--port", help="CDC Port", default="auto")
  10. self.parser.add_argument(
  11. "-n",
  12. "--no-launch",
  13. dest="launch_app",
  14. action="store_false",
  15. help="Don't launch app",
  16. )
  17. self.parser.add_argument("fap_src_path", help="App file to upload")
  18. self.parser.add_argument(
  19. "--fap_dst_dir", help="Upload path", default="/ext/apps", required=False
  20. )
  21. self.parser.set_defaults(func=self.install)
  22. def install(self):
  23. if not (port := resolve_port(self.logger, self.args.port)):
  24. return 1
  25. try:
  26. with FlipperStorage(port) as storage:
  27. storage_ops = FlipperStorageOperations(storage)
  28. fap_local_path = self.args.fap_src_path
  29. self.args.fap_dst_dir = self.args.fap_dst_dir.rstrip("/\\")
  30. if not os.path.isfile(fap_local_path):
  31. self.logger.error(
  32. f"Error: source .fap ({fap_local_path}) not found"
  33. )
  34. return 2
  35. fap_dst_path = posixpath.join(
  36. self.args.fap_dst_dir, os.path.basename(fap_local_path)
  37. )
  38. self.logger.info(f'Installing "{fap_local_path}" to {fap_dst_path}')
  39. storage_ops.recursive_send(fap_dst_path, fap_local_path, False)
  40. if not self.args.launch_app:
  41. return 0
  42. storage.send_and_wait_eol(
  43. f'loader open "Applications" {fap_dst_path}\r'
  44. )
  45. if len(result := storage.read.until(storage.CLI_EOL)):
  46. self.logger.error(f"Unexpected response: {result.decode('ascii')}")
  47. return 3
  48. return 0
  49. except Exception as e:
  50. self.logger.error(f"Error: {e}")
  51. # raise
  52. return 4
  53. if __name__ == "__main__":
  54. Main()()