main.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2018 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. main.py - Script launcher for the MMGen suite
  20. """
  21. def launch(what):
  22. def my_dec(a):
  23. try:
  24. return a.decode('utf8')
  25. except:
  26. sys.stderr.write("Argument {!r} is not a valid UTF-8 string".format(a))
  27. sys.exit(2)
  28. import sys
  29. sys.argv = map(my_dec,sys.argv)
  30. if what in ('walletgen','walletchk','walletconv','passchg'):
  31. what = 'wallet'
  32. if what == 'keygen': what = 'addrgen'
  33. try: import termios
  34. except: # Windows
  35. __import__('mmgen.main_' + what)
  36. else:
  37. import os,atexit
  38. if sys.stdin.isatty():
  39. fd = sys.stdin.fileno()
  40. old = termios.tcgetattr(fd)
  41. def at_exit(): termios.tcsetattr(fd, termios.TCSADRAIN, old)
  42. atexit.register(at_exit)
  43. try: __import__('mmgen.main_' + what)
  44. except KeyboardInterrupt:
  45. sys.stderr.write('\nUser interrupt\n')
  46. except EOFError:
  47. sys.stderr.write('\nEnd of file\n')
  48. except Exception as e:
  49. if os.getenv('MMGEN_TRACEBACK'):
  50. raise
  51. else:
  52. try: m = u'{}'.format(e.message)
  53. except:
  54. try: m = e.message.decode('utf8')
  55. except: m = repr(e.message)
  56. from mmgen.util import die,ydie
  57. if type(e).__name__ == 'UserNonConfirmation': die(1,m)
  58. if type(e).__name__ == 'RPCFailure': ydie(2,m)
  59. ydie(2,u'\nERROR: ' + m)