main.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2024 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. import sys,os
  22. def launch(*, mod=None, func=None, fqmod=None, package='mmgen'):
  23. if sys.platform in ('linux', 'darwin') and sys.stdin.isatty():
  24. import termios,atexit
  25. fd = sys.stdin.fileno()
  26. old = termios.tcgetattr(fd)
  27. atexit.register(lambda: termios.tcsetattr(fd,termios.TCSADRAIN,old))
  28. try:
  29. __import__(f'{package}.main_{mod}') if mod else func() if func else __import__(fqmod)
  30. except KeyboardInterrupt:
  31. from .color import yellow
  32. sys.stderr.write(yellow('\nUser interrupt\n'))
  33. sys.exit(1)
  34. except EOFError:
  35. from .color import yellow
  36. sys.stderr.write(yellow('\nEnd of file\n'))
  37. sys.exit(1)
  38. except Exception as e:
  39. try:
  40. errmsg = str(e.args[0])
  41. except:
  42. errmsg = repr(e.args[0]) if e.args else repr(e)
  43. from collections import namedtuple
  44. from .color import nocolor,yellow,red
  45. _o = namedtuple('exit_data',['color','exit_val','fs'])
  46. d = {
  47. 0: _o(nocolor, 0, '{message}'),
  48. 1: _o(nocolor, 1, '{message}'),
  49. 2: _o(yellow, 2, '{message}'),
  50. 3: _o(yellow, 3, '\nMMGen Error ({name}):\n{message}'),
  51. 4: _o(red, 4, '\nMMGen Fatal Error ({name}):\n{message}'),
  52. 'x': _o(yellow, 5, '\nMMGen Unhandled Exception ({name}): {e}'),
  53. }[getattr(e,'mmcode','x')]
  54. (sys.stdout if getattr(e,'stdout',None) else sys.stderr).write(
  55. d.color(d.fs.format(
  56. name = type(e).__name__,
  57. message = errmsg.strip() or e,
  58. e = e))
  59. + '\n' )
  60. if os.getenv('MMGEN_EXEC_WRAPPER') or os.getenv('MMGEN_TRACEBACK'):
  61. raise
  62. sys.exit(d.exit_val)