$search
00001 #! /usr/bin/env python 00002 00003 """ 00004 usage: %(progname)s [db filenames...] 00005 """ 00006 00007 00008 import os, sys, string, time, getopt 00009 from log import * 00010 00011 def dumpTable2(path, fn, tableName, fields): 00012 tfn = os.path.join(path, tableName + '.sql') 00013 warn("dumpTables", tfn) 00014 cmd = "sqlite3 %s '.schema %s' > %s" % (fn, tableName, tfn) 00015 warn(cmd) 00016 r = os.system(cmd) 00017 if r != 0: 00018 warn("return=", r) 00019 return False 00020 00021 import sqlite3 00022 conn = sqlite3.connect(fn) 00023 c = conn.cursor() 00024 00025 00026 fieldList = [] 00027 for field in fields: 00028 fieldList.append("%s > ''" % field) 00029 00030 c.execute("SELECT %s FROM %s WHERE %s" % (string.join(fields, ","), tableName, string.join(fieldList, " AND "))) 00031 rows = c.fetchall() 00032 print len(rows) 00033 00034 rows.sort() 00035 00036 fp = open(tfn, "a") 00037 00038 for row in rows: 00039 mnum = row[0] 00040 fieldList = [] 00041 i = 0 00042 for field in fields: 00043 fieldList.append("%s='%s'" % (field, row[i])) 00044 i = i + 1 00045 00046 open(".currentRow", "w").write("%s\n" % string.join(fieldList)) 00047 try: 00048 c.execute("select * from %s where %s" % (tableName, string.join(fieldList, " AND "))) 00049 except: 00050 warn("Error with", row) 00051 continue 00052 00053 row = c.fetchone() 00054 00055 parts = [] 00056 i = 0 00057 for col in row: 00058 if type(col) == type(None): 00059 parts.append("NULL") 00060 elif type(col) == type(1): 00061 parts.append(str(col)) 00062 else: 00063 if c.rs.col_defs[i][0] == "odb_value": col = "" 00064 col = string.replace(col,"'","''") 00065 parts.append("'" + col + "'") 00066 i = i + 1 00067 sql = "INSERT INTO %s VALUES (%s);" % (tableName, string.join(parts, ", ")) 00068 fp.write(sql + "\n") 00069 fp.flush() 00070 00071 # print mnum, len(rows) 00072 fp.close() 00073 return True 00074 00075 00076 def dumpTable(path, fn, tableName): 00077 tfn = os.path.join(path, tableName + '.sql') 00078 warn("dumpTables", tfn) 00079 cmd = "sqlite3 %s '.dump %s' > %s" % (fn, tableName, tfn) 00080 warn(cmd) 00081 r = os.system(cmd) 00082 if r != 0: 00083 warn("return=", r) 00084 return False 00085 return True 00086 00087 def loadTable(path,fn, tableName): 00088 warn("loadTables", tableName) 00089 00090 tfn = os.path.join(path, tableName + '.sql') 00091 00092 cmd = "sqlite3 %s.new < %s" % (fn, tfn) 00093 warn(cmd) 00094 r = os.system(cmd) 00095 if r != 0: 00096 warn("return=", r) 00097 return False 00098 00099 return True 00100 00101 def rebuildDatabase(fn): 00102 if not os.path.exists(fn): return 00103 00104 path, f = os.path.split(fn) 00105 00106 dumpPath = os.path.join(path, "dump") 00107 try: os.mkdir(dumpPath, 0700) 00108 except os.error, reason: pass 00109 00110 cmd = "sqlite3 %s .tables" % fn 00111 fp = os.popen(cmd, "r") 00112 body = fp.read() 00113 fp.close() 00114 00115 tables = body.split() 00116 00117 #tables = ["mb_msgdata", "mb_msgthread"] 00118 #tables = ["mb_msgthread"] 00119 00120 for tableName in tables: 00121 if tableName == "mb_msgdata": 00122 ret = dumpTable2(dumpPath, fn, tableName, fields=('mnum', )) 00123 elif tableName == "mb_thread": 00124 ret = dumpTable2(dumpPath, fn, tableName, fields=('thr_id', 'mnum')) 00125 else: 00126 ret = dumpTable(dumpPath, fn, tableName) 00127 if ret is False: return 00128 00129 try: os.unlink(fn + ".new") 00130 except os.error, reason: pass 00131 00132 for tableName in tables: 00133 ret = loadTable(dumpPath, fn, tableName) 00134 if ret is False: return 00135 00136 now = int(time.time()) 00137 dateStr = time.strftime("%Y%m%d_%H%M%S", time.localtime(now)) 00138 warn(dateStr) 00139 00140 os.rename(fn, fn + "_" + dateStr) 00141 os.rename(fn + ".new", fn) 00142 00143 00144 def test(): 00145 pass 00146 00147 def usage(progname): 00148 print __doc__ % vars() 00149 00150 def main(argv, stdout, environ): 00151 progname = argv[0] 00152 optlist, args = getopt.getopt(argv[1:], "", ["help", "test", "debug"]) 00153 00154 testflag = 0 00155 if len(args) == 0: 00156 usage(progname) 00157 return 00158 for (field, val) in optlist: 00159 if field == "--help": 00160 usage(progname) 00161 return 00162 elif field == "--debug": 00163 debugfull() 00164 elif field == "--test": 00165 testflag = 1 00166 00167 if testflag: 00168 test() 00169 return 00170 00171 for fn in args: 00172 rebuildDatabase(fn) 00173 00174 00175 00176 if __name__ == "__main__": 00177 main(sys.argv, sys.stdout, os.environ)