Debian Codegraph @

command-not-found

raw ◀ ▶
1 Debian patch touch this file
1 #!/usr/bin/python3
2 # (c) Zygmunt Krynicki 2005, 2006, 2007, 2008
3 # Licensed under GPL, see COPYING for the whole text
4
5 from __future__ import absolute_import, print_function
6
7
8 __version__ = "0.3"
•9 BUG_REPORT_URL = "http://www.debian.org/Bugs/Reporting"
10
11 try:
12 import sys
13 if sys.path and sys.path[0] == '/usr/lib':
14 # Avoid ImportError noise due to odd installation location.
15 sys.path.pop(0)
16 if sys.version < '3':
17 # We might end up being executed with Python 2 due to an old
18 # /etc/bash.bashrc.
19 import os
20 if "COMMAND_NOT_FOUND_FORCE_PYTHON2" not in os.environ:
21 os.execvp("/usr/bin/python3", [sys.argv[0]] + sys.argv)
22
23 import gettext
24 import locale
25 from optparse import OptionParser
26
27 from CommandNotFound.util import crash_guard
28 from CommandNotFound import CommandNotFound
29 except KeyboardInterrupt:
30 import sys
31 sys.exit(127)
32
33
34 def enable_i18n():
35 cnf = gettext.translation("command-not-found", fallback=True)
36 kwargs = {}
37 if sys.version < '3':
38 kwargs["unicode"] = True
39 cnf.install(**kwargs)
40 try:
41 locale.setlocale(locale.LC_ALL, '')
42 except locale.Error:
43 locale.setlocale(locale.LC_ALL, 'C')
44
45
46 def fix_sys_argv(encoding=None):
47 """
48 Fix sys.argv to have only unicode strings, not binary strings.
49 This is required by various places where such argument might be
50 automatically coerced to unicode string for formatting
51 """
52 if encoding is None:
53 encoding = locale.getpreferredencoding()
54 sys.argv = [arg.decode(encoding) for arg in sys.argv]
55
56
57 class LocaleOptionParser(OptionParser):
58 """
59 OptionParser is broken as its implementation of _get_encoding() uses
60 sys.getdefaultencoding() which is ascii, what it should be using is
61 locale.getpreferredencoding() which returns value based on LC_CTYPE (most
62 likely) and allows for UTF-8 encoding to be used.
63 """
64 def _get_encoding(self, file):
65 encoding = getattr(file, "encoding", None)
66 if not encoding:
67 encoding = locale.getpreferredencoding()
68 return encoding
69
70
71 def main():
72 enable_i18n()
73 if sys.version < '3':
74 fix_sys_argv()
75 parser = LocaleOptionParser(
76 version=__version__,
77 usage=_("%prog [options] <command-name>"))
78 parser.add_option('-d', '--data-dir', action='store',
79 default="/usr/share/command-not-found",
80 help=_("use this path to locate data fields"))
81 parser.add_option('--ignore-installed', '--ignore-installed',
82 action='store_true', default=False,
83 help=_("ignore local binaries and display the available packages"))
84 parser.add_option('--no-failure-msg',
85 action='store_true', default=False,
86 help=_("don't print '<command-name>: command not found'"))
87 (options, args) = parser.parse_args()
88 if len(args) == 1:
89 try:
90 cnf = CommandNotFound.CommandNotFound(options.data_dir)
91 except FileNotFoundError:
92 print(_("Could not find command-not-found database. Run 'sudo apt update' to populate it."), file=sys.stderr)
93 print(_("%s: command not found") % args[0], file=sys.stderr)
94 return
95 if not cnf.advise(args[0], options.ignore_installed) and not options.no_failure_msg:
96 print(_("%s: command not found") % args[0], file=sys.stderr)
97
98
99 if __name__ == "__main__":
100 crash_guard(main, BUG_REPORT_URL, __version__)