| 1 |
;;; |
| 2 |
;;; install - Generic installation utility |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2004-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
;; This is intended to replace 'install' program, in order to avoid |
| 35 |
;; variations of system's install program. Although most 'install' |
| 36 |
;; programs have various extensions, we can't reliably use those |
| 37 |
;; extended features since we don't know such extended install program |
| 38 |
;; is avilable on the target system. Assuming minimum featured install |
| 39 |
;; program makes makefile messy. |
| 40 |
|
| 41 |
(use file.util) |
| 42 |
(use gauche.parseopt) |
| 43 |
(use scheme.list) |
| 44 |
(use srfi.13) |
| 45 |
(use util.match) |
| 46 |
|
| 47 |
(define (error-exit fmtstr . args) |
| 48 |
(apply exit 1 #"gauche-install: ~fmtstr" args)) |
| 49 |
|
| 50 |
(define (p . args) (for-each print args)) |
| 51 |
|
| 52 |
(define (usage) |
| 53 |
(p "Usage: gauche-install [options] file dest (1st format*)" |
| 54 |
" gauche-install [options] file ... directory (2nd format*)" |
| 55 |
" gauche-install -d [options] directory ... (3rd format*)" |
| 56 |
" gauche-install -T directory [options] file ... (4th format)" |
| 57 |
" gauche-install -U directory [options] file ... (5th format)" |
| 58 |
"(*: 1st,2nd and 3rd format are compatible with BSD install)" |
| 59 |
"Options:" |
| 60 |
(option-parser-help-string) |
| 61 |
) |
| 62 |
(exit 0)) |
| 63 |
|
| 64 |
(define write-permission-message |
| 65 |
(cond-expand |
| 66 |
[gauche.os.windows |
| 67 |
"\nNote:\n\ |
| 68 |
Administrator privilege is required. If you want to force installation,\n\ |
| 69 |
right-click the shortcut of shell and select 'Run as administrator' menu,\n\ |
| 70 |
and then retry the command."] |
| 71 |
[else ""])) |
| 72 |
|
| 73 |
(define verbose (make-parameter #f)) |
| 74 |
(define dry-run (make-parameter #f)) |
| 75 |
|
| 76 |
(define-syntax do-it |
| 77 |
(syntax-rules () |
| 78 |
[(_ mesg . actions) |
| 79 |
(begin (when (and (verbose) mesg) (print mesg) (flush)) |
| 80 |
(unless (dry-run) . actions))])) |
| 81 |
|
| 82 |
(define check-write-permission |
| 83 |
(cond-expand |
| 84 |
[gauche.os.windows |
| 85 |
(define path-cache (make-hash-table 'equal?)) |
| 86 |
(^[path] |
| 87 |
(do-it #"check write permission of ~path" |
| 88 |
(unless (file-is-directory? path) |
| 89 |
(error-exit "non-directory file gets in my way: ~s" path)) |
| 90 |
(unless (hash-table-get path-cache path #f) |
| 91 |
(guard (e [else (error-exit "no write permission of ~s~a" |
| 92 |
path |
| 93 |
write-permission-message)]) |
| 94 |
(receive (out name) (sys-mkstemp (build-path path "checkwp")) |
| 95 |
(close-output-port out) |
| 96 |
(sys-unlink name) |
| 97 |
(hash-table-put! path-cache path #t))))))] |
| 98 |
[else |
| 99 |
(^[path])])) |
| 100 |
|
| 101 |
(define (ensure-directory path :optional (mode #f) (owner #f) (group #f)) |
| 102 |
(if (file-exists? path) |
| 103 |
(if (file-is-directory? path) |
| 104 |
(check-write-permission path) |
| 105 |
(error-exit "non-directory file gets in my way: ~s" path)) |
| 106 |
(do-it #"creating directory ~path" |
| 107 |
(guard (e [else (error-exit "can't create directory: ~s~a" |
| 108 |
(ref e 'message) |
| 109 |
write-permission-message)]) |
| 110 |
(make-directory* path) |
| 111 |
(when mode (sys-chmod path mode)) |
| 112 |
(when (or owner group) |
| 113 |
(sys-chown path |
| 114 |
(->ugid sys-user-name->uid owner "user") |
| 115 |
(->ugid sys-group-name->gid group "group")))) |
| 116 |
))) |
| 117 |
|
| 118 |
;; user/group -> uid/gid |
| 119 |
(define (->ugid str->id arg type) |
| 120 |
(cond [(not arg) -1] |
| 121 |
[(integer? arg) arg] |
| 122 |
[(and (string? arg) (str->id arg))] |
| 123 |
[else (error-exit "bad ~a name: ~a" type arg)])) |
| 124 |
|
| 125 |
;; find source path |
| 126 |
(define (ensure-src file srcdir) |
| 127 |
(or (and-let* ([ srcdir ] |
| 128 |
[srcpath (build-path srcdir file)] |
| 129 |
[ (file-exists? srcpath) ]) |
| 130 |
srcpath) |
| 131 |
file)) |
| 132 |
|
| 133 |
;; copy, possibly with appending prelude |
| 134 |
(define (cp src dest prelude) |
| 135 |
(if prelude |
| 136 |
(receive (out name) (sys-mkstemp src) |
| 137 |
(display prelude out) |
| 138 |
(call-with-input-file src (cut copy-port <> out :unit 65536)) |
| 139 |
(close-output-port out) |
| 140 |
(move-file name dest :if-exists :supersede)) |
| 141 |
(copy-file src dest :if-exists :supersede :safe #t))) |
| 142 |
|
| 143 |
;; standard install |
| 144 |
(define (install src dest prelude mode owner group canonical?) |
| 145 |
(ensure-directory (sys-dirname dest)) |
| 146 |
(do-it #"installing ~src to ~dest" |
| 147 |
(and (cp src dest prelude) |
| 148 |
(begin |
| 149 |
(sys-chmod dest mode) |
| 150 |
(when (or owner group) |
| 151 |
(sys-chown dest |
| 152 |
(->ugid sys-user-name->uid owner "user") |
| 153 |
(->ugid sys-group-name->gid group "group"))) |
| 154 |
(when (and canonical? (string-suffix? ".sci" dest)) |
| 155 |
(sys-rename dest (path-swap-extension dest "scm"))))))) |
| 156 |
|
| 157 |
;; strip PREFIX from path if possible. |
| 158 |
(define (strip-dir prefix path) |
| 159 |
(cond |
| 160 |
[(not prefix) path] |
| 161 |
[(eq? prefix #t) (sys-basename path)] |
| 162 |
[else |
| 163 |
(let1 pre (if (#/[\/\\]$/ prefix) prefix (string-append prefix "/")) |
| 164 |
(if (string-prefix? pre path) |
| 165 |
(string-drop path (string-length pre)) |
| 166 |
path))])) |
| 167 |
|
| 168 |
;; Entry point |
| 169 |
(define (main args) |
| 170 |
(let-args (cdr args) |
| 171 |
([mkdir "d|directory" |
| 172 |
? "Creates directories listed in the arguments. \ |
| 173 |
(3rd format only)."] |
| 174 |
[mode "m|mode=s{MODE}" #o755 => (cut string->number <> 8) |
| 175 |
? "Change mode of the installed file."] |
| 176 |
[owner "o|owner=s{OWNER}" |
| 177 |
? "Change owner of the installed file(s)."] |
| 178 |
[group "g|group=s{GROUP}" |
| 179 |
? "Change group of the installed file(s)."] |
| 180 |
[csfx "C|canonical-suffix" |
| 181 |
? "If installed file has a suffix *.sci, replace it for \ |
| 182 |
*.scm. This is Gauche specific convention."] |
| 183 |
[srcdir "S|srcdir=s{DIR}" |
| 184 |
? "Look for files within {DIR}; useful if VPATH is used."] |
| 185 |
[target "T|target=s{DIR}" |
| 186 |
? "Installs files to the {DIR}, creating paths if needed. \ |
| 187 |
Partial path of files are preserved. (4th format only)."] |
| 188 |
[utarget "U|uninstall=s{DIR}" |
| 189 |
? "Reverse of -T, e.g. removes files from its destination."] |
| 190 |
[shebang "shebang=s{PATH}" |
| 191 |
? "Adds #!{PATH} before the file contents. \ |
| 192 |
Useful to install scripts."] |
| 193 |
[verb "v|verbose" ? "Work verbosely"] |
| 194 |
[dry "n|dry-run" ? "Just prints what actions to be done."] |
| 195 |
[sprefix "p|strip-prefix=s{PREFIX}" |
| 196 |
? "Strip prefix dirs from FILEs before \ |
| 197 |
installation. (4th/5th format only)."] |
| 198 |
[#f "h|help" => usage ? "Show this help message."] |
| 199 |
[#f "c" ? "This option is ignored. Recognized for the \ |
| 200 |
compatibility."] |
| 201 |
[else (opt . _) (print "Unknown option : " opt) (usage)] |
| 202 |
. args) |
| 203 |
|
| 204 |
(parameterize ([verbose (or verb dry)] |
| 205 |
[dry-run dry]) |
| 206 |
(when shebang (set! shebang #"#!~shebang\n")) |
| 207 |
(cond |
| 208 |
[mkdir (for-each (cut ensure-directory <> mode owner group) args)] |
| 209 |
[target (dolist [src args] |
| 210 |
(install (ensure-src src srcdir) |
| 211 |
(build-path target (strip-dir sprefix src)) |
| 212 |
shebang mode owner group csfx))] |
| 213 |
[utarget (guard (e [else (error-exit "can't remove file: ~s~a" |
| 214 |
(ref e 'message) |
| 215 |
write-permission-message)]) |
| 216 |
(dolist [src args] |
| 217 |
(sys-unlink (build-path utarget (strip-dir sprefix src)))))] |
| 218 |
[else |
| 219 |
(match args |
| 220 |
[() (usage)] |
| 221 |
[(_) #f] ; no op |
| 222 |
[(src dst) ; file to file or file to dir |
| 223 |
(install (ensure-src src srcdir) |
| 224 |
(if (file-is-directory? (cadr args)) |
| 225 |
(build-path (cadr args) (sys-basename src)) |
| 226 |
(cadr args)) |
| 227 |
shebang mode owner group csfx)] |
| 228 |
[(args ...) |
| 229 |
(let1 target (car (last-pair args)) |
| 230 |
(dolist [src (drop-right args 1)] |
| 231 |
(install (ensure-src src srcdir) |
| 232 |
(build-path target (sys-basename src)) |
| 233 |
shebang mode owner group csfx)))] |
| 234 |
)]))) |
| 235 |
0) |
| 236 |
|
| 237 |
;; Local variables: |
| 238 |
;; mode: scheme |
| 239 |
;; end: |