~ chicken-core (master) /chicken-status.scm


  1;;;; chicken-status.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(module main ()
 27
 28  (import (scheme)
 29	  (chicken base)
 30	  (chicken condition)
 31	  (chicken file)
 32	  (chicken fixnum)
 33	  (chicken foreign)
 34	  (chicken format)
 35	  (chicken irregex)
 36	  (chicken port)
 37	  (chicken pathname)
 38	  (chicken platform)
 39	  (chicken pretty-print)
 40	  (chicken process-context)
 41	  (chicken sort)
 42	  (only (chicken string) ->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
 51  (define get-terminal-width
 52    (let ((default-width 79))	     ; Standard default terminal width
 53      (lambda ()
 54	(let ((cop (current-output-port)))
 55	  (if (terminal-port? cop)
 56	      (let ((w (handle-exceptions exn 0 (nth-value 1 (terminal-size cop)))))
 57		(if (zero? w)
 58		    default-width
 59		    (min default-width w)))
 60	      default-width)))))
 61
 62  (define list-width (quotient (- (get-terminal-width) 2) 2))
 63
 64  (define (repo-path)
 65    (if (and cross-chicken (not host-extensions))
 66	(##sys#split-path (destination-repository 'target))
 67	(repository-path)))
 68
 69  (define (grep rx lst)
 70    (filter (cut irregex-search rx <>) lst))
 71
 72  (define (read-info egg #!optional (dir (repo-path)) (ext +egg-info-extension+))
 73    (let ((f (chicken.load#find-file (make-pathname #f egg ext) dir)))
 74      (and f (load-egg-info f))))
 75
 76  (define (filter-egg-names eggs patterns mtch)
 77    (let* ((names (cond ((null? patterns) eggs)
 78                        (mtch
 79                         (concatenate
 80                           (map (lambda (pat)
 81                                  (grep (irregex (glob->sre pat)) eggs))
 82                             patterns)))
 83                        (else 
 84                          (filter 
 85                            (lambda (egg)
 86                              (any (cut string=? <> egg) patterns))
 87                            eggs)))))
 88      (delete-duplicates names string=?)))
 89
 90  (define (gather-eggs)
 91    (delete-duplicates
 92      (append-map
 93        (lambda (dir)
 94          (map pathname-file 
 95            (glob (make-pathname dir "*" +egg-info-extension+))))
 96        (repo-path))
 97      equal?))
 98
 99  (define (format-string str cols #!optional right (padc #\space))
100    (let* ((len (string-length str))
101	   (pad (make-string (fxmax 0 (fx- cols len)) padc)) )
102      (if right
103	  (string-append pad str)
104	  (string-append str pad) ) ) )
105
106  (define (list-installed-eggs eggs #!optional (dir (repo-path))
107			       (ext +egg-info-extension+))
108    (for-each (cut list-egg-info <> dir ext)
109      (sort eggs string<?)))
110
111  (define (list-egg-info egg dir ext)
112    (let ((version
113	   (or (let ((info (read-info egg dir ext)))
114		 (and info (get-egg-property info 'version)))
115               (let ((file (file-exists?
116                            (make-pathname (list cache-metadata-directory egg)
117                                           +version-file+))))
118		 (and file (with-input-from-file file read)))
119	       "unknown")))
120      (print (format-string (string-append egg " ")
121			    list-width #f #\.)
122	     (format-string (string-append " version: "
123					   (->string version))
124			    list-width #t #\.))))
125
126  (define (list-cached-eggs pats mtch)
127    (when (directory-exists? cache-directory)
128      (for-each
129       (lambda (egg)
130	 (list-egg-info egg (make-pathname cache-directory egg) +egg-extension+))
131       (sort (filter-egg-names (directory cache-directory) pats mtch) string<?))))
132
133  (define (gather-components lst mode)
134    (append-map (cut gather-components-rec <> mode) lst))
135
136  (define (gather-components-rec info mode)
137    (case (car info)
138      ((host)
139       (if host-extensions (gather-components (cdr info) 'host) '()))
140      ((target)
141       (if target-extensions (gather-components (cdr info) 'target) '()))
142      ((extension) (list (list 'extension mode (cadr info))))
143      ((data) (list (list 'data mode (cadr info))))
144      ((generated-source-file) (list (list 'generated-source-file mode (cadr info))))
145      ((c-include) (list (list 'c-include mode (cadr info))))
146      ((scheme-include) (list (list 'scheme-include mode (cadr info))))
147      ((program) (list (list 'program mode (cadr info))))
148      ((c-object) (list (list 'c-object mode (cadr info))))
149      ((installed-c-object) (list (list 'installed-c-object mode (cadr info))))
150      (else '())))
151
152  (define (list-installed-components eggs)
153    (let ((w (quotient (- (get-terminal-width) 2) 2)))
154      (for-each
155        (lambda (egg)
156          (let* ((info (read-info egg))
157                 (version (get-egg-property info 'version))
158                 (comps (get-egg-property* info 'components)))
159            (if version
160                (print (format-string (string-append egg " ") w #f #\.)
161                       (format-string (string-append " version: "
162                                                     (->string version))
163                                      w #t #\.))
164                (print egg))
165            (when comps
166              (let ((lst (gather-components comps #f)))
167                (for-each
168                  (lambda (comp)
169                    (print "  " (format-string (->string (car comp)) 32)
170                           "  " (format-string (->string (caddr comp)) 32)
171                           (case (cadr comp)
172                             ((host) " (host)")
173                             ((target) " (target)")
174                             (else ""))))
175                  lst)))))
176        eggs)))
177
178  (define (list-installed-files eggs)
179    (for-each
180     print
181     (sort
182      (append-map
183       (lambda (egg)
184	 (get-egg-property* (read-info egg) 'installed-files))
185       eggs)
186      string<?)))
187
188  (define (dump-installed-versions eggs)
189    (for-each
190     (lambda (egg)
191       (let ((version (get-egg-property (read-info egg) 'version)))
192	 (pp (cons (string->symbol egg)
193                   (if version (list version) '())))))
194     eggs))
195
196  (define (usage code)
197    (print #<<EOF
198usage: chicken-status [OPTION ...] [NAME ...]
199
200  -h   -help                    show this message
201       -version                 show version and exit
202  -c   -components              list installed components
203       -cached                  list eggs in cache
204  -f   -files                   list installed files
205       -list                    dump installed extensions and their versions in "override" format
206       -match                   treat NAME as glob pattern
207       -host                    when cross-compiling, only show host extensions
208       -target                  when cross-compiling, only show target extensions
209EOF
210);|
211    (exit code))
212
213  (define short-options '(#\h #\f #\c #\a))
214
215  (define (main args)
216    (let ((files #f)
217          (comps #f)
218          (dump #f)
219          (cached #f)
220          (mtch #f))
221      (let loop ((args args) (pats '()))
222        (if (null? args)
223            (cond ((and comps (or dump files))
224                   (with-output-to-port (current-error-port)
225                     (cut print "-components cannot be used with -list."))
226                   (exit 1))
227		  (cached (list-cached-eggs pats mtch))
228		  (else
229		   ((cond (dump dump-installed-versions)
230			  (files list-installed-files)
231			  (comps list-installed-components)
232			  (else list-installed-eggs))
233		    (filter-egg-names (gather-eggs) pats mtch))))
234            (let ((arg (car args)))
235              (cond ((member arg '("-help" "-h" "--help"))
236                     (usage 0))
237		    ((string=? arg "-host")
238		     (set! target-extensions #f)
239		     (loop (cdr args) pats))
240		    ((string=? arg "-target")
241		     (set! host-extensions #f)
242		     (loop (cdr args) pats))
243                    ((string=? arg "-match")
244                     (set! mtch #t)
245                     (loop (cdr args) pats))
246                    ((string=? arg "-cached")
247                     (set! cached #t)
248                     (loop (cdr args) pats))
249		    ((string=? arg "-list")
250		     (set! dump #t)
251		     (loop (cdr args) pats))
252		    ((or (string=? arg "-f") (string=? arg "-files"))
253		     (set! files #t)
254		     (loop (cdr args) pats))
255		    ((or (string=? arg "-c") (string=? arg "-components"))
256		     (set! comps #t)
257		     (loop (cdr args) pats))
258		    ((string=? arg "-version")
259		     (print (chicken-version))
260		     (exit 0))
261                    ((and (positive? (string-length arg))
262                          (char=? #\- (string-ref arg 0)))
263                     (if (> (string-length arg) 2)
264                         (let ((sos (string->list (substring arg 1))))
265                           (if (every (cut memq <> short-options) sos)
266                               (loop (append (map (cut string #\- <>) sos)
267                                             (cdr args)) pats)
268                               (usage 1)))
269                         (usage 1)))
270                    (else (loop (cdr args) (cons arg pats)))))))))
271
272  (main (command-line-arguments))
273  
274 )
Trap