inary-cli 3.17 KB
Newer Older
Suleyman Poyraz's avatar
Suleyman Poyraz committed
1
#!/usr/bin/env python3
2
#
3 4
# Main fork Pisi: Copyright (C) 2005 - 2011, Tubitak/UEKAE
#
Suleyman Poyraz's avatar
Suleyman Poyraz committed
5
# Copyright (C) 2016 - 2018, Suleyman POYRAZ (AquilaNipalensis)
6 7 8 9 10 11 12 13
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.
#
14

15
import errno
16 17
import traceback
import signal
sulincix's avatar
sulincix committed
18
import os
19 20
import sys
import traceback
21

22 23
if os.path.exists("/usr/lib/sulin"):
    sys.path.insert(0,"/usr/lib/sulin")
24

Suleyman Poyraz's avatar
Suleyman Poyraz committed
25
import inary
26
import inary.cli
27
import inary.context as ctx
28
import inary.db
frknkrc44's avatar
frknkrc44 committed
29
from inary.errors import Error, ParserError
30
import inary.util
31
from inary.cli import inarycli
32

33
import gettext
Suleyman Poyraz's avatar
Suleyman Poyraz committed
34 35 36 37
gettext.bindtextdomain('inary', "/usr/share/locale")
gettext.textdomain('inary')
__trans = gettext.translation('inary', fallback=True)
_ = __trans.gettext
38

39

40 41 42 43
def sig_handler(sig, frame):
    if sig == signal.SIGTERM:
        exit()

44

45
def exit():
46
    inary.util.noecho(False)
47 48
    sys.exit(1)

49

50 51 52
def handle_exception(exception, value, tb):
    signal.signal(signal.SIGINT, signal.SIG_IGN)  # disable further interrupts
    ui = inary.cli.CLI()  # make a temporary UI
53 54
    show_traceback = False

frknkrc44's avatar
frknkrc44 committed
55 56 57 58 59
    if isinstance(value, ParserError):
        show_traceback = True
        ui.error(_("\nUnhandled internal exception.\n"
                   "Please file a bug report to <https://github.com/SulinOS/inary/issues>."))
    elif isinstance(value, Error):
60
        ui.error(_("Program terminated."))
61
    elif isinstance(value, KeyboardInterrupt):
Suleyman Poyraz's avatar
Suleyman Poyraz committed
62
        ui.error(_("\nKeyboard Interrupt [Ctrl-C]: Exiting..."))
63
        exit()
Suleyman Poyraz's avatar
Suleyman Poyraz committed
64 65 66
    elif isinstance(value, EOFError):
        ui.error(_("\nKeyboard Interrupt [Ctrl-D]: Exiting..."))
        exit()
67 68 69
    elif isinstance(value, IOError) and value.errno == errno.EPIPE:
        # Ignore broken pipe errors
        sys.exit(0)
70 71 72 73
    else:
        # For any other exception (possibly Python exceptions) show
        # the traceback!
        show_traceback = ctx.get_option('debug')
Suleyman Poyraz's avatar
Suleyman Poyraz committed
74
        ui.error(_("System error. Program terminated."))
75

Fatih Aşıcı's avatar
Fatih Aşıcı committed
76
    if show_traceback:
77
        ui.error("{}: {}".format(exception, str(value)))
78
    else:
79 80 81
        msg = str(value)
        if msg:
            ui.error(msg)
82

Suleyman Poyraz's avatar
Suleyman Poyraz committed
83
    ui.info(_("Please use 'inary help' for general help."))
84 85

    if show_traceback:
Fatih Aşıcı's avatar
Fatih Aşıcı committed
86
        ui.info(_("\nTraceback:"))
87
        traceback.print_tb(tb)
88
    elif not isinstance(value, inary.errors.Error):
89
        ui.info(_("Use --debug to see a traceback."))
90 91 92

    exit()

93 94
# This is blocking WSL bash to supply stability of packages
# created by packagers.
95 96


97 98
def blc_wsl():
    """Dont allow fucking WSL"""
Your Name's avatar
Your Name committed
99
    f = str(os.uname()).lower()
100
    if (("microsoft" in f) or ("wsl" in f) or inary.util.get_cpuinfo("microcode") == "0xffffffff"):
101
        ui = inary.cli.CLI()
Suleyman Poyraz's avatar
Suleyman Poyraz committed
102
        ui.error(_("Using inary in WSL environment is not allowed."))
Your Name's avatar
Your Name committed
103
        exit(1)
104

105

106
if __name__ == "__main__":
107
    blc_wsl()
108 109 110 111
    sys.excepthook = handle_exception

    signal.signal(signal.SIGTERM, sig_handler)

112
    cli = inary.cli.inarycli.InaryCLI()
Suleyman Poyraz's avatar
Suleyman Poyraz committed
113
    if cli.command.name[1] in "rdb sf".split():
Suleyman Poyraz's avatar
Suleyman Poyraz committed
114
        ctx.filesdb = inary.db.filesdb.FilesDB()
115
    cli.run_command()