gw.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. missing_modules = []
  13. try:
  14. import bitarray
  15. except ImportError:
  16. missing_modules.append("bitarray")
  17. try:
  18. import crcmod
  19. except ImportError:
  20. missing_modules.append("crcmod")
  21. try:
  22. import serial.tools.list_ports
  23. except ImportError:
  24. missing_modules.append("pyserial")
  25. if missing_modules:
  26. print("""\
  27. ** Missing Python modules: %s
  28. For installation instructions please read the wiki:
  29. <https://github.com/keirf/Greaseweazle/wiki/Software-Installation>"""
  30. % ', '.join(missing_modules))
  31. sys.exit(1)
  32. actions = [ 'info',
  33. 'read',
  34. 'write',
  35. 'erase',
  36. 'seek',
  37. 'delays',
  38. 'update',
  39. 'pin',
  40. 'reset',
  41. 'bandwidth' ]
  42. argv = sys.argv
  43. backtrace = False
  44. if len(argv) > 1 and argv[1] == '--bt':
  45. backtrace = True
  46. argv = [argv[0]] + argv[2:]
  47. if len(argv) < 2 or argv[1] not in actions:
  48. print("Usage: %s [action] [-h] ..." % (argv[0]))
  49. print(" -h, --help Show help message for specified action")
  50. print("Actions:")
  51. for a in actions:
  52. mod = importlib.import_module('greaseweazle.tools.' + a)
  53. print(' %-12s%s' % (a, mod.__dict__['description']))
  54. sys.exit(1)
  55. mod = importlib.import_module('greaseweazle.tools.' + argv[1])
  56. main = mod.__dict__['main']
  57. try:
  58. res = main(argv)
  59. if res is None:
  60. res = 0
  61. except (IndexError, AssertionError, TypeError, KeyError):
  62. raise
  63. except KeyboardInterrupt:
  64. if backtrace: raise
  65. res = 1
  66. except Exception as err:
  67. if backtrace: raise
  68. print("** FATAL ERROR:")
  69. print(err)
  70. res = 1
  71. sys.exit(res)
  72. # Local variables:
  73. # python-indent: 4
  74. # End: