pwman3 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 os
  24. import sys
  25. import shutil
  26. from pwman import get_conf_options, get_db_version
  27. from pwman import parser_options
  28. from pwman.ui import get_ui_platform
  29. from pwman.ui.tools import CLICallback
  30. import pwman.util.config as config
  31. import pwman.data.factory
  32. from pwman.data.convertdb import PwmanConvertDB
  33. from pwman.util.crypto import CryptoEngine
  34. def auto_convert():
  35. try:
  36. #1) Display a message saying that the database will be converted
  37. # This step is done already in get_db_version
  38. dbconvertor = PwmanConvertDB(args, config)
  39. #2) copy the old database : 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 " % dbconvertor.dbname)
  58. #5c) rename the newly created db to the old name!
  59. shutil.move(dbconvertor.newdb_name, dbconvertor.dbname)
  60. #6) Start the pwman3 normally if all went ok
  61. return True
  62. except Exception, e:
  63. raise e
  64. def main(args):
  65. PwmanCliNew, OSX = get_ui_platform(sys.platform)
  66. xselpath, dbtype = get_conf_options(args, OSX)
  67. enc = CryptoEngine.get()
  68. dbver = get_db_version(config, dbtype, args)
  69. if args.dbconvert:
  70. dbconvertor = PwmanConvertDB(args, config)
  71. dbconvertor.run()
  72. sys.exit(0)
  73. if dbver < 0.4:
  74. ans = raw_input("Would you like to proceed with the conversion of the database"
  75. " before starting pwman3 [y/N]?")
  76. if ans.lower() != "y":
  77. sys.exit(1)
  78. auto_convert()
  79. dbver = 0.4
  80. db = pwman.data.factory.create(dbtype, dbver)
  81. cli = PwmanCliNew(db, xselpath, CLICallback)
  82. try:
  83. cli.cmdloop()
  84. except KeyboardInterrupt, e:
  85. print(e)
  86. if config.get_value("Global", "save") == "True":
  87. try:
  88. config.save(args.cfile)
  89. except Exception, e:
  90. print ("Error: %s" % e)
  91. sys.exit(-1)
  92. if __name__ == '__main__':
  93. args = parser_options().parse_args()
  94. main(args)