Update bdd / Recover crypt_id for files already added

This commit is contained in:
LPOCHOLLE 2021-10-11 17:02:11 +02:00
parent 479bab1d96
commit bf57b17f99

View File

@ -25,11 +25,11 @@ class Backup:
size = os.path.getsize(uri) size = os.path.getsize(uri)
print(f + " : ", human_size(size)) print(f + " : ", human_size(size))
if size > tarball_size: if size > tarball_size:
crypt_name = self.bdd.add([{'name': f, 'path': path, 'size': size}], compress_mode="gz")
enc = crypt(compress(uri), self.key) enc = crypt(compress(uri), self.key)
crypt_name = self.bdd.add([{'name': f, 'path': uri, 'size': size}], compress_mode="gz")
save(enc, os.path.join(self.save_location, crypt_name)) save(enc, os.path.join(self.save_location, crypt_name))
else: else:
files.append({'name': f, 'path': uri, 'size': size}) files.append({'name': f, 'path': path, 'size': size})
elif os.path.isdir(uri): elif os.path.isdir(uri):
self.recurse(uri) self.recurse(uri)
if len(files) > 0: if len(files) > 0:
@ -143,18 +143,45 @@ class DataBase:
self.conn.commit() self.conn.commit()
def get_crypt_id(self, list_file):
cursor = self.conn.cursor()
crypt_id_list = []
for file in list_file:
cursor.execute("""SELECT crypt_id FROM files WHERE name=? AND path=?""",
(file['name'], file['path']))
try:
crypt_id_list.append(cursor.fetchone()[0])
except TypeError:
pass
if len(crypt_id_list) > 0:
return most_frequent(crypt_id_list)
else:
return None
def exist(self, file):
cursor = self.conn.cursor()
cursor.execute("""SELECT EXISTS (SELECT 1 FROM files WHERE name=? AND path=?)""",
(file['name'], file['path']))
return cursor.fetchone()[0]
def add(self, list_file, compress_mode=None): def add(self, list_file, compress_mode=None):
cursor = self.conn.cursor() cursor = self.conn.cursor()
cursor.execute("""SELECT IFNULL(max(id) + 1, 0) FROM crypt""") crypt_id = self.get_crypt_id(list_file)
crypt_id = cursor.fetchone()[0] if crypt_id is None:
cursor.execute("""SELECT IFNULL(max(id) + 1, 0) FROM crypt""")
crypt_id = cursor.fetchone()[0]
cursor.execute("""SELECT IFNULL(max(id) + 1, 0) FROM files""") cursor.execute("""SELECT IFNULL(max(id) + 1, 0) FROM files""")
files_id = cursor.fetchone()[0] files_id = cursor.fetchone()[0]
for file in list_file: for file in list_file:
cursor.execute("""INSERT INTO files VALUES(?, ?, ?, ?, ?)""", if self.exist(file):
(files_id, file['name'], file['path'], file['size'], crypt_id)) cursor.execute("""UPDATE files SET size=?, crypt_id=? WHERE name=? AND path=?""",
files_id += 1 (file['size'], crypt_id, file['name'], file['path']))
else:
cursor.execute("""INSERT INTO files VALUES(?, ?, ?, ?, ?)""",
(files_id, file['name'], file['path'], file['size'], crypt_id))
files_id += 1
cursor.execute("""INSERT INTO crypt VALUES(?, ?)""", (crypt_id, compress_mode)) cursor.execute("""INSERT OR IGNORE INTO crypt VALUES(?, ?)""", (crypt_id, compress_mode))
self.conn.commit() self.conn.commit()
return str(crypt_id) return str(crypt_id)
@ -173,3 +200,7 @@ def parse_size(size):
size = size + 'K' size = size + 'K'
number, unit = re.match(r"([0-9]+)([BKMGT])", size, re.I).groups() number, unit = re.match(r"([0-9]+)([BKMGT])", size, re.I).groups()
return int(float(number)*units[unit]) return int(float(number)*units[unit])
def most_frequent(list):
return max(set(list), key = list.count)