gw.py 2.2 KB

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