mirror of
https://github.com/tildeclub/gopherhole
synced 2024-06-17 21:37:04 +00:00
update to modified_users.py fixes load times and other issues. Thanks to xwindows.
This commit is contained in:
parent
ca38d62131
commit
05c820f908
1
cache.json
Normal file
1
cache.json
Normal file
File diff suppressed because one or more lines are too long
55
modified_users.old.py
Executable file
55
modified_users.old.py
Executable 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()
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue
Block a user