From 773efb7778647239383f604bd94e58f9e7bc6f24 Mon Sep 17 00:00:00 2001 From: "tilde.town" Date: Sun, 26 Jul 2015 06:23:01 +0000 Subject: [PATCH] new templating stuff --- scripts/active_users.sh | 2 +- scripts/generate_home_page.sh | 18 ++++++++++++ scripts/mustache.hy | 16 +++++++++++ scripts/{userlist.hy => town_stats.hy} | 40 ++++++++++++-------------- 4 files changed, 54 insertions(+), 22 deletions(-) create mode 100755 scripts/generate_home_page.sh create mode 100644 scripts/mustache.hy rename scripts/{userlist.hy => town_stats.hy} (51%) diff --git a/scripts/active_users.sh b/scripts/active_users.sh index d44874d..efe4fbb 100755 --- a/scripts/active_users.sh +++ b/scripts/active_users.sh @@ -1,2 +1,2 @@ #!/bin/bash -who | cut -d' ' -f1 | sort -u | wc -l +who | cut -d' ' -f1 | sort -u | wc -l | xargs echo "active_user_count=" | sed 's/ //' diff --git a/scripts/generate_home_page.sh b/scripts/generate_home_page.sh new file mode 100755 index 0000000..1a591cf --- /dev/null +++ b/scripts/generate_home_page.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games + +source /var/local/venvs/tildetown/bin/activate + +stats=/usr/local/bin/generate_stats +template=/var/www/tilde.town/template.index.html +mustache=/var/local/tildetown/scripts/mustache.hy +output_path=/var/www/tilde.town/index.html + +# hello from datagrok +# +# generate_stats was breaking and outputting no lines. i dunno how to fix that, +# but i can un-disaappear the homepage by saying "output empty json if broken" +# +# generate_stats | hy $mustache $template > $output_path +($stats || echo '{}') | hy $mustache $template > $output_path diff --git a/scripts/mustache.hy b/scripts/mustache.hy new file mode 100644 index 0000000..187bdcb --- /dev/null +++ b/scripts/mustache.hy @@ -0,0 +1,16 @@ +(import [json [loads]]) +(import [sys [argv stdin]]) +(import [pystache [render]]) + +(defn slurp [filename] + (try + (.read (apply open [filename "r"] {"encoding" "utf-8"})) + (catch [e Exception] + ""))) + +(if (= __name__ "__main__") + (let [[template (-> (get argv 1) + slurp)] + [data (-> (.read stdin) + loads)]] + (print (render template data)))) diff --git a/scripts/userlist.hy b/scripts/town_stats.hy similarity index 51% rename from scripts/userlist.hy rename to scripts/town_stats.hy index d2af811..b640ce7 100644 --- a/scripts/userlist.hy +++ b/scripts/town_stats.hy @@ -1,14 +1,13 @@ +(import [json [dumps]]) (import [os [listdir]]) (import [os.path [getmtime]]) (import [datetime [datetime]]) -(import [sh [find]]) +(import [sh [find facter]]) -;; this script emits HTML on standard out that constitutes a user -;; list. It denotes who has not updated their page from the -;; default. It also reports the time this script was run. - -(def timestamp (.strftime (.now datetime) "%Y-%m-%d %H:%M:%S")) +;; this script emits json on standard out that has information about tilde.town +;; users. It denotes who has not updated their page from the default. It also +;; reports the time this script was run. The user list is sorted by public_html update time. (defn slurp [filename] (try @@ -18,13 +17,8 @@ (def default-html (slurp "/etc/skel/public_html/index.html")) -(defn dir->html [username] - (let [[default (= default-html (slurp (.format "/home/{}/public_html/index.html" username)))]] - (.format "
  • {} {}
  • " - username username - (if default - "(default :3)" - "")))) +(defn default? [username] + (= default-html (slurp (.format "/home/{}/public_html/index.html" username)))) (defn bounded-find [path] ;; find might return 1 but still have worked fine (because of dirs it can't @@ -51,13 +45,17 @@ (defn user-generator [] (->> (listdir "/home") (filter (fn [f] (and (not (= f "ubuntu")) (not (= f "poetry"))))))) -(def user-list (->> (user-generator) +(if (= __name__ "__main__") + (let [[users (->> (user-generator) sort-user-list reversed - (map dir->html) - (.join "\n"))) - -(print (.format "our esteemed users ({})
    generated at {}
    " - (len (list (user-generator))) - timestamp - user-list)) + (map (fn [un] {"username" un + "default" (default? un)})) + list)] + [data {"users" users + "active_user_count" (-> (. (facter "active_user_count") stdout) + .strip + int) + "generated_at" (.strftime (.now datetime) "%Y-%m-%d %H:%M:%S") + "num_users" (len users)}]] + (print (dumps data))))