cli.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # ============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. # ============================================================================
  17. # Copyright (C) 2012-2018 Oz N Tiram <oz.tiram@gmail.com>
  18. # ============================================================================
  19. # pylint: disable=I0011
  20. import cmd
  21. import sys
  22. try:
  23. import readline
  24. _readline_available = True
  25. except ImportError as e: # pragma: no cover
  26. import pyreadline as readline
  27. _readline_available = True
  28. from pwman.ui.baseui import BaseCommands
  29. from pwman import (get_conf_options, get_db_version, version, website,
  30. parser_options, has_cryptography, calculate_client_info,
  31. is_latest_version)
  32. from pwman.ui.tools import CLICallback
  33. from pwman.data import factory
  34. from pwman.exchange.importer import Importer
  35. from pwman.util.crypto_engine import CryptoEngine
  36. class PwmanCli(cmd.Cmd, BaseCommands):
  37. """
  38. Inherit from the BaseCommands and Aliases
  39. """
  40. undoc_header = "Aliases:"
  41. def __init__(self, db, hasxsel, callback, config_parser, **kwargs):
  42. """
  43. initialize CLI interface, set up the DB
  44. connecion, see if we have xsel ...
  45. """
  46. super(PwmanCli, self).__init__(**kwargs)
  47. self.intro = "%s %s (c) visit: %s" % ('pwman3', version, website)
  48. self._historyfile = config_parser.get_value("Readline", "history")
  49. self.hasxsel = hasxsel
  50. self.config = config_parser
  51. try:
  52. enc = CryptoEngine.get()
  53. enc.callback = callback()
  54. self._db = db
  55. # this cascades down all the way to setting the database key
  56. self._db.open()
  57. except Exception as e: # pragma: no cover
  58. self.error(e)
  59. sys.exit(1)
  60. try:
  61. readline.read_history_file(self._historyfile)
  62. except IOError as e: # pragma: no cover
  63. pass
  64. self.prompt = "pwman> "
  65. def get_ui_platform(platform): # pragma: no cover
  66. if 'darwin' in platform:
  67. from pwman.ui.mac import PwmanCliMac as PwmanCli
  68. OSX = True
  69. elif 'win' in platform:
  70. from pwman.ui.win import PwmanCliWin as PwmanCli
  71. OSX = False
  72. else:
  73. from pwman.ui.cli import PwmanCli
  74. OSX = False
  75. return PwmanCli, OSX
  76. def check_version(version, client_info):
  77. _, latest = is_latest_version(version, client_info)
  78. if not latest:
  79. print("A newer version of Pwman3 was released, you should consider updating") # noqa
  80. return latest
  81. def main():
  82. args = parser_options().parse_args()
  83. PwmanCli, OSX = get_ui_platform(sys.platform)
  84. xselpath, dbtype, config = get_conf_options(args, OSX)
  85. dburi = config.get_value('Database', 'dburi')
  86. client_info = config.get_value('Updater', 'client_info')
  87. if not client_info:
  88. client_info = calculate_client_info()
  89. config.set_value('Updater', 'client_info', client_info)
  90. if not has_cryptography:
  91. import colorama
  92. if config.get_value('Crypto', 'supress_warning').lower() != 'yes':
  93. print("{}WARNING: You are not using PyCrypto!!!\n"
  94. "WARNING: You should install PyCrypto for better security and " # noqa
  95. "perfomance\nWARNING: You can supress this warning by editing " # noqa
  96. "pwman config file.{}".format(colorama.Fore.RED,
  97. colorama.Style.RESET_ALL))
  98. if args.cmd == "version":
  99. latest = check_version(version, client_info)
  100. print("version: %s is latest: %s" % (version, latest))
  101. sys.exit(0)
  102. elif config.get_value('Updater',
  103. 'supress_version_check').lower() != 'yes':
  104. check_version(version, client_info)
  105. print(dburi)
  106. dbver = get_db_version(config, args)
  107. timeout = int(config.get_value('Global', 'lock_timeout'))
  108. CryptoEngine.get(timeout)
  109. db = factory.createdb(dburi, dbver)
  110. if args.file_delim:
  111. importer = Importer((args, config, db))
  112. importer.run()
  113. sys.exit(0)
  114. cli = PwmanCli(db, xselpath, CLICallback, config)
  115. if args.cmd == "p":
  116. cli.onecmd("pp %s" % args.node)
  117. sys.exit(0)
  118. if args.cmd == "cp":
  119. cli.onecmd("cp %s" % args.node)
  120. sys.exit(0)
  121. try:
  122. cli.cmdloop()
  123. except KeyboardInterrupt as e:
  124. print(e)
  125. finally:
  126. config.save()