~ chicken-core (chicken-5) /chicken-uninstall.scm
Trap1;;;; chicken-uninstall.scm
2;
3; Copyright (c) 2008-2022, The CHICKEN Team
4; All rights reserved.
5;
6; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
7; conditions are met:
8;
9; Redistributions of source code must retain the above copyright notice, this list of conditions and the following
10; disclaimer.
11; Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
12; disclaimer in the documentation and/or other materials provided with the distribution.
13; Neither the name of the author nor the names of its contributors may be used to endorse or promote
14; products derived from this software without specific prior written permission.
15;
16; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
17; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18; AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24; POSSIBILITY OF SUCH DAMAGE.
25
26
27(module main ()
28
29 (import (scheme)
30 (chicken base)
31 (chicken file)
32 (chicken fixnum)
33 (chicken foreign)
34 (chicken format)
35 (chicken io)
36 (chicken irregex)
37 (chicken port)
38 (chicken pathname)
39 (chicken platform)
40 (chicken process)
41 (chicken process-context)
42 (chicken string))
43
44(include "mini-srfi-1.scm")
45(include "egg-environment.scm")
46(include "egg-information.scm")
47
48(define host-extensions #t)
49(define target-extensions #t)
50(define force-uninstall #f)
51(define sudo-uninstall #f)
52
53(define sudo-program (or (get-environment-variable "SUDO")
54 "sudo"))
55
56(define (repo-path)
57 (destination-repository
58 (if (and cross-chicken (not host-extensions))
59 'target
60 'host)))
61
62(define (grep rx lst)
63 (filter (cut irregex-search rx <>) lst))
64
65(define (gather-eggs patterns mtch)
66 (let* ((eggs (map pathname-file
67 (glob (make-pathname (repo-path) "*" +egg-info-extension+))))
68 (pats (if mtch
69 (concatenate
70 (map (lambda (pat)
71 (grep (irregex (glob->sre pat)) eggs))
72 patterns))
73 (filter
74 (lambda (egg)
75 (any (cut string=? <> egg) patterns))
76 eggs))))
77 (delete-duplicates pats string=?)))
78
79(define (fini code)
80 (print "aborted.")
81 (exit code))
82
83(define (ask eggs)
84 (print (string-intersperse
85 (append (list "About to delete the following extensions:\n\n")
86 (map (cut string-append " " <> "\n") eggs))
87 ""))
88 (let loop ()
89 (display "Do you want to proceed? (yes/no) ")
90 (flush-output)
91 (let ((a (read-line)))
92 (when (eof-object? a)
93 (error "EOF - use `-force' to proceed anyway"))
94 (let ((r (trim a)))
95 (cond ((string=? r "yes"))
96 ((string=? r "no") (fini 1))
97 (else (loop)))))))
98
99(define (trim str)
100 (define (left lst)
101 (cond ((null? lst) '())
102 ((char-whitespace? (car lst)) (left (cdr lst)))
103 (else (cons (car lst) (left (cdr lst))))))
104 (list->string (reverse (left (reverse (left (string->list str)))))))
105
106(define (remove-extension egg)
107 (and-let* ((ifile (file-exists?
108 (make-pathname (repo-path) egg +egg-info-extension+)))
109 (files (get-egg-property* (load-egg-info ifile)
110 'installed-files)))
111 (for-each
112 (lambda (f)
113 (when (file-exists? f) (delete-installed-file f)))
114 files)
115 (delete-installed-file ifile)))
116
117(define (delete-installed-file fname)
118 (cond ((not (file-exists? fname))
119 (warning "file does not exist" fname))
120 ((and sudo-uninstall (eq? 'unix default-platform))
121 (let ((r (system (string-append sudo-program " rm -f -r -- " (qs fname)))))
122 (unless (zero? r)
123 (warning "deleting file failed" fname))))
124 ((directory-exists? fname)
125 (delete-directory fname #t))
126 (else
127 (delete-file fname))))
128
129(define (uninstall pats mtch)
130 (let ((eggs (gather-eggs pats mtch)))
131 (cond ((null? eggs)
132 (print "nothing to remove.") )
133 ((or force-uninstall (ask eggs))
134 (for-each
135 (lambda (e)
136 (print "removing " e)
137 (remove-extension e))
138 eggs)))))
139
140(define (usage code)
141 (print #<<EOF
142usage: chicken-uninstall [OPTION ...] [NAME ...]
143
144 -h -help show this message and exit
145 -version show version and exit
146 -force don't ask, delete whatever matches
147 -match treat NAME as a glob pattern
148 -s -sudo use external command to elevate privileges for deleting files
149 -host when cross-compiling, uninstall host extensions only
150 -target when cross-compiling, uninstall target extensions only
151EOF
152)
153 (exit code))
154
155(define short-options '(#\h #\s #\p))
156
157(define (main args)
158 (let ((mtch #f))
159 (let loop ((args args) (pats '()))
160 (cond ((null? args)
161 (when (null? pats) (usage 1))
162 (validate-environment)
163 (uninstall (reverse pats) mtch))
164 (else
165 (let ((arg (car args)))
166 (cond ((or (string=? arg "-help")
167 (string=? arg "-h")
168 (string=? arg "--help"))
169 (usage 0))
170 ((string=? arg "-version")
171 (print (chicken-version))
172 (exit 0))
173 ((string=? arg "-target")
174 (set! host-extensions #f)
175 (loop (cdr args) pats))
176 ((string=? arg "-host")
177 (set! target-extensions #f)
178 (loop (cdr args) pats))
179 ((string=? arg "-force")
180 (set! force-uninstall #t)
181 (loop (cdr args) pats))
182 ((string=? arg "-match")
183 (set! mtch #t)
184 (loop (cdr args) pats))
185 ((or (string=? arg "-s") (string=? arg "-sudo"))
186 (set! sudo-uninstall #t)
187 (loop (cdr args) pats))
188 ((and (positive? (string-length arg))
189 (char=? #\- (string-ref arg 0)))
190 (if (> (string-length arg) 2)
191 (let ((sos (string->list (substring arg 1))))
192 (if (every (cut memq <> short-options) sos)
193 (loop (append (map (cut string #\- <>) sos)
194 (cdr args)) pats)
195 (usage 1)))
196 (usage 1)))
197 (else (loop (cdr args) (cons arg pats))))))))))
198
199(main (command-line-arguments))
200
201)