main.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. 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('You must specify a destination filename for each file you want to upload.')
  46. if do_restart:
  47. uploader.node_restart()
  48. log.info('All done!')
  49. def operation_download(uploader, sources):
  50. """The download operation"""
  51. destinations = destination_from_source(sources)
  52. if len(destinations) == len(sources):
  53. for f, d in zip(sources, destinations):
  54. uploader.read_file(f, d)
  55. else:
  56. raise Exception('You must specify a destination filename for each file you want to download.')
  57. log.info('All done!')
  58. def operation_file(uploader, cmd, filename=''):
  59. """File operations"""
  60. if cmd == 'list':
  61. uploader.file_list()
  62. if cmd == 'do':
  63. for f in filename:
  64. uploader.file_do(f)
  65. elif cmd == 'format':
  66. uploader.file_format()
  67. elif cmd == 'remove':
  68. for f in filename:
  69. uploader.file_remove(f)
  70. def arg_auto_int(value):
  71. """parsing function for integer arguments"""
  72. return int(value, 0)
  73. def main_func():
  74. parser = argparse.ArgumentParser(
  75. description='NodeMCU Lua file uploader',
  76. prog='nodemcu-uploader'
  77. )
  78. parser.add_argument(
  79. '--verbose',
  80. help='verbose output',
  81. action='store_true',
  82. default=False)
  83. parser.add_argument(
  84. '--version',
  85. help='prints the version and exists',
  86. action='version',
  87. version='%(prog)s {version} (serial {serialversion})'.format(version=__version__, serialversion=serialversion)
  88. )
  89. parser.add_argument(
  90. '--port', '-p',
  91. help='Serial port device',
  92. default=Uploader.PORT)
  93. parser.add_argument(
  94. '--baud', '-b',
  95. help='Serial port baudrate',
  96. type=arg_auto_int,
  97. default=Uploader.BAUD)
  98. subparsers = parser.add_subparsers(
  99. dest='operation',
  100. help='Run nodemcu-uploader {command} -h for additional help')
  101. upload_parser = subparsers.add_parser(
  102. 'upload',
  103. help='Path to one or more files to be uploaded. Destination name will be the same as the file name.')
  104. upload_parser.add_argument(
  105. 'filename',
  106. nargs='+',
  107. help='Lua file to upload. Use colon to give alternate destination.'
  108. )
  109. upload_parser.add_argument(
  110. '--compile', '-c',
  111. help='If file should be uploaded as compiled',
  112. action='store_true',
  113. default=False
  114. )
  115. upload_parser.add_argument(
  116. '--verify', '-v',
  117. help='To verify the uploaded data.',
  118. action='store',
  119. nargs='?',
  120. choices=['standard', 'sha1'],
  121. default='standard'
  122. )
  123. upload_parser.add_argument(
  124. '--dofile', '-e',
  125. help='If file should be run after upload.',
  126. action='store_true',
  127. default=False
  128. )
  129. upload_parser.add_argument(
  130. '--restart', '-r',
  131. help='If esp should be restarted',
  132. action='store_true',
  133. default=False
  134. )
  135. exec_parser = subparsers.add_parser(
  136. 'exec',
  137. help='Path to one or more files to be executed line by line.')
  138. exec_parser.add_argument('filename', nargs='+', help='Lua file to execute.')
  139. download_parser = subparsers.add_parser(
  140. 'download',
  141. help='Path to one or more files to be downloaded. Destination name will be the same as the file name.')
  142. download_parser.add_argument('filename', nargs='+', help='Lua file to download. Use colon to give alternate destination.')
  143. file_parser = subparsers.add_parser(
  144. 'file',
  145. help='File functions')
  146. file_parser.add_argument(
  147. 'cmd',
  148. choices=('list', 'do', 'format', 'remove'),
  149. help="list=list files, do=dofile given path, format=formate file area, remove=remove given path")
  150. file_parser.add_argument('filename', nargs='*', help='path for cmd')
  151. node_parse = subparsers.add_parser(
  152. 'node',
  153. help='Node functions')
  154. node_parse.add_argument('ncmd', choices=('heap', 'restart'), help="heap=print heap memory, restart=restart nodemcu")
  155. terminal_parser = subparsers.add_parser(
  156. 'terminal',
  157. help='Run pySerials miniterm'
  158. )
  159. args = parser.parse_args()
  160. default_level = logging.INFO
  161. if args.verbose:
  162. default_level = logging.DEBUG
  163. #formatter = logging.Formatter('%(message)s')
  164. logging.basicConfig(level=default_level, format='%(message)s')
  165. uploader = Uploader(args.port, args.baud)
  166. if args.operation == 'upload':
  167. operation_upload(uploader, args.filename, args.verify, args.compile, args.dofile,
  168. args.restart)
  169. elif args.operation == 'download':
  170. operation_download(uploader, args.filename)
  171. elif args.operation == 'exec':
  172. sources = args.filename
  173. for f in sources:
  174. uploader.exec_file(f)
  175. elif args.operation == 'file':
  176. operation_file(uploader, args.cmd, args.filename)
  177. elif args.operation == 'node':
  178. if args.ncmd == 'heap':
  179. uploader.node_heap()
  180. elif args.ncmd == 'restart':
  181. uploader.node_restart()
  182. #no uploader related commands after this point
  183. uploader.close()
  184. if args.operation == 'terminal':
  185. #uploader can not claim the port
  186. uploader.close()
  187. terminal(args.port)