gw.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. # gw.py
  3. #
  4. # Greaseweazle control script.
  5. #
  6. # Written & released by Keir Fraser <keir.xen@gmail.com>
  7. #
  8. # This is free and unencumbered software released into the public domain.
  9. # See the file COPYING for more details, or visit <http://unlicense.org>.
  10. import sys, time
  11. import importlib
  12. from greaseweazle import version
  13. if hasattr(version, 'commit'):
  14. print("""*** TEST/PRE-RELEASE: commit %s
  15. *** Use these tools and firmware ONLY for test and development!!"""
  16. % version.commit)
  17. missing_modules = []
  18. try:
  19. import bitarray
  20. except ImportError:
  21. missing_modules.append("bitarray")
  22. try:
  23. import crcmod
  24. except ImportError:
  25. missing_modules.append("crcmod")
  26. try:
  27. import serial.tools.list_ports
  28. except ImportError:
  29. missing_modules.append("pyserial")
  30. if missing_modules:
  31. print("""\
  32. ** Missing Python modules: %s
  33. For installation instructions please read the wiki:
  34. <https://github.com/keirf/Greaseweazle/wiki/Software-Installation>"""
  35. % ', '.join(missing_modules))
  36. sys.exit(1)
  37. actions = [ 'info',
  38. 'read',
  39. 'write',
  40. 'erase',
  41. 'clean',
  42. 'seek',
  43. 'delays',
  44. 'update',
  45. 'pin',
  46. 'reset',
  47. 'bandwidth' ]
  48. argv = sys.argv
  49. def usage():
  50. print("Usage: %s [--time] [action] [-h] ..." % (argv[0]))
  51. print(" --time Print elapsed time after action is executed")
  52. print(" -h, --help Show help message for specified action")
  53. print("Actions:")
  54. for a in actions:
  55. mod = importlib.import_module('greaseweazle.tools.' + a)
  56. print(' %-12s%s' % (a, mod.__dict__['description']))
  57. sys.exit(1)
  58. backtrace = False
  59. start_time = None
  60. while len(argv) > 1 and argv[1].startswith('--'):
  61. if argv[1] == '--bt':
  62. backtrace = True
  63. elif argv[1] == '--time':
  64. start_time = time.time()
  65. else:
  66. usage()
  67. argv = [argv[0]] + argv[2:]
  68. if len(argv) < 2 or argv[1] not in actions:
  69. usage()
  70. mod = importlib.import_module('greaseweazle.tools.' + argv[1])
  71. main = mod.__dict__['main']
  72. try:
  73. res = main(argv)
  74. if res is None:
  75. res = 0
  76. except (IndexError, AssertionError, TypeError, KeyError):
  77. raise
  78. except KeyboardInterrupt:
  79. if backtrace: raise
  80. res = 1
  81. except Exception as err:
  82. if backtrace: raise
  83. print("** FATAL ERROR:")
  84. print(err)
  85. res = 1
  86. if start_time is not None:
  87. elapsed = time.time() - start_time
  88. print("Time elapsed: %.2f seconds" % elapsed)
  89. sys.exit(res)
  90. # Local variables:
  91. # python-indent: 4
  92. # End: