Skip to content

Commit

Permalink
Reformat files with zprint
Browse files Browse the repository at this point in the history
  • Loading branch information
schmir committed May 7, 2024
1 parent 2e08af9 commit 8dafe0d
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 206 deletions.
79 changes: 32 additions & 47 deletions build.clj
Original file line number Diff line number Diff line change
@@ -1,74 +1,59 @@
;; Run this script with "clj -T:build uber"
(ns build
(:require [clojure.tools.build.api :as b]
[babashka.fs :as fs]))
(:require [babashka.fs :as fs]
[clojure.tools.build.api :as b]))

(def lib 'com.github.schmir/prune-backups)
(def git-branch (b/git-process {:git-args "branch --show-current"}))
(def version (str (if (= git-branch "main")
nil
(str git-branch "-"))
"v0.1." (b/git-count-revs nil)))
(def version
(str (if (= git-branch "main") nil (str git-branch "-")) "v0.1." (b/git-count-revs nil)))
(def class-dir "target/classes")
(def meta-clj (format "%s/prune_backups/meta.clj" class-dir))
(def basis (b/create-basis {:project "deps.edn"}))
(def uber-file (format "target/%s.jar" (name lib)))
(def uberscript-file (format "target/%s" (name lib)))

(let [d (delay (b/delete {:path "target"}))]
(defn clean [_]
@d))
(let [d (delay (b/delete {:path "target"}))] (defn clean [_] @d))

(defn meta-ns
[]
(str "(ns prune-backups.meta)" \newline
"(def version " (pr-str version) ")" \newline
\newline))
(str "(ns prune-backups.meta)" \newline "(def version " (pr-str version) ")" \newline \newline))

(defn uber [_]
(defn uber
[_]
(clean nil)
(b/copy-dir {:src-dirs ["src" "resources"]
:target-dir class-dir})
(b/copy-dir {:src-dirs ["src" "resources"], :target-dir class-dir})
(spit meta-clj (meta-ns))
(b/compile-clj {:basis basis
:ns-compile '[prune-backups.cli]
:class-dir class-dir})
(b/uber {:class-dir class-dir
:uber-file uber-file
:basis basis
:main 'prune-backups.cli}))
(b/compile-clj {:basis basis, :ns-compile '[prune-backups.cli], :class-dir class-dir})
(b/uber {:class-dir class-dir, :uber-file uber-file, :basis basis, :main 'prune-backups.cli}))

(defn read-source
[]
(apply str (->> ["proto.clj" "rotate.clj" "tarsnap.clj" "zfs.clj" "cli.clj"]
(map (fn [s]
(str ";; --- " s \newline
(slurp (str "src/prune_backups/" s))
\newline))))))
(apply str
(->> ["proto.clj" "rotate.clj" "tarsnap.clj" "zfs.clj" "cli.clj"]
(map (fn [s]
(str ";; --- " s \newline (slurp (str "src/prune_backups/" s)) \newline))))))

(defn prelude
[]
(str
"#!/usr/bin/env bb" \newline
";; -*- mode: clojure -*-" \newline
";; prune-backups " version \newline
";;" \newline
";; DO NOT EDIT THIS FILE" \newline
";;" \newline
"" \newline))


(defn uberscript [_]
(str "#!/usr/bin/env bb"
\newline
";; -*- mode: clojure -*-"
\newline
";; prune-backups "
version
\newline
";;" \newline
";; DO NOT EDIT THIS FILE" \newline
";;" \newline
"" \newline))


(defn uberscript
[_]
(clean nil)
(fs/create-dirs "target")
(spit uberscript-file
(str
(prelude)
(meta-ns)
(read-source)))
(spit uberscript-file (str (prelude) (meta-ns) (read-source)))
(fs/set-posix-file-permissions uberscript-file "rwxr-xr-x"))

(defn uberall [_]
(uber nil)
(uberscript nil)
(spit "target/VERSION" version))
(defn uberall [_] (uber nil) (uberscript nil) (spit "target/VERSION" version))
56 changes: 27 additions & 29 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
{:paths ["src"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}
org.babashka/cli {:mvn/version "0.7.53"}
babashka/fs {:mvn/version "0.5.20"}
babashka/process {:mvn/version "0.5.21"}}
:aliases {:build {:deps {io.github.clojure/tools.build {:mvn/version "0.9.6"}
babashka/fs {:mvn/version "0.5.20"}}
:ns-default build}
:test
{:extra-paths ["test"]
:extra-deps {io.github.cognitect-labs/test-runner
{:git/tag "v0.5.1" :git/sha "dfb30dd"}}
:main-opts ["-m" "cognitect.test-runner"]
:exec-fn cognitect.test-runner.api/test}
{:paths ["src"],
:deps {org.clojure/clojure {:mvn/version "1.11.1"},
org.babashka/cli {:mvn/version "0.7.53"},
babashka/fs {:mvn/version "0.5.20"},
babashka/process {:mvn/version "0.5.21"}},
:aliases {:build {:deps {io.github.clojure/tools.build {:mvn/version "0.9.6"},
babashka/fs {:mvn/version "0.5.20"}},
:ns-default build},
:test {:extra-paths ["test"],
:extra-deps {io.github.cognitect-labs/test-runner {:git/tag "v0.5.1",
:git/sha "dfb30dd"}},
:main-opts ["-m" "cognitect.test-runner"],
:exec-fn cognitect.test-runner.api/test},

:outdated
#_:clj-kondo/ignore
{:extra-deps {com.github.liquidz/antq {:mvn/version "RELEASE"}
org.slf4j/slf4j-nop {:mvn/version "2.0.9"}} ;; silence SLF4J warning
:main-opts ["-m" "antq.core"]}
{:extra-deps {com.github.liquidz/antq {:mvn/version "RELEASE"},
org.slf4j/slf4j-nop {:mvn/version "2.0.9"}}, ;; silence SLF4J warning
:main-opts ["-m" "antq.core"]},

:dev
{:extra-paths ["test" "dev"]
:extra-deps {org.clojure/tools.namespace {:mvn/version "1.4.4"}
lambdaisland/kaocha {:mvn/version "1.87.1366"}
org.slf4j/slf4j-nop {:mvn/version "2.0.9"} ;; silence SLF4J warning
}}
:dev {:extra-paths ["test" "dev"],
:extra-deps {org.clojure/tools.namespace {:mvn/version "1.4.4"},
lambdaisland/kaocha {:mvn/version "1.87.1366"},
org.slf4j/slf4j-nop {:mvn/version "2.0.9"} ;; silence SLF4J
;; warning
}},

:kaocha
{:extra-deps {lambdaisland/kaocha {:mvn/version "1.87.1366"}
org.slf4j/slf4j-nop {:mvn/version "2.0.9"} ;; silence SLF4J warning
}
:main-opts ["-m" "kaocha.runner"]}}
}
:kaocha {:extra-deps {lambdaisland/kaocha {:mvn/version "1.87.1366"},
org.slf4j/slf4j-nop {:mvn/version "2.0.9"} ;; silence SLF4J
;; warning
},
:main-opts ["-m" "kaocha.runner"]}}}
9 changes: 4 additions & 5 deletions dev/user.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
(ns user
#_:clj-kondo/ignore
(:require
[clojure.test]
[clojure.tools.namespace.repl :as nsrepl]
[kaocha.repl]))
(:require [clojure.test]
[clojure.tools.namespace.repl :as nsrepl]
[kaocha.repl]))

(clojure.tools.namespace.repl/set-refresh-dirs "src" "test" "dev")
;; (clojure.tools.namespace.repl/disable-reload!)
Expand All @@ -16,4 +15,4 @@
(nsrepl/refresh)
(kaocha.repl/run-all)
;;(clojure.test/run-all-tests #"fin\..*")
)
)
75 changes: 29 additions & 46 deletions src/prune_backups/cli.clj
Original file line number Diff line number Diff line change
@@ -1,88 +1,71 @@
(ns prune-backups.cli
(:require [clojure.edn :as edn]
(:require [babashka.cli :as cli]
[clojure.edn :as edn]
[clojure.string :as str]
[prune-backups.meta :as meta]
[prune-backups.rotate :as rotate]
[prune-backups.proto :as proto]
[prune-backups.rotate :as rotate]
[prune-backups.tarsnap :as tarsnap]
[prune-backups.zfs :as zfs]
[babashka.cli :as cli])
[prune-backups.zfs :as zfs])
(:gen-class))

(set! *warn-on-reflection* true)


(def spec
{:config
{:desc "Path to edn config file"}
:version
{:desc "Show version information"}
:help
{:desc "Show help message"
:alias :h}})
{:config {:desc "Path to edn config file"},
:version {:desc "Show version information"},
:help {:desc "Show help message", :alias :h}})

(defn print-help
[]
(println "Usage: prune-backups [options]")
(println (cli/format-opts {:spec spec :order [:config :version :help]}))
(println (cli/format-opts {:spec spec, :order [:config :version :help]}))
(System/exit 0))

(defn find-archives-with-prefix
[prefix archives]
(filter (fn [a] (str/starts-with? (:archive a) prefix))
archives))
(filter (fn [a] (str/starts-with? (:archive a) prefix)) archives))

(defn rotate
([cfg]
(rotate cfg (proto/list-backups (:backup-set cfg))))
([cfg] (rotate cfg (proto/list-backups (:backup-set cfg))))
([cfg all-archives]
(->> (:prefixes cfg)
(mapcat (fn [prefix]
(let [archives (find-archives-with-prefix prefix all-archives)
res (rotate/rotate-backups archives :datetime (:rotate cfg))]
(println (str (pr-str prefix) ": " (count res) " archives, destroying " (count (filter rotate/drop? res))))
res (rotate/rotate-backups archives :datetime (:rotate cfg))]
(println (str (pr-str prefix)
": " (count res)
" archives, destroying " (count (filter rotate/drop? res))))
res))))))

(defn run
[bs]
(println (pr-str bs))
(let [archives (rotate bs)
destroy (filter rotate/drop? archives)]
destroy (filter rotate/drop? archives)]
;; (println (count archives) "archives, destroying" (count destroy))
;; (pprint/pprint archives)
(doseq [d destroy]
(proto/destroy-backup (:backup-set bs) d))))
(doseq [d destroy] (proto/destroy-backup (:backup-set bs) d))))

