From 5908218cc206e0a3637d542360afa78eb2d943a8 Mon Sep 17 00:00:00 2001 From: rlbr Date: Thu, 29 Nov 2018 18:51:11 -0600 Subject: [PATCH] added hashify --- .sql.swp | Bin 0 -> 12288 bytes backup.py | 30 ------------------------------ fs_class.py | 32 +++++++++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 31 deletions(-) create mode 100644 .sql.swp diff --git a/.sql.swp b/.sql.swp new file mode 100644 index 0000000000000000000000000000000000000000..901c4f4abbca6f10a0e9a8bdbb5068ecbd3443ae GIT binary patch literal 12288 zcmeI%%}T>S5C`zBcTw~OMm=aix{19Ao*G#(P+PPc?J=7)HK7TK-7WYE`qG-Ut@KnW zcq;#a*^lgOV1CCWcAtX>x^8={i9cNJ&s1j9Vl{On{>=7R;qZUI_ zm2H#G%K1B=r#1w%Qcs8%M^5ncQ&B9dYxFDc5)DpNX($nOpOW*--7nyz+XKX?qq^2S>6Z=K`Z HoIC6r;~zy^ literal 0 HcmV?d00001 diff --git a/backup.py b/backup.py index f26d74c..a2dd90e 100755 --- a/backup.py +++ b/backup.py @@ -27,36 +27,6 @@ def file_hash(path, block_size=4096*8): _hash.update(bytes) return _hash.digest() -def hashify(top): - old_dir = os.getcwd() - os.chdir(top) - ret = [] - imo_hashes = set() - for root,dirs,files in os.walk('.'): - try: - for file in files: - filepath = os.path.join(root,file) - imohash = _imohash(filepath) - real_hash = None - if imohash in imo_hashes: - real_hash = file_hash(filepath) - else: - imo_hashes.add(imohash) - ret.append( - ( - filepath, - imohash, - real_hash - ) - ) - except PermissionError: - print('Access denied:',root) - except Exception as e: - print(e,file) - os.chdir(old_dir) - #print(imo_hashes) - return ret - def backup(top,db_path): paths = hashify(top) con = __init_database__(db_path) diff --git a/fs_class.py b/fs_class.py index 17e94d3..b30a09d 100755 --- a/fs_class.py +++ b/fs_class.py @@ -1,8 +1,31 @@ #!/usr/bin/python import sqlite3 +import functools def hash_file(path,hash_if_less,sample): return _imohash(path,hash_if_less,sample) +def hashify(top,hashfunc): + old_dir = os.getcwd() + os.chdir(top) + ret = [] + for root,dirs,files in os.walk('.'): + try: + for file in files: + filepath = os.path.join(root,file) + hash = hashfunc(filepath) + ret.append( + ( + filepath, + hash + ) + ) + except PermissionError: + print('Access denied:',root) + except Exception as e: + print(e,file) + os.chdir(old_dir) + return ret + def __init_database__(con): cur = con.cursor() cur.execute('''CREATE TABLE IF NOT EXISTS `PATHS` ( @@ -19,6 +42,11 @@ class fs: self.top=top self.hash_threshold=hash_threshold self.sample_size=sample_size + self.hashfunc = functools.partial( + hashfile, + hash_if_less=self.hash_threshold, + sample=self.sample + ) if db_path is None: self.db_path = 'fs.db' else: @@ -26,6 +54,8 @@ class fs: self.con = sqlite3.connect(self.db_path) __init_database__(self.con) self.cur = self.con.cursor() + + self.hash_files = {} if id is None: self.cur.execute('SELECT MAX(id) FROM backups') id=self.cur.fetchone() @@ -43,7 +73,7 @@ class fs: pass def backup(self,other): - pass + if __name__ == "__main__": import os test = fs(os.getcwd())