main.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2022 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: Script launcher for the MMGen project
  20. """
  21. def launch(mod,package='mmgen'):
  22. if mod in ('walletgen','walletchk','walletconv','passchg','subwalletgen','seedsplit'):
  23. mod = 'wallet'
  24. if mod == 'keygen':
  25. mod = 'addrgen'
  26. import sys,os
  27. from .globalvars import g
  28. if g.platform == 'linux' and sys.stdin.isatty():
  29. import termios,atexit
  30. fd = sys.stdin.fileno()
  31. old = termios.tcgetattr(fd)
  32. atexit.register(lambda: termios.tcsetattr(fd,termios.TCSADRAIN,old))
  33. try:
  34. __import__(f'{package}.main_{mod}')
  35. except KeyboardInterrupt:
  36. sys.stderr.write('\nUser interrupt\n')
  37. sys.exit(1) # must exit normally so exit handlers will be called
  38. except EOFError:
  39. sys.stderr.write('\nEnd of file\n')
  40. except Exception as e:
  41. if os.getenv('MMGEN_EXEC_WRAPPER'):
  42. raise
  43. else:
  44. try:
  45. errmsg = '{}'.format(e.args[0])
  46. except:
  47. errmsg = repr(e.args[0])
  48. from collections import namedtuple
  49. from mmgen.color import nocolor,yellow,red
  50. _o = namedtuple('exit_data',['color','exit_val','fs'])
  51. d = {
  52. 0: _o(nocolor, 0, '{message}'),
  53. 1: _o(nocolor, 1, '{message}'),
  54. 2: _o(yellow, 2, '{message}'),
  55. 3: _o(yellow, 3, '\nMMGen Error ({name}):\n{message}'),
  56. 4: _o(red, 4, '\nMMGen Fatal Error ({name}):\n{message}'),
  57. 'x': _o(yellow, 5, '\nMMGen Unhandled Exception ({name}):\n{message}'),
  58. }[getattr(e,'mmcode','x')]
  59. (sys.stdout if getattr(e,'stdout',None) else sys.stderr).write(
  60. d.color(d.fs.format(
  61. name = type(e).__name__,
  62. message = errmsg ))
  63. + '\n' )
  64. sys.exit(d.exit_val)
  65. except SystemExit as e:
  66. if os.getenv('MMGEN_EXEC_WRAPPER') and e.code != 0:
  67. from mmgen.color import red
  68. sys.stdout.write(red(f'{type(e).__name__}: {e}\n'))
  69. raise