(defn tarsnap-reader
[m]
(-> m
(dissoc :configfile)
(assoc :backup-set (tarsnap/->TarsnapBackups (:configfile m)))))
(-> m (dissoc :configfile) (assoc :backup-set (tarsnap/->TarsnapBackups (:configfile m)))))

(defn zfs-reader
[m]
(-> m
(assoc :backup-set (zfs/->ZFS))))
(defn zfs-reader [m] (-> m (assoc :backup-set (zfs/->ZFS))))

(def custom-readers
{:readers {'prune-backups/tarsnap tarsnap-reader
'prune-backups/zfs zfs-reader}})
{:readers {'prune-backups/tarsnap tarsnap-reader, 'prune-backups/zfs zfs-reader}})

(defn -main [& args]
(try
(let [{:keys [help version config]} (cli/parse-opts args
{:spec spec
:restrict (keys spec)})]
(when version
(println meta/version)
(System/exit 0))
(when help
(print-help))
(defn -main
[& args]
(try (let [{:keys [help version config]} (cli/parse-opts args
{:spec spec, :restrict (keys spec)})]
(when version (println meta/version) (System/exit 0))
(when help (print-help))

(->> config slurp (edn/read-string custom-readers) run))
(System/exit 0)
(finally
(shutdown-agents))))
(->> config slurp (edn/read-string custom-readers) run))
(System/exit 0)
(finally (shutdown-agents))))

(when (= *file* (System/getProperty "babashka.file"))
(apply -main *command-line-args*))
(when (= *file* (System/getProperty "babashka.file")) (apply -main *command-line-args*))
6 changes: 4 additions & 2 deletions src/prune_backups/proto.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns prune-backups.proto)

(defprotocol BackupSet
(list-backups [this] "list all backups")
(destroy-backup [this backup] "destroy the given backup"))
(list-backups [this]
"list all backups")
(destroy-backup [this backup]
"destroy the given backup"))
56 changes: 18 additions & 38 deletions src/prune_backups/rotate.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@
[^java.time.LocalDateTime dt]
(.truncatedTo dt java.time.temporal.ChronoUnit/MINUTES))

