aboutsummaryrefslogtreecommitdiffstats
path: root/scan.py
diff options
context:
space:
mode:
authorVG <vg@devys.org>2015-07-19 03:42:12 +0200
committerVG <vg@devys.org>2015-07-19 03:42:12 +0200
commit6f1ef76174f7cc5de369b45c203a5e5001cdf1c3 (patch)
tree8031b808421e1a9931029a9da5304e49ccd82ff1 /scan.py
parent49a2da7bf5253a5c3769a783ce182e83cadf1c4a (diff)
downloaduuidxer-6f1ef76174f7cc5de369b45c203a5e5001cdf1c3.tar.gz
uuidxer-6f1ef76174f7cc5de369b45c203a5e5001cdf1c3.tar.bz2
uuidxer-6f1ef76174f7cc5de369b45c203a5e5001cdf1c3.zip
finish first implementation of scan.py
Diffstat (limited to 'scan.py')
-rwxr-xr-x[-rw-r--r--]scan.py50
1 files changed, 47 insertions, 3 deletions
diff --git a/scan.py b/scan.py
index c611920..7432cef 100644..100755
--- 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()