diff options
author | Filipp Lepalaan <filipp@mac.com> | 2017-10-19 11:20:55 +0300 |
---|---|---|
committer | Filipp Lepalaan <filipp@mac.com> | 2017-10-19 11:20:55 +0300 |
commit | 8d560d6cf854fea81e0e0a2b8a22c406de4febb3 (patch) | |
tree | 04d9c5d26a27c982da5cba86fe3f4fac53149914 /dirstat.py | |
download | dirstat-8d560d6cf854fea81e0e0a2b8a22c406de4febb3.tar.gz dirstat-8d560d6cf854fea81e0e0a2b8a22c406de4febb3.tar.bz2 dirstat-8d560d6cf854fea81e0e0a2b8a22c406de4febb3.zip |
Diffstat (limited to 'dirstat.py')
-rwxr-xr-x | dirstat.py | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/dirstat.py b/dirstat.py new file mode 100755 index 0000000..e403334 --- /dev/null +++ b/dirstat.py @@ -0,0 +1,91 @@ +#! /usr/bin/env python + +import re +import os +import sys +import time +import sqlite3 +from os.path import join, getsize, basename + +DBPATH = 'dirstat.sqlite3' + +if not os.path.exists(DBPATH): + os.system('sqlite3 ' + DBPATH + ' < schema.sql') + +DB = sqlite3.connect(DBPATH) +SKIPNAMES = re.compile(r'^\.') +EXTENSION = re.compile(r'.+\.(\w+)$') + +def stats(sql): + for r in DB.execute(sql): + print(r) + + +def scandir(path, overwrite=True): + if not os.path.exists(path): + raise Exception('Invalid path: %s' % path) + + if not overwrite: + DB.execute('SELECT id FROM imports WHERE path = ?', (path,)) + if DB.fetchone(): + raise Exception('Path %s already imported' % path) + + DB.execute('INSERT INTO imports(path) VALUES (?)', (path,)) + cursor = DB.execute('SELECT last_insert_rowid() FROM imports') + import_id = cursor.fetchone()[0] + + total_size = 0 + total_count = 0 + sql = 'INSERT INTO paths(path, type, mtime, size, import_id) VALUES (?,?,?,?, ?)' + + start_time = time.time() + + for root, subdirs, files in os.walk(path): + for f in files: + t = 'DIR' + fp = unicode(join(root, f), 'UTF-8') + if SKIPNAMES.match(f): + continue + + result = EXTENSION.match(f) + + if result and result.group(0): + t = result.group(1).upper() + + s = os.lstat(fp) + + print 'Importing %s' % fp + DB.execute(sql, (fp, t, s.st_mtime, s.st_size, import_id,)) + total_count += 1 + total_size += s.st_size + + # Convert to timestamps + DB.execute("UPDATE paths SET mtime = datetime(mtime, 'unixepoch', 'localtime')") + DB.execute("UPDATE imports SET endtime = datetime('now'), count = ?, size = ? WHERE id = ?", + (import_id, total_count, total_size,)) + DB.commit() + + print '{0} items imported from {2} ({1} bytes)'.format(total_count, total_size, path) + + +if __name__ == '__main__': + if sys.argv[1] == 'import': + path = sys.argv[2] + scandir(path) + elif sys.argv[1] == 'stats': + query = 'SELECT type, size FROM paths GROUP BY type' + if len(sys.argv) == 3: + query = sys.argv[2] + stats(query) + elif sys.argv[1] == 'clear': + DB.execute('DELETE FROM paths') + DB.commit() + elif sys.argv[1] == 'shell': + while True: + cmd = raw_input() + try: + for r in DB.execute(cmd): + s = [] + print(u', '.join(r)) + except sqlite3.OperationalError as e: + print e |