~ chicken-core (chicken-5) /chicken-uninstall.scm
Trap1;;;; chicken-uninstall.scm2;3; Copyright (c) 2008-2022, The CHICKEN Team4; All rights reserved.5;6; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following7; conditions are met:8;9; Redistributions of source code must retain the above copyright notice, this list of conditions and the following10; disclaimer.11; Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following12; 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 promote14; 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 EXPRESS17; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY18; AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR19; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR20; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR21; SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY22; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR23; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE24; POSSIBILITY OF SUCH DAMAGE.252627(module main ()2829 (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))4344(include "mini-srfi-1.scm")45(include "egg-environment.scm")46(include "egg-information.scm")4748(define host-extensions #t)49(define target-extensions #t)50(define force-uninstall #f)51(define sudo-uninstall #f)5253(define sudo-program (or (get-environment-variable "SUDO")54 "sudo"))5556(define (repo-path)57 (destination-repository58 (if (and cross-chicken (not host-extensions))59 'target60 'host)))6162(define (grep rx lst)63 (filter (cut irregex-search rx <>) lst))6465(define (gather-eggs patterns mtch)66 (let* ((eggs (map pathname-file67 (glob (make-pathname (repo-path) "*" +egg-info-extension+))))68 (pats (if mtch69 (concatenate70 (map (lambda (pat)71 (grep (irregex (glob->sre pat)) eggs))72 patterns))73 (filter74 (lambda (egg)75 (any (cut string=? <> egg) patterns))76 eggs))))77 (delete-duplicates pats string=?)))7879(define (fini code)80 (print "aborted.")81 (exit code))8283(define (ask eggs)84 (print (string-intersperse85 (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)))))))9899(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)))))))105106(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-each112 (lambda (f)113 (when (file-exists? f) (delete-installed-file f)))114 files)115 (delete-installed-file ifile)))116117(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 (else127 (delete-file fname))))128129(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-each135 (lambda (e)136 (print "removing " e)137 (remove-extension e))138 eggs)))))139140(define (usage code)141 (print #<<EOF142usage: chicken-uninstall [OPTION ...] [NAME ...]143144 -h -help show this message and exit145 -version show version and exit146 -force don't ask, delete whatever matches147 -match treat NAME as a glob pattern148 -s -sudo use external command to elevate privileges for deleting files149 -host when cross-compiling, uninstall host extensions only150 -target when cross-compiling, uninstall target extensions only151EOF152)153 (exit code))154155(define short-options '(#\h #\s #\p))156157(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 (else165 (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))))))))))198199(main (command-line-arguments))200201)