(defn hour-key
[^java.time.LocalDateTime dt]
(.truncatedTo dt java.time.temporal.ChronoUnit/HOURS))
(defn hour-key [^java.time.LocalDateTime dt] (.truncatedTo dt java.time.temporal.ChronoUnit/HOURS))

(defn day-key
[^java.time.LocalDateTime dt]
(.. dt
(truncatedTo java.time.temporal.ChronoUnit/DAYS)
(toLocalDate)))
(.. dt (truncatedTo java.time.temporal.ChronoUnit/DAYS) (toLocalDate)))

(defn week-key
[^java.time.LocalDateTime dt]
Expand All @@ -33,49 +29,33 @@
(truncatedTo java.time.temporal.ChronoUnit/DAYS)
(toLocalDate)))

(defn year-key
[^java.time.LocalDateTime dt]
(.getYear dt))
(defn year-key [^java.time.LocalDateTime dt] (.getYear dt))

(defn keep-first-drop-rest
[part]
(cons (assoc (first part)
::keep? true)
(map (fn [elem]
(assoc elem
::drop? true))
(rest part))))
(cons (assoc (first part) ::keep? true) (map (fn [elem] (assoc elem ::drop? true)) (rest part))))

(defn preserve*
[p keyfn num-preserve]
(let [partitions (take num-preserve (partition-by keyfn (:rest p)))
this-result (mapcat keep-first-drop-rest partitions)
(let [partitions (take num-preserve (partition-by keyfn (:rest p)))
this-result (mapcat keep-first-drop-rest partitions)
num-considered (count this-result)]
{:result (concat (:result p) this-result)
:rest (drop num-considered (:rest p))}))
{:result (concat (:result p) this-result), :rest (drop num-considered (:rest p))}))

(defn drop-rest
[p]
{:result (concat (:result p)
(mapv (fn [el] (assoc el ::drop? true))
(:rest p)))
:rest nil})
{:result (concat (:result p) (mapv (fn [el] (assoc el ::drop? true)) (:rest p))), :rest nil})

(defn sort-backups
[xs get-datetime]
(sort-by get-datetime
(fn [a b] (compare b a))
xs))
(defn sort-backups [xs get-datetime] (sort-by get-datetime (fn [a b] (compare b a)) xs))

(defn rotate-backups
[xs get-datetime & {:keys [minutely hourly daily weekly monthly yearly]}]
(cond-> {:result nil
:rest (sort-backups xs get-datetime)}
minutely (preserve* (comp minute-key get-datetime) minutely)
hourly (preserve* (comp hour-key get-datetime) hourly)
daily (preserve* (comp day-key get-datetime) daily)
weekly (preserve* (comp week-key get-datetime) weekly)
monthly (preserve* (comp month-key get-datetime) monthly)
yearly (preserve* (comp year-key get-datetime) yearly)
true drop-rest
true :result))
(cond-> {:result nil, :rest (sort-backups xs get-datetime)}
minutely (preserve* (comp minute-key get-datetime) minutely)
hourly (preserve* (comp hour-key get-datetime) hourly)
daily (preserve* (comp day-key get-datetime) daily)
weekly (preserve* (comp week-key get-datetime) weekly)
monthly (preserve* (comp month-key get-datetime) monthly)
yearly (preserve* (comp year-key get-datetime) yearly)
true drop-rest
true :result))
Loading

0 comments on commit 8dafe0d

Please sign in to comment.