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 --- __init__.py | 0 readme.rst | 4 ++++ scan.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- uuid-rules.rst | 2 +- 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 __init__.py mode change 100644 => 100755 scan.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/readme.rst b/readme.rst index caed21a..f6037eb 100644 --- a/readme.rst +++ b/readme.rst @@ -1 +1,5 @@ Beta + +Resources: + +https://nylas.com/blog/packaging-deploying-python 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() diff --git a/uuid-rules.rst b/uuid-rules.rst index 8c7b1e2..7ec3ad8 100644 --- a/uuid-rules.rst +++ b/uuid-rules.rst @@ -7,7 +7,7 @@ Lors d'un réorganisation, il faut: l'arborescence. -On réserve uuid.txt*. +On réserve `uuid.txt*`. scan: -- cgit v1.2.3