-
Notifications
You must be signed in to change notification settings - Fork 12
/
update-sightings.rkt
47 lines (41 loc) · 1.51 KB
/
update-sightings.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#lang racket
;; if this directory contains no subdirectories
;; read the contents of all the files, stick them in a list
;; delete the directory
;; create a file with the same name as the directory we just deleted
;; write the list into that file
(require "userinfo.rkt")
(define old-sightings-root "/home/erich/live-bot/sightings.db")
(define new-sightings-root "/home/erich/live-bot/userinfo.db")
(define (nick-dirs)
(reverse
(fold-files
(lambda (path flavor accum)
(let* ([rel (find-relative-path old-sightings-root path)]
[depth (length (explode-path rel))])
(if (and (= 2 depth)
(directory-exists? path))
(cons path accum)
accum)))
'()
old-sightings-root)))
(define (upgrade! nick-dir)
(fprintf (current-error-port) "~a ... ~%" nick-dir)
(let* ([files
(map (lambda (rel)
(build-path nick-dir rel))
(directory-list nick-dir))]
[structs (for/list ([f files])
(call-with-input-file f read))])
(delete-directory/files nick-dir)
(let ([nick-file (regexp-replace #rx"/$"
(path->string (simplify-path nick-dir))
"")])
(call-with-output-file nick-file
(lambda (op)
(pretty-print (list (cons 'sightings structs)) op)
(newline op))))))
(module+ main
(for ([nick (nick-dirs)])
(upgrade! nick))
(rename-file-or-directory old-sightings-root new-sightings-root))