pwman3 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python
  2. #============================================================================
  3. # This file is part of Pwman3.
  4. #
  5. # Pwman3 is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License, version 2
  7. # as published by the Free Software Foundation;
  8. #
  9. # Pwman3 is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Pwman3; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. #============================================================================
  18. # Copyright (C) 2012-2014 Oz Nahum <nahumoz@gmail.com>
  19. #============================================================================
  20. # Copyright (C) 2006 Ivan Kelly <ivan@ivankelly.net>
  21. #============================================================================
  22. from __future__ import print_function
  23. import sys
  24. import shutil
  25. from pwman import get_conf_options, get_db_version
  26. from pwman import parser_options
  27. from pwman.ui import get_ui_platform
  28. from pwman.ui.tools import CLICallback
  29. import pwman.util.config as config
  30. import pwman.data.factory
  31. from pwman.data.convertdb import PwmanConvertDB
  32. from pwman.util.crypto import CryptoEngine
  33. def auto_convert():
  34. try:
  35. #1) Display a message saying that the database will be converted
  36. # This step is done already in get_db_version
  37. dbconvertor = PwmanConvertDB(args, config)
  38. #2) copy the old database :
  39. # cp ~/.pwman3/pwman.db ~/.pwman3/pwman.backup-2013-11-23-23:15.db
  40. #3) Display a message about the backup file path
  41. # These steps are done by PwmanConvertDB.backup_old_db()
  42. dbconvertor.backup_old_db()
  43. #4) convert the old database to the new format in ~/.pwman3/pwman.db
  44. # This step is done by PwmanConvertDB.create_new_db()
  45. # PwmanConvertDB.convert_nodes()
  46. # PwmanConvertDB.save_new_nodes_to_db()
  47. # PwmanConvertDB.save_old_key()
  48. dbconvertor.read_old_db()
  49. dbconvertor.create_new_db()
  50. dbconvertor.convert_nodes()
  51. dbconvertor.save_new_nodes_to_db()
  52. dbconvertor.save_old_key()
  53. #5) Display a message about the result of the conversion
  54. # add message here ...
  55. #5b) close connection
  56. dbconvertor.db.close()
  57. print("Your datbase %s was successfully converted " %
  58. dbconvertor.dbname)
  59. #5c) rename the newly created db to the old name!
  60. shutil.move(dbconvertor.newdb_name, dbconvertor.dbname)
  61. #6) Start the pwman3 normally if all went ok
  62. return True
  63. except Exception as e:
  64. raise e
  65. def main(args):
  66. PwmanCliNew, OSX = get_ui_platform(sys.platform)
  67. xselpath, dbtype = get_conf_options(args, OSX)
  68. dbver = get_db_version(config, dbtype, args)
  69. CryptoEngine.get(dbver)
  70. if args.dbconvert:
  71. dbconvertor = PwmanConvertDB(args, config)
  72. dbconvertor.run()
  73. sys.exit(0)
  74. if dbver < 0.4:
  75. ans = raw_input("Would you like to proceed with the conversion of the "
  76. "database before starting pwman3 [y/N]?")
  77. if ans.lower() != "y":
  78. sys.exit(1)
  79. auto_convert()
  80. dbver = 0.4
  81. db = pwman.data.factory.create(dbtype, dbver)
  82. cli = PwmanCliNew(db, xselpath, CLICallback)
  83. try:
  84. cli.cmdloop()
  85. except KeyboardInterrupt as e:
  86. print(e)
  87. if config.get_value("Global", "save") == "True":
  88. try:
  89. config.save(args.cfile)
  90. except Exception as e:
  91. print ("Error: %s" % e)
  92. sys.exit(-1)
  93. if __name__ == '__main__':
  94. args = parser_options().parse_args()
  95. main(args)