gw.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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
  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. 'seek',
  42. 'delays',
  43. 'update',
  44. 'pin',
  45. 'reset',
  46. 'bandwidth' ]
  47. argv = sys.argv
  48. backtrace = False
  49. if len(argv) > 1 and argv[1] == '--bt':
  50. backtrace = True
  51. argv = [argv[0]] + argv[2:]
  52. if len(argv) < 2 or argv[1] not in actions:
  53. print("Usage: %s [action] [-h] ..." % (argv[0]))
  54. print(" -h, --help Show help message for specified action")
  55. print("Actions:")
  56. for a in actions:
  57. mod = importlib.import_module('greaseweazle.tools.' + a)
  58. print(' %-12s%s' % (a, mod.__dict__['description']))
  59. sys.exit(1)
  60. mod = importlib.import_module('greaseweazle.tools.' + argv[1])
  61. main = mod.__dict__['main']
  62. try:
  63. res = main(argv)
  64. if res is None:
  65. res = 0
  66. except (IndexError, AssertionError, TypeError, KeyError):
  67. raise
  68. except KeyboardInterrupt:
  69. if backtrace: raise
  70. res = 1
  71. except Exception as err:
  72. if backtrace: raise
  73. print("** FATAL ERROR:")
  74. print(err)
  75. res = 1
  76. sys.exit(res)
  77. # Local variables:
  78. # python-indent: 4
  79. # End: