Skip to content

Commit

Permalink
added ability to clear catalogs
Browse files Browse the repository at this point in the history
moved a chunk of repeated logic into utils/delete-many-files! fn
  • Loading branch information
Torkus committed Sep 30, 2019
1 parent 1ea936c commit 8d5ecd5
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 46 deletions.
6 changes: 5 additions & 1 deletion TODO.md
Expand Up @@ -88,14 +88,16 @@ see CHANGELOG.md for a more formal list of changes by release
- the old one must have any open transactions closed and be shutdown before switching
- this should be easy to recreate
- done
* bug, 'clear cache' didn't delete the catalog.json
- added option to 'Cache' menu to 'Clear catalogs'
- done

### todo

* investigate switching to an embedded database
- compare current speed and code against loading addon category data serially
- as opposed to in three blocks (categories, addons, category-addons). We might save some time and code
- investigate prepared statements when inserting
* bug, 'clear cache' didn't delete the catalog.json
* gui tests are bypassing the path wrangling because the envvar library is using thread-local `binding`
- change path access to an atom
- I *think* this may have something to do with a truncated catalog I've encountered now (twice)
Expand All @@ -117,6 +119,8 @@ see CHANGELOG.md for a more formal list of changes by release

## todo bucket

* bug, if an addon directory goes missing between restarts, user configuration is lost
- initially it's ignored, but then the new settings are saved over the top
* bug, we have addons in multiple identical categories. fix this in catalog.clj
- see 319346
- remove call to set in db-load-catalog
Expand Down
61 changes: 22 additions & 39 deletions src/wowman/core.clj
Expand Up @@ -19,7 +19,7 @@
[http :as http]
[logging :as logging]
[nfo :as nfo]
[utils :as utils :refer [join not-empty? false-if-nil nav-map nav-map-fn]]
[utils :as utils :refer [join not-empty? false-if-nil nav-map nav-map-fn delete-many-files!]]
[catalog :as catalog]
[toc]
[specs :as sp]]))
Expand Down Expand Up @@ -775,55 +775,38 @@
;;


(defn-spec delete-cache nil?
"deletes the 'cache' directory that contains scraped html files, etag files, the catalog, etc.
nothing that isn't regenerated when missing."
(defn-spec delete-cache! nil?
"deletes the 'cache' directory that contains scraped html files and the etag db file.
all these are regenerated when missing"
[]
(warn "deleting cache")
(fs/delete-dir (paths :cache-dir))
;; todo: this and `init-dirs` needs revisiting
(fs/mkdirs (paths :cache-dir))
nil)
(fs/delete (paths :etag-db-file))

(defn-spec list-downloaded-addon-zips (s/coll-of ::sp/extant-file)
[dir ::sp/extant-dir]
(mapv str (fs/find-files dir #".+\-\-.+\.zip")))
(fs/mkdirs (paths :cache-dir)) ;; todo: this and `init-dirs` needs revisiting
nil)

;; todo: these are all variations on a theme. consider something generic
(defn delete-downloaded-addon-zips!
[]
(delete-many-files! (get-state :selected-addon-dir) #".+\-\-.+\.zip$" "downloaded addon zip"))

(defn-spec delete-downloaded-addon-zips nil?
"deletes all of the addon zip files downloaded to '/path/to/Addons/'"
(defn delete-wowman-json-files!
[]
(when-let [addon-dir (get-state :selected-addon-dir)]
(let [zip-files (list-downloaded-addon-zips addon-dir)
alert #(warn "deleting file " %)]
(warn (format "deleting %s downloaded addon zip files" (count zip-files)))
(dorun (map (juxt alert fs/delete) zip-files)))))
(delete-many-files! (get-state :selected-addon-dir) #"\.wowman\.json$" ".wowman.json"))

(defn delete-wowman-json-files
(defn delete-wowmatrix-dat-files!
[]
(when-let [addon-dir (get-state :selected-addon-dir)]
(let [wowman-json #(fs/find-files % #"\.wowman\.json$")
subdirs (filter fs/directory? (fs/list-dir addon-dir))
wowman-files (flatten (map wowman-json subdirs))
alert #(warn "deleting file " %)]
(warn (format "deleting %s .wowman.json files" (count wowman-files)))
(dorun (vec (map (juxt alert fs/delete) wowman-files))))))

(defn delete-wowmatrix-dat-files
(delete-many-files! (get-state :selected-addon-dir) #"(?i)WowMatrix.dat$" "WowMatrix.dat"))

(defn delete-catalog-files!
[]
(when-let [addon-dir (get-state :selected-addon-dir)]
(let [wowman-json #(fs/find-files % #"WowMatrix.dat$")
subdirs (filter fs/directory? (fs/list-dir addon-dir))
wowman-files (flatten (map wowman-json subdirs))
alert #(warn "deleting file " %)]
(warn (format "deleting %s WowMatrix.dat files" (count wowman-files)))
(dorun (vec (map (juxt alert fs/delete) wowman-files))))))

(defn-spec clear-all-temp-files nil?
(delete-many-files! (paths :data-dir) #".+\-catalog\.json$" "catalog"))

(defn-spec clear-all-temp-files! nil?
[]
(delete-downloaded-addon-zips)
(delete-cache))
(delete-downloaded-addon-zips!)
(delete-cache!)
(delete-catalog-files!))

;; version checking

Expand Down
2 changes: 2 additions & 0 deletions src/wowman/specs.clj
Expand Up @@ -10,6 +10,7 @@
(s/def ::list-of-keywords (s/coll-of keyword?))
(s/def ::list-of-list-of-keywords (s/coll-of ::list-of-keywords))

(s/def ::regex #(instance? java.util.regex.Pattern %))
(s/def ::short-string #(<= (count %) 80))

(defn-spec has-ext boolean?
Expand Down Expand Up @@ -66,6 +67,7 @@
(s/def ::extant-archive-file (s/and ::extant-file ::archive-file))
(s/def ::list-of-files (s/coll-of ::file))
(s/def ::anything (complement nil?)) ;; like `any?` but nil is considered false
(s/def ::dir ::file) ;; directory must also be a string and a valid File object, but not necessarily exist (yet)
(s/def ::extant-dir (s/and string? fs/directory?))
(s/def ::writeable-dir (s/and ::extant-dir fs/writeable?))
(s/def ::download-uri ::uri)
Expand Down
11 changes: 6 additions & 5 deletions src/wowman/ui/gui.clj
Expand Up @@ -735,12 +735,13 @@
impexp-menu [(ss/action :name "Export addon list" :handler (async-handler export-addon-list-handler))
(ss/action :name "Import addon list" :handler (async-handler import-addon-list-handler))]

cache-menu [(ss/action :name "Clear cache" :handler (async-handler core/delete-cache))
(ss/action :name "Clear addon zips" :handler (async-handler core/delete-downloaded-addon-zips))
(ss/action :name "Clear all" :handler (async-handler core/clear-all-temp-files))
cache-menu [(ss/action :name "Clear cache" :handler (async-handler core/delete-cache!))
(ss/action :name "Clear addon zips" :handler (async-handler core/delete-downloaded-addon-zips!))
(ss/action :name "Clear catalogs" :handler (async-handler core/delete-catalog-files!))
(ss/action :name "Clear all" :handler (async-handler core/clear-all-temp-files!))
:separator
(ss/action :name "Delete WowMatrix.dat files" :handler (async-handler core/delete-wowmatrix-dat-files))
(ss/action :name "Delete .wowman.json files" :handler (async-handler (comp core/refresh core/delete-wowman-json-files)))]
(ss/action :name "Delete WowMatrix.dat files" :handler (async-handler core/delete-wowmatrix-dat-files!))
(ss/action :name "Delete .wowman.json files" :handler (async-handler (comp core/refresh core/delete-wowman-json-files!)))]

help-menu [(ss/action :name "About wowman" :handler (handler about-wowman-dialog))]

Expand Down
23 changes: 23 additions & 0 deletions src/wowman/utils.clj
Expand Up @@ -15,6 +15,29 @@
[java-time :as jt]
[java-time.format]))

(defn-spec safe-to-delete? boolean?
"predicate, returns true if given file, stripped of parents, is rooted in given directory"
[dir ::sp/extant-dir file ::sp/extant-file]
(clojure.string/starts-with? file dir))

(defn-spec delete-many-files! nil?
"deletes a list of files rooted in given directory"
[dir ::sp/dir, regex ::sp/regex, file-type ::sp/short-string]
(if-not (fs/exists? dir)
(warn "directory does not exist:" dir) ;; yet ...
(let [file-list (mapv str (fs/find-files dir regex))
suspicious (remove (partial safe-to-delete? dir) file-list)
alert #(warn "deleting file " %)]
(if-not (empty? suspicious)
;; this is a programming error, not a user error. if there is a problem we don't want N-1 more problems
(error (format "refusing to delete all files. files were found not rooted at %s" (count suspicious) dir))

(if (empty? file-list)
(info (format "no %s files to delete" file-type))
(do
(warn (format "deleting %s %s files" (count file-list) file-type))
(dorun (map (juxt alert fs/delete) file-list))))))))

(defn shallow-flatten
[lst]
(mapcat identity lst))
Expand Down
2 changes: 1 addition & 1 deletion test.sh
@@ -1,3 +1,3 @@
#!/bin/bash
# (always ratchet threshold upwards)
lein cloverage --fail-threshold 72 --html
lein cloverage --fail-threshold 74 --html

0 comments on commit 8d5ecd5

Please sign in to comment.