convertdb.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 Node
  28. from pwman.data.nodes import NewNode
  29. _NEWVERSION = 0.4
  30. class CLICallback(Callback):
  31. def getinput(self, question):
  32. return raw_input(question)
  33. def getsecret(self, question):
  34. return getpass.getpass(question + ":")
  35. class PwmanConvertDB(object):
  36. """
  37. Class to migrate from DB in version 0.3 to
  38. DB used in later versions.
  39. """
  40. def __init__(self, args, config):
  41. self.dbname = config.get_value('Database', 'filename')
  42. self.dbtype = config.get_value("Database", "type")
  43. print "Will convert the following Database: %s " % self.dbname
  44. if os.path.exists(config.get_value("Database", "filename")):
  45. dbver = pwman.data.factory.check_db_version(self.dbtype)
  46. self.dbver = float(dbver.strip("\'"))
  47. backup = '.backup-%s'.join(os.path.splitext(self.dbname)) % \
  48. time.strftime(
  49. '%Y-%m-%d-%H:%m')
  50. shutil.copy(self.dbname, backup)
  51. print "backup created in ", backup
  52. def read_old_db(self):
  53. "read the old db and get all nodes"
  54. self.db = pwman.data.factory.create(self.dbtype, self.dbver)
  55. enc = CryptoEngine.get()
  56. enc.set_callback(CLICallback())
  57. self.db.open()
  58. self.oldnodes = self.db.listnodes()
  59. self.oldnodes = self.db.getnodes(self.oldnodes)
  60. def create_new_db(self):
  61. newdb_name = '-newdb'.join(os.path.splitext(self.dbname))
  62. self.newdb = pwman.data.factory.create(self.dbtype, _NEWVERSION,
  63. newdb_name)
  64. self.newdb._open()
  65. def convert_nodes(self):
  66. """convert old nodes instances to new format"""
  67. self.NewNodes = []
  68. for node in self.oldnodes:
  69. username = node.get_username()
  70. password = node.get_password()
  71. url = node.get_url()
  72. notes = node.get_notes()
  73. tags = node.get_tags()
  74. tags_strings = [tag.get_name() for tag in tags]
  75. newNode = NewNode(username=username,
  76. password=password,
  77. url=url,
  78. notes=notes,
  79. tags=tags_strings
  80. )
  81. self.NewNodes.append(newNode)
  82. def save_new_nodes_to_db(self):
  83. self.newdb.addnodes(self.NewNodes)
  84. self.newdb._commit()
  85. def run(self):
  86. self.read_old_db()
  87. self.create_new_db()
  88. self.convert_nodes()
  89. self.save_new_nodes_to_db()
  90. # TODO: copy all other stuff from old data base ...