Browse Source

refactor new registrations script into python

Pi 1 year ago
parent
commit
dbccc66821
2 changed files with 61 additions and 27 deletions
  1. 48 0
      cropswap-register-new.py
  2. 13 27
      cropswap-register-new.sh

+ 48 - 0
cropswap-register-new.py

@@ -0,0 +1,48 @@
+#!/usr/bin/env python3
+# depends (debian) python3-gdbm
+import dbm
+import sys
+import os
+from pathlib import Path
+# from python 3.12 only :(
+#from itertools import batched
+
+mlist = f'/var/mail/maildrop/{sys.argv[1]}/mlist'
+
+def batched(_iter, n, pad=True):
+    ret = []
+    for i in _iter:
+        if len(ret) < n:
+            ret.append(i)
+            continue
+        yield ret
+        ret = [i]
+
+    rem = len(ret) - n
+    if pad and rem:
+        ret[rem:] = [None,]*rem
+    yield ret
+
+def gdbm_iter_keys(dbpath):
+    with dbm.open(dbpath, 'r') as db:
+        k = db.firstkey()
+        while k is not None:
+            yield map(lambda x: x.decode('utf-8'), [k, db.get(k)])
+            k = db.nextkey(k)
+
+members = {
+     k for k,_ in filter(
+        lambda x: '@' in set(x[0]) - set(x[1]),
+        map(tuple, gdbm_iter_keys(mlist))
+    )
+}
+
+lines = sys.stdin.buffer.read().decode('utf-8').splitlines()
+
+for name, email in batched(lines, 2):
+    if email in members:
+        continue
+    if '@' in name:
+        continue
+    sys.stdout.buffer.write(f"{name}\n{email}\n".encode('utf-8'))
+

+ 13 - 27
cropswap-register-new.sh

@@ -9,40 +9,26 @@ form_dir="$1"
 
 SCRIPTDIR=$(readlink -f "${BASH_SOURCE[0]%/*}")
 
-declare -a contacts
-update_members(){
-	name="${contacts[$1-1]}"
-	email="${2}"
-	MEMBERS["$email"]="${name}"
-}
+cd "${WD}"
+
+awk -F ":" -- \
+	'/Name:/ { print substr($2,2) } /Email:/ { print substr($2,2) }' \
+	$(find ${form_dir} -type f -name "*.txt") \
+> signup
+
 update_signup(){
-	name="${contacts[$1-1]}"
-	email="${2}"
-	for key in "${!MEMBERS[@]}"; do
-		[ "${key}" == "${email}" ] && return
-	done
-	SIGNUP["$email"]="${name}"
+       name="${signup[$1-1]}"
+       email="${2}"
+       SIGNUP["$email"]="${name}"
 }
-for group in cropswap cropswap-members; do
-	declare -A MEMBERS=()
-	# Define an array of contacts (name and email pairs)
-	#
-	# Reads files in $form_dir with structure like
-	#
-	#  Name: Som E. Name
-	#  Email: address@example.com
-	mapfile -t -c 2 -C update_members contacts < <("${SCRIPTDIR}/gdbm-iter.py" "/var/mail/maildrop/${group}@shandan.one/mlist")
-	unset contacts
 
+for group in cropswap{,-members}; do
 	declare -A SIGNUP=()
-	mapfile -t -c 2 -C update_signup contacts < <(find ${form_dir} -type f -name "*.txt" -exec awk -F ":" '
-/Name:/ { print substr($2,2) }
-/Email:/ { print substr($2,2) }
-' {} \;)
+	mapfile -t -c 2 -C update_signup signup < <("${SCRIPTDIR}/cropswap-register-new.py" "${group}@shandan.one" < signup)
 
 	for email in "${!SIGNUP[@]}"; do
 		set -x
-		"${SCRIPTDIR}/cropswap-register.sh" "${group}@shandan.one" "${SIGNUP["$email"]}" "$email" NONE
+		"${SCRIPTDIR}/cropswap-register.sh" "${group}@shandan.one" "${signup["$email"]}" "$email" NONE
 		set +x
 	done
 done