convertdb.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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) 2013 Oz Nahum <nahumoz@gmail.com>
  18. #============================================================================
  19. import os
  20. import shutil
  21. import os.path
  22. import time
  23. import getpass
  24. from pwman.util.crypto import CryptoEngine
  25. import pwman.data.factory
  26. from pwman.util.callback import Callback
  27. from pwman.data.nodes import NewNode
  28. _NEWVERSION = 0.4
  29. class CLICallback(Callback):
  30. def getinput(self, question):
  31. return raw_input(question)
  32. def getsecret(self, question):
  33. return getpass.getpass(question + ":")
  34. class PwmanConvertDB(object):
  35. """
  36. Class to migrate from DB in version 0.3 to
  37. DB used in later versions.
  38. """
  39. def __init__(self, args, config):
  40. self.dbname = config.get_value('Database', 'filename')
  41. self.dbtype = config.get_value("Database", "type")
  42. print "Will convert the following Database: %s " % self.dbname
  43. if os.path.exists(config.get_value("Database", "filename")):
  44. dbver = pwman.data.factory.check_db_version(self.dbtype)
  45. self.dbver = float(dbver.strip("\'"))
  46. backup = '.backup-%s'.join(os.path.splitext(self.dbname)) % \
  47. time.strftime(
  48. '%Y-%m-%d-%H:%M')
  49. shutil.copy(self.dbname, backup)
  50. print "backup created in ", backup
  51. def read_old_db(self):
  52. "read the old db and get all nodes"
  53. self.db = pwman.data.factory.create(self.dbtype, self.dbver)
  54. enc = CryptoEngine.get()
  55. enc.set_callback(CLICallback())
  56. self.db.open()
  57. self.oldnodes = self.db.listnodes()
  58. self.oldnodes = self.db.getnodes(self.oldnodes)
  59. def create_new_db(self):
  60. dest = '-newdb'.join(os.path.splitext(self.dbname))
  61. if os.path.exists('-newdb'.join(os.path.splitext(self.dbname))):
  62. raise Exception("%s already exists, please move this file!" % dest)
  63. self.newdb_name = '-newdb'.join(os.path.splitext(self.dbname))
  64. self.newdb = pwman.data.factory.create(self.dbtype, _NEWVERSION,
  65. self.newdb_name)
  66. self.newdb._open()
  67. def convert_nodes(self):
  68. """convert old nodes instances to new format"""
  69. self.NewNodes = []
  70. for node in self.oldnodes:
  71. username = node.get_username()
  72. password = node.get_password()
  73. url = node.get_url()
  74. notes = node.get_notes()
  75. tags = node.get_tags()
  76. tags_strings = [tag.get_name() for tag in tags]
  77. newNode = NewNode(username=username,
  78. password=password,
  79. url=url,
  80. notes=notes,
  81. tags=tags_strings
  82. )
  83. self.NewNodes.append(newNode)
  84. def save_new_nodes_to_db(self):
  85. self.newdb.addnodes(self.NewNodes)
  86. self.newdb._commit()
  87. def save_old_key(self):
  88. enc = CryptoEngine.get()
  89. self.oldkey = enc.get_cryptedkey()
  90. self.newdb.savekey(self.oldkey)
  91. def print_success(self):
  92. print """pwman successfully converted the old database to the new
  93. format.\nPlease run `pwman3 -d %s` to make sure your password and
  94. data are still correct. If you are convinced that no harm was done,
  95. update your config file to indicate the permanent location
  96. to your new database.
  97. If you found errors, please report a bug in Pwman homepage in github.
  98. """ % self.newdb_name
  99. def run(self):
  100. self.read_old_db()
  101. self.create_new_db()
  102. self.convert_nodes()
  103. self.save_new_nodes_to_db()
  104. self.save_old_key()
  105. self.print_success()