فهرست منبع

- add module and command line option to convert the db to new format

oz123 11 سال پیش
والد
کامیت
d9d1435eae
2فایلهای تغییر یافته به همراه56 افزوده شده و 2 حذف شده
  1. 42 0
      pwman/data/convertdb.py
  2. 14 2
      scripts/pwman3

+ 42 - 0
pwman/data/convertdb.py

@@ -0,0 +1,42 @@
+#============================================================================
+# This file is part of Pwman3.
+#
+# Pwman3 is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License, version 2
+# as published by the Free Software Foundation;
+#
+# Pwman3 is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Pwman3; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+#============================================================================
+# Copyright (C) 2013 Oz Nahum <nahumoz@gmail.com>
+#============================================================================
+
+import os
+import shutil
+import os.path
+import time
+
+
+class PwmanConvertDB(object):
+    """
+    Class to migrate from DB in version 0.3 to
+    DB used in later versions.
+    """
+
+    def __init__(self, args, config):
+        db = config.get_value('Database', 'filename')
+        print "Will convert the following Database: %s " % db
+        backup = '.backup-%s'.join(os.path.splitext(db)) % \
+            time.strftime(
+                '%Y-%m-%d-%H:%m')
+        shutil.copy(db, backup)
+        print "backup created in ", backup
+
+    def run(self):
+        pass

+ 14 - 2
scripts/pwman3

@@ -37,9 +37,15 @@ parser.add_argument('-d', '--database', dest='dbase')
 parser.add_argument('-e', '--encryption', dest="algo",
                     help="Possible options are: AES(default), ARC2, ARC4, "
                     + "Blowfish, CAST, DES, DES3, IDEA, RC5")
+parser.add_argument('-k', '--convert', dest='dbconvert',
+                    action='store_true', default=False,
+                    # os.path.expanduser('~/.pwman/pwman.db'),
+                    help="Convert old DB format to Version >= 0.4.",
+                    )
 
 parser.add_argument('-t', '--test',  help="Run pwman from current directory \
 without installation", action="store_true")
+
 args = parser.parse_args()
 
 import sys
@@ -60,8 +66,10 @@ else:
 
 import pwman.util.config as config
 import pwman.data.factory
+from pwman.data.convertdb import PwmanConvertDB
 
 config_file = args.cfile
+print config_file
 
 
 def which(cmd):
@@ -129,7 +137,7 @@ try:
         dbver = pwman.data.factory.check_db_version(dbtype)
         dbver = float(dbver.strip("\'"))
     else:
-        dbver = 0.4
+        dbver = 0.3
     # the method create could create an old instance that
     # accepts cPickle object or new style instance that
     # accepts only strings.
@@ -137,6 +145,11 @@ try:
     # database to the new format using a command line tool.
     # version 0.5 pwman will depreciate that old and insecure
     # code ...
+    if args.dbconvert:
+        dbconvertor = PwmanConvertDB(args, config)
+        status = dbconvertor.run()
+        sys.exit(status)
+
     db = pwman.data.factory.create(dbtype, dbver)
     if dbver >= 0.4:
         cli = PwmanCliNew(db, xselpath)
@@ -157,4 +170,3 @@ finally:
     except Exception, e:
         print "Error: %s" % (e)
         sys.exit(-1)
-