gw.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. if len(argv) < 2 or argv[1] not in actions:
  43. print("Usage: %s [action] [-h] ..." % (argv[0]))
  44. print(" -h, --help Show help message for specified action")
  45. print("Actions:")
  46. for a in actions:
  47. mod = importlib.import_module('greaseweazle.tools.' + a)
  48. print(' %-12s%s' % (a, mod.__dict__['description']))
  49. sys.exit(1)
  50. mod = importlib.import_module('greaseweazle.tools.' + argv[1])
  51. main = mod.__dict__['main']
  52. try:
  53. res = main(argv)
  54. if res is None:
  55. res = 0
  56. except (IndexError, AssertionError):
  57. raise
  58. except Exception as err:
  59. print("** FATAL ERROR:")
  60. print(err)
  61. res = 1
  62. sys.exit(res)
  63. # Local variables:
  64. # python-indent: 4
  65. # End: