diff options
-rw-r--r-- | __init__.py | 0 | ||||
-rw-r--r-- | readme.rst | 4 | ||||
-rwxr-xr-x[-rw-r--r--] | scan.py | 50 | ||||
-rw-r--r-- | uuid-rules.rst | 2 |
4 files changed, 52 insertions, 4 deletions
diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/__init__.py @@ -1 +1,5 @@ Beta + +Resources: + +https://nylas.com/blog/packaging-deploying-python @@ -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: |