From 71d3f9ecea8b3d0f53df7cc09af194e5b6f42db3 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Sat, 2 Jun 2018 14:37:53 -0400 Subject: [PATCH] doorUpdater: Do hashing on output csvs instead of MembershipWorks input --- doorUpdater.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/doorUpdater.py b/doorUpdater.py index 0e8e15a..cf318a6 100755 --- a/doorUpdater.py +++ b/doorUpdater.py @@ -56,14 +56,7 @@ def getMembershipworksData(): print(r.text) sys.exit(1) - newHash = md5(bytes(r.text, 'utf8')).hexdigest() - if os.path.exists('/tmp/doorUpdaterLastHash'): - with open('/tmp/doorUpdaterLastHash', 'r') as f: - if newHash == f.read(): - print("hashes are the same, not updating") - sys.exit(0) - - return r.text, newHash + return r.text def makeMember(member, doorAuth): """Create an output CSV row for the member""" @@ -92,7 +85,7 @@ def makeMember(member, doorAuth): return out -def makeDoor(doorName, doorData, members): +def makeDoor(doorName, doorData, members, prevHash): """Create a CSV for the given door""" outString = StringIO() writer = csv.DictWriter(outString, fieldnames) @@ -105,21 +98,33 @@ def makeDoor(doorName, doorData, members): print(outString.getvalue()) outString.seek(0) - doCSVImport(doorData["ip"], outString) - pass + doorHash = md5(bytes(outString.getvalue(), 'utf8')).hexdigest() + if doorHash == prevHash: + print("Door", doorName, "not changed, not updating") + else: + doCSVImport(doorData["ip"], outString) + + return doorHash def main(): - data, newHash = getMembershipworksData() + data = getMembershipworksData() reader = csv.DictReader(StringIO(data)) members = list(reader) + if os.path.exists('/tmp/doorUpdaterLastHash'): + with open('/tmp/doorUpdaterLastHash', 'r') as f: + hashes = json.load(f) + else: + hashes = {} + for doorName, doorData in config["doors"].items(): print(doorName, doorData) - makeDoor(doorName, doorData, members) + hashes[doorName] = makeDoor(doorName, doorData, members, + hashes.get(doorName, "")) # write out hash if we sucessfully got here with open('/tmp/doorUpdaterLastHash', 'w') as f: - f.write(newHash) + json.dump(hashes, f) if __name__ == '__main__': main()