main.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. log = logging.getLogger(__name__)
  9. def destination_from_source(sources):
  10. destinations = []
  11. for i in range(0, len(sources)):
  12. sd = sources[i].split(':')
  13. if len(sd) == 2:
  14. destinations.append(sd[1])
  15. sources[i] = sd[0]
  16. else:
  17. destinations.append(sd[0])
  18. return destinations
  19. def operation_upload(uploader, sources, verify, do_compile, do_file, do_restart):
  20. destinations = destination_from_source(sources)
  21. if len(destinations) == len(sources):
  22. uploader.prepare()
  23. for f, d in zip(sources, destinations):
  24. if do_compile:
  25. uploader.file_remove(os.path.splitext(d)[0]+'.lc')
  26. uploader.write_file(f, d, verify)
  27. if do_compile and d != 'init.lua':
  28. uploader.file_compile(d)
  29. uploader.file_remove(d)
  30. if do_file:
  31. uploader.file_do(os.path.splitext(d)[0]+'.lc')
  32. elif do_file:
  33. uploader.file_do(d)
  34. else:
  35. raise Exception('You must specify a destination filename for each file you want to upload.')
  36. if do_restart:
  37. uploader.node_restart()
  38. log.info('All done!')
  39. def operation_download(uploader, sources):
  40. destinations = destination_from_source(sources)
  41. if len(destinations) == len(sources):
  42. for f, d in zip(sources, destinations):
  43. uploader.read_file(f, d)
  44. else:
  45. raise Exception('You must specify a destination filename for each file you want to download.')
  46. log.info('All done!')
  47. def operation_file(uploader, cmd, filename=''):
  48. if cmd == 'list':
  49. uploader.file_list()
  50. if cmd == 'do':
  51. for f in filename:
  52. uploader.file_do(f)
  53. elif cmd == 'format':
  54. uploader.file_format()
  55. elif cmd == 'remove':
  56. for f in filename:
  57. uploader.file_remove(f)
  58. def arg_auto_int(value):
  59. """parsing function for integer arguments"""
  60. return int(value, 0)
  61. def main_func():
  62. parser = argparse.ArgumentParser(
  63. description='NodeMCU Lua file uploader',
  64. prog='nodemcu-uploader'
  65. )
  66. parser.add_argument(
  67. '--verbose',
  68. help='verbose output',
  69. action='store_true',
  70. default=False)
  71. parser.add_argument(
  72. '--port', '-p',
  73. help='Serial port device',
  74. default=Uploader.PORT)
  75. parser.add_argument(
  76. '--baud', '-b',
  77. help='Serial port baudrate',
  78. type=arg_auto_int,
  79. default=Uploader.BAUD)
  80. subparsers = parser.add_subparsers(
  81. dest='operation',
  82. help='Run nodemcu-uploader {command} -h for additional help')
  83. upload_parser = subparsers.add_parser(
  84. 'upload',
  85. help='Path to one or more files to be uploaded. Destination name will be the same as the file name.')
  86. # upload_parser.add_argument(
  87. # '--filename', '-f',
  88. # help = 'File to upload. You can specify this option multiple times.',
  89. # action='append')
  90. # upload_parser.add_argument(
  91. # '--destination', '-d',
  92. # help = 'Name to be used when saving in NodeMCU. You should specify one per file.',
  93. # action='append')
  94. upload_parser.add_argument(
  95. 'filename',
  96. nargs='+',
  97. help='Lua file to upload. Use colon to give alternate destination.'
  98. )
  99. upload_parser.add_argument(
  100. '--compile', '-c',
  101. help='If file should be uploaded as compiled',
  102. action='store_true',
  103. default=False
  104. )
  105. upload_parser.add_argument(
  106. '--verify', '-v',
  107. help='To verify the uploaded data.',
  108. action='store',
  109. nargs='?',
  110. choices=['standard', 'sha1'],
  111. default='standard'
  112. )
  113. upload_parser.add_argument(
  114. '--dofile', '-e',
  115. help='If file should be run after upload.',
  116. action='store_true',
  117. default=False
  118. )
  119. upload_parser.add_argument(
  120. '--restart', '-r',
  121. help='If esp should be restarted',
  122. action='store_true',
  123. default=False
  124. )
  125. exec_parser = subparsers.add_parser(
  126. 'exec',
  127. help='Path to one or more files to be executed line by line.')
  128. exec_parser.add_argument('filename', nargs='+', help='Lua file to execute.')
  129. download_parser = subparsers.add_parser(
  130. 'download',
  131. help='Path to one or more files to be downloaded. Destination name will be the same as the file name.')
  132. download_parser.add_argument('filename', nargs='+', help='Lua file to download. Use colon to give alternate destination.')
  133. file_parser = subparsers.add_parser(
  134. 'file',
  135. help='File functions')
  136. file_parser.add_argument('cmd', choices=('list', 'do', 'format', 'remove'))
  137. file_parser.add_argument('filename', nargs='*', help='Lua file to run.')
  138. node_parse = subparsers.add_parser(
  139. 'node',
  140. help='Node functions')
  141. node_parse.add_argument('ncmd', choices=('heap', 'restart'))
  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. uploader.close()