pwman3 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 : cp ~/.pwman3/pwman.db ~/.pwman3/pwman.backup-2013-11-23-23:15.db
  39. #3) Display a message about the backup file path
  40. # These steps are done by PwmanConvertDB.backup_old_db()
  41. dbconvertor.backup_old_db()
  42. #4) convert the old database to the new format in ~/.pwman3/pwman.db
  43. # This step is done by PwmanConvertDB.create_new_db()
  44. # PwmanConvertDB.convert_nodes()
  45. # PwmanConvertDB.save_new_nodes_to_db()
  46. # PwmanConvertDB.save_old_key()
  47. dbconvertor.read_old_db()
  48. dbconvertor.create_new_db()
  49. dbconvertor.convert_nodes()
  50. dbconvertor.save_new_nodes_to_db()
  51. dbconvertor.save_old_key()
  52. #5) Display a message about the result of the conversion
  53. # add message here ...
  54. #5b) close connection
  55. dbconvertor.db.close()
  56. print("Your datbase %s was successfully converted " % dbconvertor.dbname)
  57. #5c) rename the newly created db to the old name!
  58. shutil.move(dbconvertor.newdb_name, dbconvertor.dbname)
  59. #6) Start the pwman3 normally if all went ok
  60. return True
  61. except Exception, e:
  62. raise e
  63. def main(args):
  64. PwmanCliNew, OSX = get_ui_platform(sys.platform)
  65. xselpath, dbtype = get_conf_options(args, OSX)
  66. dbver = get_db_version(config, dbtype, args)
  67. enc = CryptoEngine.get(dbver)
  68. if args.dbconvert:
  69. dbconvertor = PwmanConvertDB(args, config)
  70. dbconvertor.run()
  71. sys.exit(0)
  72. if dbver < 0.4:
  73. ans = raw_input("Would you like to proceed with the conversion of the database"
  74. " before starting pwman3 [y/N]?")
  75. if ans.lower() != "y":
  76. sys.exit(1)
  77. auto_convert()
  78. dbver = 0.4
  79. db = pwman.data.factory.create(dbtype, dbver)
  80. cli = PwmanCliNew(db, xselpath, CLICallback)
  81. try:
  82. cli.cmdloop()
  83. except KeyboardInterrupt, e:
  84. print(e)
  85. if config.get_value("Global", "save") == "True":
  86. try:
  87. config.save(args.cfile)
  88. except Exception, e:
  89. print ("Error: %s" % e)
  90. sys.exit(-1)
  91. if __name__ == '__main__':
  92. args = parser_options().parse_args()
  93. main(args)