update to modified_users.py fixes load times and other issues. Thanks to xwindows.

This commit is contained in:
deepend 2022-12-31 20:00:02 +00:00
parent ca38d62131
commit 05c820f908
4 changed files with 134 additions and 13 deletions

1
cache.json Normal file

File diff suppressed because one or more lines are too long

55
modified_users.old.py Executable file
View File

@ -0,0 +1,55 @@
#!/usr/bin/env python
import hashlib
import json
import os
LINK = "1~{0} /~{0} tilde.club 70"
def compare_gophermaps(user, cache):
user_path = f"/home/{user}/public_gopher/gophermap"
if not os.path.exists(user_path):
# Ignore user if public_gopher folder is not found
return
if user in cache:
print(LINK.format(user))
return
skel_path = "/etc/skel/public_gopher/gophermap"
skel_hash = hashlib.sha256()
with open(skel_path, "rb") as f:
skel_hash.update(f.read())
try:
user_hash = hashlib.sha256()
with open(user_path, "rb") as f:
user_hash.update(f.read())
if user_hash.hexdigest() != skel_hash.hexdigest():
cache[user]=True
print(LINK.format(user))
except PermissionError:
# Ignore PermissionError exceptions and treat the gophermap file as one to not list
pass
def main():
cache_path = "cache.json"
# Load the cache from disk if it exists, otherwise create a new cache
if os.path.exists(cache_path):
with open(cache_path, "r") as f:
cache = json.load(f)
else:
cache = {}
for user in sorted(os.listdir("/home"), key=lambda x: x.lower()):
compare_gophermaps(user, cache)
# Save the cache to disk
with open(cache_path, "w") as f:
json.dump(cache, f)
if __name__ == "__main__":
main()

View File

@ -1,19 +1,83 @@
#!/usr/bin/env python
import filecmp
import os
def users():
return sorted(os.listdir("/home"), key=lambda x: x.lower())
import hashlib
import json
import os
import time
LINK = "1~{0} /~{0} tilde.club 70"
for user in users():
if os.path.exists(f"/home/{user}/public_gopher/gophermap"):
try:
if not filecmp.cmp(
"/etc/skel/public_gopher/gophermap",
f"/home/{user}/public_gopher/gophermap", shallow=False):
print(LINK.format(user))
except IOError:
pass
# This function returns boolean on whether `cache` was modified
def compare_gophermaps(user, cache):
user_path = f"/home/{user}/public_gopher/gophermap"
current_time = time.time()
time_threshold = 259200
if user in cache and current_time-cache[user] < time_threshold:
print(LINK.format(user))
return False
if not os.path.exists(user_path):
# Ignore user if public_gopher folder is not found
if user in cache:
del cache[user]
return True
else:
return False
skel_path = "/etc/skel/public_gopher/gophermap"
skel_hash = hashlib.sha256()
with open(skel_path, "rb") as f:
skel_hash.update(f.read())
try:
user_hash = hashlib.sha256()
with open(user_path, "rb") as f:
user_hash.update(f.read())
if user_hash.hexdigest() != skel_hash.hexdigest():
cache[user]=current_time
print(LINK.format(user))
return True
else:
if user in cache:
del cache[user]
return True
else:
return False
except FileNotFoundError:
# Accessible gopher directory without gophermap is a non-stock gopher directory
cache[user]=current_time
print(LINK.format(user))
return True
except:
# PermissionError and the rest, treat the gophermap file as one to not list
if user in cache:
del cache[user]
return True
else:
return False
def main():
cache_path = "cache.json"
modified = False
# Load the cache from disk if it exists, otherwise create a new cache
if os.path.exists(cache_path):
with open(cache_path, "r") as f:
cache = json.load(f)
else:
cache = {}
modified = True
for user in sorted(os.listdir("/home"), key=lambda x: x.lower()):
if compare_gophermaps(user, cache):
modified = True
# Save the cache to disk, if modified
if modified:
with open(cache_path, "w") as f:
json.dump(cache, f)
if __name__ == "__main__":
main()

1
ttbp.txt Symbolic link
View File

@ -0,0 +1 @@
/home/benharri/workspace/ttbp/README.md