main.py 7.2 KB

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