gw.py 1.9 KB

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