main.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2015-2016 Peter Magnusson <peter@birchroad.net>
  4. import argparse
  5. import logging
  6. import os
  7. from .uploader import Uploader
  8. from .term import terminal
  9. log = logging.getLogger(__name__)
  10. def destination_from_source(sources):
  11. destinations = []
  12. for i in range(0, len(sources)):
  13. sd = sources[i].split(':')
  14. if len(sd) == 2:
  15. destinations.append(sd[1])
  16. sources[i] = sd[0]
  17. else:
  18. destinations.append(sd[0])
  19. return destinations
  20. def operation_upload(uploader, sources, verify, do_compile, do_file, do_restart):
  21. destinations = destination_from_source(sources)
  22. if len(destinations) == len(sources):
  23. uploader.prepare()
  24. for f, d in zip(sources, destinations):
  25. if do_compile:
  26. uploader.file_remove(os.path.splitext(d)[0]+'.lc')
  27. uploader.write_file(f, d, verify)
  28. if do_compile and d != 'init.lua':
  29. uploader.file_compile(d)
  30. uploader.file_remove(d)
  31. if do_file:
  32. uploader.file_do(os.path.splitext(d)[0]+'.lc')
  33. elif do_file:
  34. uploader.file_do(d)
  35. else:
  36. raise Exception('You must specify a destination filename for each file you want to upload.')
  37. if do_restart:
  38. uploader.node_restart()
  39. log.info('All done!')
  40. def operation_download(uploader, sources):
  41. destinations = destination_from_source(sources)
  42. if len(destinations) == len(sources):
  43. for f, d in zip(sources, destinations):
  44. uploader.read_file(f, d)
  45. else:
  46. raise Exception('You must specify a destination filename for each file you want to download.')
  47. log.info('All done!')
  48. def operation_file(uploader, cmd, filename=''):
  49. if cmd == 'list':
  50. uploader.file_list()
  51. if cmd == 'do':
  52. for f in filename:
  53. uploader.file_do(f)
  54. elif cmd == 'format':
  55. uploader.file_format()
  56. elif cmd == 'remove':
  57. for f in filename:
  58. uploader.file_remove(f)
  59. def arg_auto_int(value):
  60. """parsing function for integer arguments"""
  61. return int(value, 0)
  62. def main_func():
  63. parser = argparse.ArgumentParser(
  64. description='NodeMCU Lua file uploader',
  65. prog='nodemcu-uploader'
  66. )
  67. parser.add_argument(
  68. '--verbose',
  69. help='verbose output',
  70. action='store_true',
  71. default=False)
  72. parser.add_argument(
  73. '--port', '-p',
  74. help='Serial port device',
  75. default=Uploader.PORT)
  76. parser.add_argument(
  77. '--baud', '-b',
  78. help='Serial port baudrate',
  79. type=arg_auto_int,
  80. default=Uploader.BAUD)
  81. subparsers = parser.add_subparsers(
  82. dest='operation',
  83. help='Run nodemcu-uploader {command} -h for additional help')
  84. upload_parser = subparsers.add_parser(
  85. 'upload',
  86. help='Path to one or more files to be uploaded. Destination name will be the same as the file name.')
  87. upload_parser.add_argument(
  88. 'filename',
  89. nargs='+',
  90. help='Lua file to upload. Use colon to give alternate destination.'
  91. )
  92. upload_parser.add_argument(
  93. '--compile', '-c',
  94. help='If file should be uploaded as compiled',
  95. action='store_true',
  96. default=False
  97. )
  98. upload_parser.add_argument(
  99. '--verify', '-v',
  100. help='To verify the uploaded data.',
  101. action='store',
  102. nargs='?',
  103. choices=['standard', 'sha1'],
  104. default='standard'
  105. )
  106. upload_parser.add_argument(
  107. '--dofile', '-e',
  108. help='If file should be run after upload.',
  109. action='store_true',
  110. default=False
  111. )
  112. upload_parser.add_argument(
  113. '--restart', '-r',
  114. help='If esp should be restarted',
  115. action='store_true',
  116. default=False
  117. )
  118. exec_parser = subparsers.add_parser(
  119. 'exec',
  120. help='Path to one or more files to be executed line by line.')
  121. exec_parser.add_argument('filename', nargs='+', help='Lua file to execute.')
  122. download_parser = subparsers.add_parser(
  123. 'download',
  124. help='Path to one or more files to be downloaded. Destination name will be the same as the file name.')
  125. download_parser.add_argument('filename', nargs='+', help='Lua file to download. Use colon to give alternate destination.')
  126. file_parser = subparsers.add_parser(
  127. 'file',
  128. help='File functions')
  129. file_parser.add_argument(
  130. 'cmd',
  131. choices=('list', 'do', 'format', 'remove'),
  132. help="list=list files, do=dofile given path, format=formate file area, remove=remove given path")
  133. file_parser.add_argument('filename', nargs='*', help='path for cmd')
  134. node_parse = subparsers.add_parser(
  135. 'node',
  136. help='Node functions')
  137. node_parse.add_argument('ncmd', choices=('heap', 'restart'), help="heap=print heap memory, restart=restart nodemcu")
  138. terminal_parser = subparsers.add_parser(
  139. 'terminal',
  140. help='Run pySerials miniterm'
  141. )
  142. args = parser.parse_args()
  143. default_level = logging.INFO
  144. if args.verbose:
  145. default_level = logging.DEBUG
  146. #formatter = logging.Formatter('%(message)s')
  147. logging.basicConfig(level=default_level, format='%(message)s')
  148. uploader = Uploader(args.port, args.baud)
  149. if args.operation == 'upload':
  150. operation_upload(uploader, args.filename, args.verify, args.compile, args.dofile,
  151. args.restart)
  152. elif args.operation == 'download':
  153. operation_download(uploader, args.filename)
  154. elif args.operation == 'exec':
  155. sources = args.filename
  156. for f in sources:
  157. uploader.exec_file(f)
  158. elif args.operation == 'file':
  159. operation_file(uploader, args.cmd, args.filename)
  160. elif args.operation == 'node':
  161. if args.ncmd == 'heap':
  162. uploader.node_heap()
  163. elif args.ncmd == 'restart':
  164. uploader.node_restart()
  165. #no uploader related commands after this point
  166. uploader.close()
  167. if args.operation == 'terminal':
  168. #uploader can not claim the port
  169. uploader.close()
  170. terminal(args.port)