From 6f1ef76174f7cc5de369b45c203a5e5001cdf1c3 Mon Sep 17 00:00:00 2001 From: VG Date: Sun, 19 Jul 2015 03:42:12 +0200 Subject: finish first implementation of scan.py --- scan.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) mode change 100644 => 100755 scan.py (limited to 'scan.py') diff --git a/scan.py b/scan.py old mode 100644 new mode 100755 index c611920..7432cef --- a/scan.py +++ b/scan.py @@ -1,10 +1,54 @@ #!/usr/bin/python3 +import os import uuid -import Path from pathlib +import time +import pickle + START_DIR = "/storage/animes" +DB_FILENAME = "/storage/animes.db" +uuid_msg = '''{uuid} +creation_date: {date} +orig_hostname: {hostname} +''' + + +def create_uuid_txt(root, hostname=os.uname().nodename): + my_uuid = str(uuid.uuid4()) + with open(os.path.join(root, 'uuid.txt'), 'w') as out: + out.write(uuid_msg.format(uuid=my_uuid, + date=time.strftime('%F %T'), + hostname=hostname)) + return my_uuid + + +def partition(pred, iterable): + 'Use a predicate to partition entries into false and true entries' + result = [], [] + for x in iterable: + result[pred(x)].append(x) + return result + + +def main(): + db = [] + with open(DB_FILENAME + '.new', 'wb') as out: + for root, dirs, files in os.walk(START_DIR): + files, uuids = partition(lambda f: '/uuid.txt' in f, + (os.path.join(root, f) for f in files)) + if len(uuids) == 1: + my_uuid = open(uuids[0]).readline().strip() + else: + print('removing:', uuids) + for f in uuids: + os.unlink(f) + my_uuid = create_uuid_txt(root) + db.append((my_uuid, [(f, os.stat(f).st_size) for f in files])) + + pickle.dump(db, out) + os.rename(DB_FILENAME + '.new', DB_FILENAME) -for root, dirs, files in os.walk(START_DIR): - s +if __name__ == '__main__': + main() -- cgit v1.2.3