inarysh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env python3
  2. #
  3. # Main fork Pisi: Copyright (C) 2005 - 2011, Tubitak/UEKAE
  4. #
  5. # Copyright (C) 2018, Suleyman POYRAZ (Zaryob)
  6. #
  7. # This program is free software; you can redistribute it and/or modify it under the terms of the GNU General
  8. # Public License as published by the Free Software Foundation; either version 2 of the License, or (at your
  9. # option) any later version.
  10. #
  11. # Please read the COPYING file.
  12. #
  13. # Authors: Eray, Baris
  14. import sys
  15. import locale
  16. import traceback
  17. import signal
  18. import os
  19. if os.path.exists("/usr/lib/sulin"):
  20. sys.path.insert(0,"/usr/lib/sulin")
  21. # libreadline interface
  22. from ctypes import CDLL, c_char_p
  23. from ctypes.util import find_library
  24. libreadline = CDLL(find_library("readline") or "libreadline.so")
  25. libreadline.readline.restype = c_char_p
  26. libreadline.readline.argtypes=[c_char_p]
  27. import inary.ui
  28. import inary.context as ctx
  29. from inary.cli.inarycli import InaryCLI
  30. import inary.util
  31. import gettext
  32. gettext.bindtextdomain('inary', "/usr/share/locale")
  33. gettext.textdomain('inary')
  34. __trans = gettext.translation('inary', fallback=True)
  35. _ = __trans.gettext
  36. try:
  37. import ciksemel
  38. except Exception:
  39. #FIXME: Gorunusu guzel olsa bile kodda anlamsizlik yaratiyor
  40. warn = inary.util.colorize(_("WARNING:\n"),"blinkingred")+ \
  41. inary.util.colorize(_("\tCiksemel XML Parser not found!!!\n"
  42. "\tMinidom slower but\n"
  43. "\tFalling back with minidom!!! :(\n\n"), 'faintwhite')
  44. sys.stdout.write(warn)
  45. def exit(retval = 0):
  46. sys.exit(retval)
  47. def sig_handler(sig, frame):
  48. if sig == signal.SIGTERM:
  49. exit()
  50. def handle_exception(exception, value, tb):
  51. signal.signal(signal.SIGINT, signal.SIG_IGN) # disable further interrupts
  52. ui = inary.cli.CLI() # make a temporary UI
  53. show_traceback = False
  54. if isinstance(value, inary.errors.Error):
  55. ui.error(_("\nProgram terminated."))
  56. elif isinstance(value, KeyboardInterrupt):
  57. ui.error(_("\nKeyboard Interrupt: Exiting..."))
  58. exit()
  59. elif isinstance(value, inary.errors.Exception):
  60. show_traceback = True
  61. ui.error(_("\nUnhandled internal exception.\n"
  62. "Please file a bug report to <http://bugs.sulin.org>."))
  63. elif isinstance(value, IOError) and value.errno == errno.EPIPE:
  64. # Ignore broken pipe errors
  65. sys.exit(0)
  66. else:
  67. # For any other exception (possibly Python exceptions) show
  68. # the traceback!
  69. show_traceback = ctx.get_option('debug')
  70. ui.error(_("\nSystem error. Program terminated."))
  71. if show_traceback:
  72. ui.error("{}: {}".format(exception, str(value)))
  73. else:
  74. msg = str(value)
  75. if msg:
  76. ui.error(msg)
  77. ui.info(_("Please use 'inary help' for general help."))
  78. if show_traceback:
  79. ui.info(_("\nTraceback:"))
  80. traceback.print_tb(tb)
  81. elif not isinstance(value, inary.errors.Error):
  82. ui.info(_("Use --debug to see a traceback."))
  83. exit()
  84. if __name__ == "__main__":
  85. locale.setlocale(locale.LC_ALL, '')
  86. sys.excepthook = handle_exception
  87. signal.signal(signal.SIGTERM, sig_handler)
  88. script = None
  89. if len(sys.argv) > 1:
  90. script = open(sys.argv[1],"r")
  91. ui = inary.cli.CLI() # make a temporary UI
  92. ui.info(_("Welcome to the interactive INARY shell.\n"
  93. "Type 'help' to see a list of commands.\n"
  94. "To end the session, type 'exit'.\n"
  95. "You can run system commands by prefixing with '!' as in '!ls'.\n\n"
  96. " Copyright 2005-2011, Tubitak/UEKAE\n"
  97. " Copyright 2018 (c) Zaryob and Sulin Community.\n\n"))
  98. while 1:
  99. sys.excepthook = handle_exception
  100. try:
  101. if script == None:
  102. cmd = libreadline.readline(inary.util.colorize('inary >> ',color="green").encode("utf-8"))
  103. if cmd == None:
  104. ui.error(_("\nKeyboard Interrupt [Ctrl-D]: Exiting..."))
  105. break
  106. cmd = cmd.decode("utf-8")
  107. else:
  108. cmd = script.readline().replace("\n","")
  109. if script.tell() == os.fstat(script.fileno()).st_size:
  110. exit(0)
  111. if cmd.strip()=='exit':
  112. ui.info(_('Bye!'))
  113. break
  114. elif cmd.startswith('!'):
  115. cmd = cmd[1:]
  116. if "cd" == cmd.split()[0]:
  117. os.chdir(cmd.replace("cd ",""))
  118. elif cmd.startswith('!'):
  119. os.system(os.environ["SHELL"])
  120. else:
  121. os.system(cmd)
  122. elif cmd.startswith('#') or len(cmd) == 0:
  123. continue
  124. else:
  125. cli = InaryCLI(cmd.split())
  126. cli.run_command()
  127. except Exception as e:
  128. ui.error(str(e))
  129. if script:
  130. exit(1)