From b3e6113ba4c329e899fa3d1c5c5cdc01654c5ed3 Mon Sep 17 00:00:00 2001 From: Lehi Toskin Date: Wed, 3 Dec 2014 16:59:58 -0800 Subject: [PATCH] Migrate old files, proper handling of profiles --- blight.rkt | 86 +++--------------------------- config.rkt | 148 ++++++++++++++++++++++++++++++++++++++++++---------- history.rkt | 10 ---- 3 files changed, 128 insertions(+), 116 deletions(-) diff --git a/blight.rkt b/blight.rkt index 6fcc51d..e00b1c8 100755 --- a/blight.rkt +++ b/blight.rkt @@ -44,72 +44,6 @@ Unported\", all credit attributed to Adam Reid.") and bug the dev! Alternatively, you could join #tox-dev on freenode and see if people have a similar problem.") -#| -command-line stuff - ---profile: determine the profile to use ---list: list available profiles (by looking for .tox files) -arg: list of files to copy to tox-dir as .tox files - -1. default profile is "blight" -2. all other files are determined by adding to profile-name - ex: (string-append (profile-name) "-config.json") - (string-append (profile-name) "-data.tox") - (string-append (profile-name) "-history.sqlite") -|# -(let* ([ext ".tox"] - [dlst (directory-list tox-path)] - [checker (λ (f) - (let ([name (path->string f)]) - (cond [(false? (string-contains-ci name ext)) #f] - [else f])))] - [filtered (filter checker dlst)]) - (command-line - #:usage-help - "Calling Blight without any arguments will start Blight with the defualt profile." - "Otherwise, please provide a valid profile for Blight to use." - "Giving Blight an optional number of files will have them be imported" - "as Tox profiles." - #:once-any - [("-p" "--profile") - pn ; takes one argument pn - "Specify the profile (by name) to use at startup. (Do not include a .tox extension.)" - "Use --list to see a list of available profiles." - ; given profile has no extension - (cond [(integer? (string-contains-ci pn ext)) - (displayln "Invalid profile entered! Reverting to default profile...")] - ; given profile is valid (if it doesn't exist, we'll just make it) - [else (profile-name pn) - (printf "db-file is now ~a~n" ((db-file))) - ((data-file)) - ((config-file))])] - [("-l" "--list") "List available Tox profiles to load." - (for-each (λ (f) - (let ([name (path->string f)]) - (displayln - (substring name 0 (- (string-length name) 4))))) - filtered) - (exit)] - #:args import-files - (unless (empty? import-files) - (for-each - (λ (x) - (let* ([fn (path->string (file-name-from-path x))] - [contains (string-contains-ci fn ext)] - [timestamp - (inexact->exact - (floor (current-inexact-milliseconds)))]) - (if (false? contains) - (copy-file x (build-path tox-path - (string-append fn timestamp ext))) - (copy-file x (build-path tox-path - (substring fn 0 contains)))))) - import-files)))) - -(printf "profile-name: ~a~ndb-file: ~a~ndata-file: ~a~nconfig-file: ~a~n" - (profile-name) ((db-file)) ((data-file)) ((config-file))) -(exit) - ; instantiate Tox session (define my-tox (tox-new #f)) @@ -129,15 +63,11 @@ val is a value that corresponds to the value of the key |# (define blight-save-config (λ (key val) - (let* ([profile (if (string=? (profile-name) "blight") - config-file - (build-path tox-path - (string-append (profile-name) ".json")))] - [new-input-port (open-input-file profile + (let* ([new-input-port (open-input-file ((config-file)) #:mode 'text)] [json (read-json new-input-port)] [modified-json (hash-set* json key val)] - [config-port-out (open-output-file profile + [config-port-out (open-output-file ((config-file)) #:mode 'text #:exists 'truncate/replace)]) (json-null 'null) @@ -155,17 +85,17 @@ val is a value that corresponds to the value of the key (define percent 0) ; percent of bytes sent ; data-file is empty, use default settings -(cond [(zero? (file-size (data-file))) +(cond [(zero? (file-size ((data-file)))) ; set username (set-name my-tox my-name) ; set status message (set-status-message my-tox my-status-message)] ; data-file is not empty, load from data-file - [(not (zero? (file-size (data-file)))) + [(not (zero? (file-size ((data-file))))) ; load the messenger from data of size length - (define size (file-size (data-file))) + (define size (file-size ((data-file)))) ; no conversions necessary because bytes-ref reports a decimal value - (define my-bytes (file->bytes (data-file) #:mode 'binary)) + (define my-bytes (file->bytes ((data-file)) #:mode 'binary)) (display "Loading from data file... ") (if (zero? (tox-load my-tox my-bytes size)) (displayln "Done!") @@ -217,7 +147,7 @@ val is a value that corresponds to the value of the key ; place all tox info into data-bytes (tox-save! my-tox data-bytes) ; SAVE INFORMATION TO DATA - (let ([data-port-out (open-output-file (data-file) + (let ([data-port-out (open-output-file ((data-file)) #:mode 'binary #:exists 'truncate/replace)]) (write-bytes data-bytes data-port-out) @@ -231,8 +161,6 @@ val is a value that corresponds to the value of the key (blight-save-data) ; disconnect from the database (disconnect sqlc) - ; close config file input port - (close-input-port config-port-in) ; kill tox thread (kill-thread tox-loop-thread) ; this kills the tox diff --git a/config.rkt b/config.rkt index 77fb578..0887815 100644 --- a/config.rkt +++ b/config.rkt @@ -1,7 +1,12 @@ #lang racket/base ; config.rkt ; contains default values for variables -(require json) +(require json + racket/bool + srfi/13 + racket/cmdline + racket/list + racket/path) (provide (all-defined-out)) ; base tox directory @@ -34,13 +39,13 @@ (λ ([profile (profile-name)]) (make-parameter (build-path tox-path - (string-append (profile-name) ".tox"))))) + (string-append profile ".tox"))))) ; blight-specific configurations (define config-file (λ ([profile (profile-name)]) (make-parameter (build-path tox-path - (string-append (profile-name) ".json"))))) + (string-append profile ".json"))))) ; location of sound directory (currently depends on running from same dir ; change to /usr/share/blight/sounds (or something) once a proper @@ -84,36 +89,122 @@ (unless (directory-exists? avatar-dir) (make-directory avatar-dir)) -; if blight-config.json does not exist, create it +#| +command-line stuff + +--profile: determine the profile to use +--list: list available profiles (by looking for .tox files) +arg: list of files to copy to tox-path as .tox files + +1. default profile is "blight" +2. all other files are determined by adding to profile-name + ex: (string-append (profile-name) ".json") + (string-append (profile-name) ".tox") + (string-append (profile-name) ".sqlite") +3. file parameters are curried, which means (db-file) returns a procedure + and to change it, you must run ((db-file )), but that is + only temporary as calling ((db-file)) again will change it back to + whatever (profile-name) is currently +|# +(let* ([ext ".tox"] + [dlst (directory-list tox-path)] + [checker (λ (f) + (let ([name (path->string f)]) + (cond [(false? (string-contains-ci name ext)) #f] + [else f])))] + [filtered (filter checker dlst)]) + (command-line + #:usage-help + "Calling Blight without any arguments will start Blight with the defualt profile." + "Otherwise, please provide a valid profile for Blight to use." + "Giving Blight an optional number of files will have them be imported" + "as Tox profiles." + #:once-any + [("-p" "--profile") + pn ; takes one argument pn + "Specify the profile (by name) to use at startup. (Do not include a .tox extension.)" + "Use --list to see a list of available profiles." + ; given profile has no extension + (cond [(integer? (string-contains-ci pn ext)) + (displayln "Invalid profile entered! Reverting to default profile...")] + ; given profile is valid (if it doesn't exist, we'll just make it) + [else (profile-name pn) + ((data-file)) + ((config-file)) + ((db-file))])] + [("-l" "--list") "List available Tox profiles to load." + (for-each (λ (f) + (let ([name (path->string f)]) + (displayln + (substring name 0 (- (string-length name) 4))))) + filtered) + (exit)] + #:args import-files + (unless (empty? import-files) + (for-each + (λ (x) + (let* ([fn (path->string (file-name-from-path x))] + [contains (string-contains-ci fn ext)] + [timestamp + (inexact->exact + (floor (current-inexact-milliseconds)))]) + (if (false? contains) + (copy-file x (build-path tox-path + (string-append fn timestamp ext))) + (copy-file x (build-path tox-path + (substring fn 0 contains)))))) + import-files)))) +#| ###################### END COMMAND-LINE STUFF ######################### |# + +#| ###################### OLD FILE IMPORTATION ########################## |# +; migrate old config file +(let ([old-config-file (build-path tox-path "blight-config.json")] + [new-config-file (build-path tox-path "blight.json")]) + (cond [(and (not (file-exists? new-config-file)) + (file-exists? old-config-file)) + (printf "Detected old config file ~a, copying to ~a... " + old-config-file new-config-file) + (copy-file old-config-file new-config-file) + (displayln "Done!")])) + +; migrate old data file +(let ([old-data-file (build-path tox-path "blight-data")] + [new-data-file (build-path tox-path "blight.tox")]) + ; if old data-file exists, but new one does not, + ; copy it over, otherwise do nothing + (cond [(and (not (file-exists? new-data-file)) + (file-exists? old-data-file)) + (printf "Detected old data file ~a, copying to ~a... " old-data-file new-data-file) + (copy-file old-data-file new-data-file) + (displayln "Done!")])) + +; migrate old history database +(let ([old-db-file (build-path tox-path "blight-tox.db")] + [new-db-file (build-path tox-path "blight.sqlite")]) + (cond [(and (not (file-exists? new-db-file)) + (file-exists? old-db-file)) + (printf "Detected old history database ~a, copying to ~a... " old-db-file new-db-file) + (copy-file old-db-file new-db-file) + (displayln "Done!")])) + #| ##################### END OLD FILE IMPORTATION ############################ |# + +; if .json does not exist, create it (unless (file-exists? ((config-file))) (define config-port-out - (open-output-file config-file + (open-output-file ((config-file)) #:mode 'text #:exists 'can-update)) (printf "~a created...\n" ((config-file))) (close-output-port config-port-out)) -; open blight-config.json -(define config-port-in (open-input-file ((config-file)) - #:mode 'text)) - -(let ([old-data-file (build-path tox-path "blight-data")]) - ; if old data-file exists, but new one does not, - ; copy it over, otherwise do nothing - (cond [(and (not (file-exists? ((data-file)))) - (file-exists? old-data-file)) - (printf "Detected old data file ~a, copying to ~a... " old-data-file ((data-file))) - (copy-file old-data-file ((data-file))) - (displayln "Done!")] - ; if neither exist, this must all be new, so create a fresh data-file - [(and (not (file-exists? ((data-file)))) - (not (file-exists? old-data-file))) - (define data-port-out +; if .tox does not exist, create it +(unless (file-exists? ((data-file))) + (define data-port-out (open-output-file ((data-file)) #:mode 'binary #:exists 'can-update)) (printf "~a created...~n" ((data-file))) - (close-output-port data-port-out)])) + (close-output-port data-port-out)) ; default name and status message ; if data exists, do no use these @@ -121,7 +212,7 @@ (define my-status-message-default "Toxing on Blight") ; default DHT settings -; if blight-config.json exists, do not use these +; if .json exists, do not use these (define dht-address-default "23.226.230.47") (define dht-port-default 33445) (define dht-public-key-default @@ -136,7 +227,7 @@ 'my-status-last my-status-message-default 'make-noise-last make-noise-default)) -; blight-config.json is empty, initialize with default values for variables +; .json is empty, initialize with default values for variables (unless (not (zero? (file-size ((config-file))))) (let ([config-port-out (open-output-file ((config-file)) #:mode 'text @@ -146,9 +237,12 @@ (write-json (json-null) config-port-out) (close-output-port config-port-out))) -; read from blight-config.json -(define json-info (read-json config-port-in)) -; set variables to values those contained in blight-config.json +; read from .json +(define json-info (let ([config-port-in + (open-input-file ((config-file)) + #:mode 'text)]) + (read-json config-port-in))) +; set variables to values those contained in .json (define dht-address (hash-ref json-info 'dht-address)) (define dht-port (hash-ref json-info 'dht-port)) (define dht-public-key (hash-ref json-info 'dht-public-key)) diff --git a/history.rkt b/history.rkt index fe3f31e..4d8a54f 100644 --- a/history.rkt +++ b/history.rkt @@ -9,16 +9,6 @@ disconnect get-history) -(printf "db-file is currently ~a~n" ((db-file))) -; copy old file scheme to follow new file scheme -(let ([old-db-file (build-path tox-path "blight-tox.db")]) - (cond [(and (not (file-exists? ((db-file)))) - (file-exists? old-db-file)) - (printf "Detected old history database ~a, copying to ~a... " old-db-file ((db-file))) - (copy-file old-db-file ((db-file))) - (displayln "Done!")])) - -(printf "db-file is currently ~a~n" ((db-file))) #| ############ BEGIN DATABASE STUFF ################ |# ; DATABASE DATABASE! JUST LIVING IN THE DATABASE! ; WOWOW