-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.py
More file actions
53 lines (33 loc) · 1.08 KB
/
sync.py
File metadata and controls
53 lines (33 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
from datetime import datetime
import hashlib
import sqlite3
class FileInfo(object):
def __init__(self):
self.name = None
self.ext = None
self.fullpath = None
def get_connection():
return sqlite3.connect('memstore.db')
def insert_file(full_path, creation_date, size, md5_hash):
conn = get_connection()
c = conn.cursor()
c.execute('INSERT INTO "fileobj"(fullpath, creation_date, size, md5_hash) VALUES(?, ?, ?, ?);',
[full_path, creation_date, size, md5_hash])
conn.commit()
def get_files_recursive(dir_path):
result = []
for root, dirnames, filenames in os.walk(dir_path):
for f in filenames:
full_path = os.path.join(root, f)
result.append(full_path)
#result[f] = full_path
return result
def get_md5(filename):
return hashlib.md5(open(filename, 'rb').read()).hexdigest()
def sync(d):
files = get_files_recursive(d)
for f in files:
md5_hash = get_md5(f)
size = os.path.getsize(f)
insert_file(f, datetime.now(), size, md5_hash)