This repository has been archived on 2019-12-12. You can view files and clone it, but cannot push or open issues/pull-requests.
tildetown-scripts/scripts/town_stats.hy

65 lines
2.1 KiB
Hy
Raw Normal View History

2015-07-26 06:23:01 +00:00
(import [json [dumps]])
2014-10-15 05:07:15 +00:00
(import [os [listdir]])
2014-11-09 01:09:28 +00:00
(import [os.path [getmtime]])
2014-10-15 05:07:15 +00:00
(import [datetime [datetime]])
2015-07-26 06:23:01 +00:00
(import [sh [find facter]])
2014-12-29 02:46:07 +00:00
2015-07-26 06:23:01 +00:00
;; 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.
2014-10-15 05:07:15 +00:00
(defn slurp [filename]
2014-11-09 00:35:01 +00:00
(try
(.read (apply open [filename "r"] {"encoding" "utf-8"}))
(catch [e Exception]
"")))
2014-10-15 05:07:15 +00:00
(def default-html (slurp "/etc/skel/public_html/index.html"))
2015-07-26 06:23:01 +00:00
(defn default? [username]
(= default-html (slurp (.format "/home/{}/public_html/index.html" username))))
2014-10-15 05:07:15 +00:00
2015-02-13 21:22:09 +00:00
(defn bounded-find [path]
;; find might return 1 but still have worked fine (because of dirs it can't
;; go into)
(apply find [path "-maxdepth" "3"] {"_ok_code" [0 1]}))
2015-02-10 18:45:37 +00:00
2015-02-10 19:00:18 +00:00
(defn guarded-mtime [filename]
(let [[path (.rstrip filename)]]
(try
(getmtime path)
(catch [e Exception]
0))))
2014-11-09 01:09:28 +00:00
(defn modify-time [username]
2014-12-29 02:46:07 +00:00
(->> (.format "/home/{}/public_html" username)
2015-02-10 18:45:37 +00:00
bounded-find
2015-02-10 19:00:18 +00:00
(map guarded-mtime)
2014-12-29 02:46:07 +00:00
list
max))
2014-11-09 01:09:28 +00:00
(defn sort-user-list [usernames]
(apply sorted [usernames] {"key" modify-time}))
2014-11-09 01:16:11 +00:00
(defn user-generator [] (->> (listdir "/home")
2014-12-29 02:50:15 +00:00
(filter (fn [f] (and (not (= f "ubuntu")) (not (= f "poetry")))))))
2014-11-09 01:09:28 +00:00
2015-07-26 06:23:01 +00:00
(if (= __name__ "__main__")
2015-07-26 07:01:25 +00:00
(let [[all_users (->> (user-generator)
sort-user-list
reversed
(map (fn [un] {"username" un
"default" (default? un)}))
list)
[live_users (-> (filter (fn [u] (not (get u "default"))) all_users)
list)]]
[data {"all_users" users
"live_users" live_users
2015-07-26 06:23:01 +00:00
"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))))