command-not-found 3.5 KB

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