main.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. subparsers = parser.add_subparsers(
  104. dest='operation',
  105. help='Run nodemcu-uploader {command} -h for additional help')
  106. upload_parser = subparsers.add_parser(
  107. 'upload',
  108. help='Path to one or more files to be uploaded. Destination name will be the same as the file name.')
  109. upload_parser.add_argument(
  110. 'filename',
  111. nargs='+',
  112. help='Lua file to upload. Use colon to give alternate destination.'
  113. )
  114. upload_parser.add_argument(
  115. '--compile', '-c',
  116. help='If file should be uploaded as compiled',
  117. action='store_true',
  118. default=False
  119. )
  120. upload_parser.add_argument(
  121. '--verify', '-v',
  122. help='To verify the uploaded data.',
  123. action='store',
  124. nargs='?',
  125. choices=['none', 'raw', 'sha1'],
  126. default='none'
  127. )
  128. upload_parser.add_argument(
  129. '--dofile', '-e',
  130. help='If file should be run after upload.',
  131. action='store_true',
  132. default=False
  133. )
  134. upload_parser.add_argument(
  135. '--restart', '-r',
  136. help='If esp should be restarted',
  137. action='store_true',
  138. default=False
  139. )
  140. exec_parser = subparsers.add_parser(
  141. 'exec',
  142. help='Path to one or more files to be executed line by line.')
  143. exec_parser.add_argument('filename', nargs='+', help='Lua file to execute.')
  144. download_parser = subparsers.add_parser(
  145. 'download',
  146. help='Path to one or more files to be downloaded. Destination name will be the same as the file name.')
  147. download_parser.add_argument('filename', nargs='+', help='Lua file to download. Use colon to give alternate destination.')
  148. file_parser = subparsers.add_parser(
  149. 'file',
  150. help='File functions')
  151. file_parser.add_argument(
  152. 'cmd',
  153. choices=('list', 'do', 'format', 'remove', 'print'),
  154. help="list=list files, do=dofile given path, format=formate file area, remove=remove given path")
  155. file_parser.add_argument('filename', nargs='*', help='path for cmd')
  156. node_parse = subparsers.add_parser(
  157. 'node',
  158. help='Node functions')
  159. node_parse.add_argument('ncmd', choices=('heap', 'restart'), help="heap=print heap memory, restart=restart nodemcu")
  160. terminal_parser = subparsers.add_parser(
  161. 'terminal',
  162. help='Run pySerials miniterm'
  163. )
  164. args = parser.parse_args()
  165. default_level = logging.INFO
  166. if args.verbose:
  167. default_level = logging.DEBUG
  168. #formatter = logging.Formatter('%(message)s')
  169. logging.basicConfig(level=default_level, format='%(message)s')
  170. uploader = Uploader(args.port, args.baud)
  171. if args.operation == 'upload':
  172. operation_upload(uploader, args.filename, args.verify, args.compile, args.dofile,
  173. args.restart)
  174. elif args.operation == 'download':
  175. operation_download(uploader, args.filename)
  176. elif args.operation == 'exec':
  177. sources = args.filename
  178. for f in sources:
  179. uploader.exec_file(f)
  180. elif args.operation == 'file':
  181. operation_file(uploader, args.cmd, args.filename)
  182. elif args.operation == 'node':
  183. if args.ncmd == 'heap':
  184. uploader.node_heap()
  185. elif args.ncmd == 'restart':
  186. uploader.node_restart()
  187. #no uploader related commands after this point
  188. uploader.close()
  189. if args.operation == 'terminal':
  190. #uploader can not claim the port
  191. uploader.close()
  192. terminal(args.port)