gw.py 1.7 KB

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