Megatest

Diff
Login

Differences From Artifact [6c415a23c3]:

To Artifact [d4b0a42397]:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52




53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

;; Copyright 2006-2013, Matthew Welland.
;; 
;;  This program is made available under the GNU GPL version 2.0 or
;;  greater. See the accompanying file COPYING for details.
;; 
;;  This program is distributed WITHOUT ANY WARRANTY; without even the
;;  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;;  PURPOSE.

(use defstruct)
(use scsh-process)

;; (use ssax)
;; (use sxml-serializer)
;; (use sxml-modifications)
;; (use regex)
;; (use srfi-69)
;; (use regex-case)
;; (use posix)
;; (use json)
;; (use csv)
;; (use directory-utils)
(use srfi-18)
(use srfi-19)
;;(use utils)
;;(use format)
(use refdb)
;; (require-library ini-file)
;; (import (prefix ini-file ini:))

(use sql-de-lite srfi-1 posix regex regex-case srfi-69)
;; (import (prefix sqlite3 sqlite3:))
;; 
(declare (uses common))

(declare (uses configf))
;; (declare (uses tree))
(declare (uses margs))
;; (declare (uses dcommon))
;; (declare (uses launch))
;; (declare (uses gutils))
;; (declare (uses db))
;; (declare (uses synchash))
;; (declare (uses server))
(declare (uses megatest-version))
;; (declare (uses tbd))

(include "megatest-fossil-hash.scm")
;;; please create this file before using sautherise. For sample file is avaliable sample-sauth-paths.scm. 
(include "sauth-paths.scm")
(include "sauth-common.scm")





;;
;; GLOBALS
;;
(define *verbosity* 1)
(define *logging* #f)
(define *exe-name* (pathname-file (car (argv))))
(define *sretrieve:current-tab-number* 0)
(define *args-hash* (make-hash-table))
(define sretrieve:help (conc "Usage: " *exe-name* " [action [params ...]]

  ls                     : list contents of target area
  get <relversion>       : retrieve data for release <version>
    -m \"message\"       : why retrieved?
  cp <relative path>     : copy file to current directory 
  log                    : get listing of recent downloads
  shell                  : start a shell-like interface

Part of the Megatest tool suite.
Learn more at http://www.kiatoa.com/fossils/megatest

Version: " megatest-fossil-hash)) ;; "

;;======================================================================













<
<
<
<
<
<
<
<
<
<














<

<
<
<
<
<
<

|





>
>
>
>











|
|
|
<
<
|







1
2
3
4
5
6
7
8
9
10
11
12
13










14
15
16
17
18
19
20
21
22
23
24
25
26
27

28






29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53


54
55
56
57
58
59
60
61

;; Copyright 2006-2013, Matthew Welland.
;; 
;;  This program is made available under the GNU GPL version 2.0 or
;;  greater. See the accompanying file COPYING for details.
;; 
;;  This program is distributed WITHOUT ANY WARRANTY; without even the
;;  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;;  PURPOSE.

(use defstruct)
(use scsh-process)











(use srfi-18)
(use srfi-19)
;;(use utils)
;;(use format)
(use refdb)
;; (require-library ini-file)
;; (import (prefix ini-file ini:))

(use sql-de-lite srfi-1 posix regex regex-case srfi-69)
;; (import (prefix sqlite3 sqlite3:))
;; 
(declare (uses common))

(declare (uses configf))

(declare (uses margs))






(declare (uses megatest-version))
 

(include "megatest-fossil-hash.scm")
;;; please create this file before using sautherise. For sample file is avaliable sample-sauth-paths.scm. 
(include "sauth-paths.scm")
(include "sauth-common.scm")

(define (toplevel-command . args) #f)
(use readline)


;;
;; GLOBALS
;;
(define *verbosity* 1)
(define *logging* #f)
(define *exe-name* (pathname-file (car (argv))))
(define *sretrieve:current-tab-number* 0)
(define *args-hash* (make-hash-table))
(define sretrieve:help (conc "Usage: " *exe-name* " [action [params ...]]

  ls   <area>                        : list contents of target area
  get  <area>  <reletive path>       : retrieve path to the data within <area>
     -m \"message\"       : why retrieved?


  shell  <area>                   : start a shell-like interface

Part of the Megatest tool suite.
Learn more at http://www.kiatoa.com/fossils/megatest

Version: " megatest-fossil-hash)) ;; "

;;======================================================================
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
;; (call-with-database
;;  (lambda (db)
;;   (set-busy-handler! db (busy-timeout 10000)) ; 10 second timeout
;;   ...))

;; Create the sqlite db
(define (sretrieve:db-do configdat proc) 

  (let ((path (configf:lookup configdat "database" "location")))
    (if (not path)
	(begin
	  (debug:print 0 "[database]\nlocation /some/path\n\n Is missing from the config file!")
	  (exit 1)))
    (if (and path
	     (directory? path)







<







100
101
102
103
104
105
106

107
108
109
110
111
112
113
;; (call-with-database
;;  (lambda (db)
;;   (set-busy-handler! db (busy-timeout 10000)) ; 10 second timeout
;;   ...))

;; Create the sqlite db
(define (sretrieve:db-do configdat proc) 

  (let ((path (configf:lookup configdat "database" "location")))
    (if (not path)
	(begin
	  (debug:print 0 "[database]\nlocation /some/path\n\n Is missing from the config file!")
	  (exit 1)))
    (if (and path
	     (directory? path)
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
	     (process-execute "/bin/tar" (append (append (list  "chfv" "-") files) (list "--ignore-failed-read")))))
             (begin
               (let* ((parent-dir (pathname-directory datadir) )
                      (filename  (conc(pathname-file datadir) "." (pathname-extension datadir))))
                  (change-directory parent-dir)  
                  (process-execute "/bin/tar" (list "chfv" "-" filename))
             )))
))
))


;; copy in file to dest, validation is done BEFORE calling this
;;
(define (sretrieve:cp configdat retriever file comment)
  (let* ((base-dir  (configf:lookup configdat "settings" "base-dir"))
         (allowed-sub-paths (configf:lookup configdat "settings" "allowed-sub-paths"))    







|
<







163
164
165
166
167
168
169
170

171
172
173
174
175
176
177
	     (process-execute "/bin/tar" (append (append (list  "chfv" "-") files) (list "--ignore-failed-read")))))
             (begin
               (let* ((parent-dir (pathname-directory datadir) )
                      (filename  (conc(pathname-file datadir) "." (pathname-extension datadir))))
                  (change-directory parent-dir)  
                  (process-execute "/bin/tar" (list "chfv" "-" filename))
             )))
))))



;; copy in file to dest, validation is done BEFORE calling this
;;
(define (sretrieve:cp configdat retriever file comment)
  (let* ((base-dir  (configf:lookup configdat "settings" "base-dir"))
         (allowed-sub-paths (configf:lookup configdat "settings" "allowed-sub-paths"))    
396
397
398
399
400
401
402
403


404
405
406
407
408
409
410
      (print (string-intersperse top-areas " "))
  (let* ((resolved-path (sauth-common:resolve-path ext-path base-path-list top-areas )))
           ;(print resolved-path)
           (if (not (equal? resolved-path #f))
           (if (null? resolved-path) 
             (print (string-intersperse top-areas " "))
           (let* ((target-path (sauth-common:get-target-path  base-path-list  ext-path top-areas base-path)))
                (print "Resolved path: " target-path)


                (if (not (equal? target-path #f))
                (begin 
                (cond
		  ((null? tail-cmd-list)
		     (run (pipe
      	      	      (ls "-lrt" ,target-path))))
		  ((not (equal? (car tail-cmd-list) "|"))







|
>
>







379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
      (print (string-intersperse top-areas " "))
  (let* ((resolved-path (sauth-common:resolve-path ext-path base-path-list top-areas )))
           ;(print resolved-path)
           (if (not (equal? resolved-path #f))
           (if (null? resolved-path) 
             (print (string-intersperse top-areas " "))
           (let* ((target-path (sauth-common:get-target-path  base-path-list  ext-path top-areas base-path)))
                ;(print "Resolved path: " target-path)
                (if (symbolic-link? target-path)
                   (set! target-path (conc target-path "/"))) 
                (if (not (equal? target-path #f))
                (begin 
                (cond
		  ((null? tail-cmd-list)
		     (run (pipe
      	      	      (ls "-lrt" ,target-path))))
		  ((not (equal? (car tail-cmd-list) "|"))
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561

562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577








578
579
580
581

582
583
584
585
586
587
588









589
590
591
592

593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618













619





























































620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
  (if (null? restriction-list)
     ""
   (let loop ((hed (car restriction-list))
		 (tal (cdr restriction-list))
                  (ret-str ""))
                   (cond
                   ((null? tal)
                      (conc ret-str " --exclude='*" hed "*'")) 
                   (else 
		  	(loop (car tal)(cdr tal)(conc ret-str " --exclude='*" hed "*'"))))))    )

(define (sretrieve:get-shell-cmd target-path base-path restrictions iport)
     (if (not (file-exists? target-path))
        (print "Target path does not exist!")
    (begin
    (if (not (equal? target-path #f))
    (begin     
        (if (is_directory target-path) 
        (begin

           (let* ((parent-dir target-path)
                  (last-dir-name (if  (pathname-extension target-path)  
                                      (conc(pathname-file target-path) "." (pathname-extension target-path))
                                      (pathname-file target-path)))
                  (curr-dir (current-directory))   
                  (start-dir (conc (current-directory) "/" last-dir-name))
                  (execlude (make-exclude-pattern (string-split restrictions ","))))
                   (print execlude) 
                   (print (file-exists? start-dir))
                  (if  (file-exists? start-dir)
                    (begin
                         (print last-dir-name " already exist in your work dir. Do you want to over write it? [y|n]")
                        (let* ((inl (read-line iport)))
                            (if (equal? inl "y")
                              (begin
                                 (change-directory parent-dir)








                                  (run (pipe
                   		   (tar "chfv" "-" "." )
                   		   (begin (system (conc "cd " start-dir ";tar  xUf - "   execlude )))))
                                   (change-directory curr-dir))

			      (begin	
                               (print  "Nothing has been retrieved!!  ")))))
                     (begin
                       (sretrieve:do-as-calling-user
                    (lambda ()
		      (create-directory start-dir #t)))
                          (change-directory parent-dir)









                                  (run (pipe
                   		   (tar "chfv" "-" "." )
                   		   (begin (system (conc "cd " start-dir ";tar  xUf - "   execlude )))))
                           (change-directory curr-dir))))) 

        (begin
           (let*((parent-dir (pathname-directory target-path))
                 (start-dir (current-directory))
                 (filename (if  (pathname-extension target-path)  
                                      (conc(pathname-file target-path) "." (pathname-extension target-path))
                                      (pathname-file target-path)))
                 (work-dir-file (conc (current-directory) "/" filename)))
                 (if  (file-exists? work-dir-file)
                    (begin
                       (print filename " already exist in your work dir. Do you want to over write it? [y|n]")
                        (let* ((inl (read-line iport)))
                            (if (equal? inl "y")
                              (begin
                                 (change-directory parent-dir)
                                  (run (pipe
                   		   (tar "chfv" "-" "." )
                   		   (begin (system (conc "cd " start-dir ";tar  xUf - "   execlude )))))
                                     (change-directory curr-dir))
			      (begin	
                               (print  "Nothing has been retrieved!!  ")))))
                    (begin
               (change-directory parent-dir)  
                 (run (pipe
                   (tar "chfv" "-" ,filename)
                   (begin (system (conc "cd " start-dir ";tar xUf -")))))
                    (change-directory start-dir))))))))













                     (print (current-directory)))))






























































(define (sretrieve:shell-help)
(conc "Usage: " *exe-name* " [action [params ...]]

  ls    [target path]               	  : list contents of target area. The output of the cmd can be piped into other system cmd. eg ls <path> | grep txt
  cd    <target directory>	     	  : To change the current directory within the sretrive shell. 
  pwd				     	  : Prints the full pathname of the current directory within the sretrive shell.
  get   <file or directory path>     	  : download directory/files into the directory where sretrieve shell cmd was invoked   
  less  <file path>		     	  : Read input file to allows backward movement in the file as well as forward movement 
  cat   <file path>                  	  : show the contents of a file. The output of the cmd can be piped into other system cmd.

  sgrep <search path> <pattern> [options] : Similar to unix grep cmd But with diffrent parameter ordering. The output of the cmd can be piped into other system cmd. 
Part of the Megatest tool suite.
Learn more at http://www.kiatoa.com/fossils/megatest

Version: " megatest-fossil-hash)
)	
(define (toplevel-command . args) #f)
(define (sretrieve:shell area)
 ; (print area)
  (use readline)
  (let* ((path      '())
	 (prompt    "sretrieve> ")
	 (args      (argv))
         (usr (current-user-name) )   







|

|









>
|






|
<
|






>
>
>
>
>
>
>
>

|
|
|
>







>
>
>
>
>
>
>
>
>

|
|
|
>















|
|
|







|
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

















|







528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555

556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
  (if (null? restriction-list)
     ""
   (let loop ((hed (car restriction-list))
		 (tal (cdr restriction-list))
                  (ret-str ""))
                   (cond
                   ((null? tal)
                      (conc ret-str ".+" hed ".*")) 
                   (else 
		  	(loop (car tal)(cdr tal)(conc ret-str ".+" hed ".*|"))))))    )

(define (sretrieve:get-shell-cmd target-path base-path restrictions iport)
     (if (not (file-exists? target-path))
        (print "Target path does not exist!")
    (begin
    (if (not (equal? target-path #f))
    (begin     
        (if (is_directory target-path) 
        (begin
           (let* ((tmpfile (conc "/tmp/" (current-user-name) "/my-pipe"))
                  (parent-dir target-path)
                  (last-dir-name (if  (pathname-extension target-path)  
                                      (conc(pathname-file target-path) "." (pathname-extension target-path))
                                      (pathname-file target-path)))
                  (curr-dir (current-directory))   
                  (start-dir (conc (current-directory) "/" last-dir-name))
                  (execlude (make-exclude-pattern (string-split restrictions ","))))
                  ; (print tmpfile)

                    (if  (file-exists? start-dir)
                    (begin
                         (print last-dir-name " already exist in your work dir. Do you want to over write it? [y|n]")
                        (let* ((inl (read-line iport)))
                            (if (equal? inl "y")
                              (begin
                                 (change-directory parent-dir)
                                  (create-fifo  tmpfile)
                                  (process-fork 
    				   (lambda()
                                       (sleep 1) 
       					(with-output-to-file tmpfile
         				(lambda ()
            				(sretrieve:make_file parent-dir execlude parent-dir)))))
 
                                  (run (pipe
                   		   (tar "chfv" "-" "-T" ,tmpfile )
                   		   (begin (system (conc "cd " start-dir ";tar  xUf - "   )))))
                                   (change-directory curr-dir)
                                    (system (conc "rm " tmpfile)) )
			      (begin	
                               (print  "Nothing has been retrieved!!  ")))))
                     (begin
                       (sretrieve:do-as-calling-user
                    (lambda ()
		      (create-directory start-dir #t)))
                          (change-directory parent-dir)
                          ; (print execlude)
                           (create-fifo tmpfile)
                            (process-fork 
    				   (lambda()
                                       (sleep 1) 
       					(with-output-to-file tmpfile
         				(lambda ()
            				(sretrieve:make_file parent-dir execlude parent-dir)))))

                                  (run (pipe
                   		   (tar "chfv" "-"  "-T" ,tmpfile)
                   		   (begin (system (conc "cd " start-dir ";tar  xUf - "    )))))
                           (change-directory curr-dir)
                            (system (conc "rm " tmpfile)))))) 
        (begin
           (let*((parent-dir (pathname-directory target-path))
                 (start-dir (current-directory))
                 (filename (if  (pathname-extension target-path)  
                                      (conc(pathname-file target-path) "." (pathname-extension target-path))
                                      (pathname-file target-path)))
                 (work-dir-file (conc (current-directory) "/" filename)))
                 (if  (file-exists? work-dir-file)
                    (begin
                       (print filename " already exist in your work dir. Do you want to over write it? [y|n]")
                        (let* ((inl (read-line iport)))
                            (if (equal? inl "y")
                              (begin
                                 (change-directory parent-dir)
                                  (run (pipe
                   		   (tar "chfv" "-" ,filename)
                   		   (begin (system (conc "cd " start-dir ";tar  xUf - "   )))))
                                     (change-directory start-dir))
			      (begin	
                               (print  "Nothing has been retrieved!!  ")))))
                    (begin
               (change-directory parent-dir)  
                 (run (pipe
                   (tar "chfv" "-" ,filename)
                   (begin (system (conc "cd " start-dir ";tar xUf -")))))
                    (change-directory start-dir)))))))))))

(define (sretrieve:get-shell-cmd-line target-path base-path restrictions iport)
     (if (not (file-exists? target-path))
        (print "Target path does not exist!")
    (begin
    (if (not (equal? target-path #f))
    (begin     
        (if (is_directory target-path) 
        (begin
           (let* ((parent-dir target-path)
                  (last-dir-name (if  (pathname-extension target-path)  
                                      (conc(pathname-file target-path) "." (pathname-extension target-path))
                                      (pathname-file target-path)))
                  (curr-dir (current-directory))   
                  (start-dir (conc (current-directory) "/" last-dir-name))
                  (execlude (make-exclude-pattern (string-split restrictions ",")))
                   (tmpfile (conc "/tmp/" (current-user-name) "/my-pipe-" (current-process-id))))
                    (if  (file-exists? start-dir)
                    (begin
                         (print last-dir-name " already exist in your work dir.")
                         (print  "Nothing has been retrieved!!  "))
                     (begin
                   ;    (sretrieve:do-as-calling-user
                   ; (lambda ()
		      ;(create-directory start-dir #t)))
                          (change-directory parent-dir)
                            (create-fifo  tmpfile)
                                  (process-fork 
    				   (lambda()
                                       (sleep 1) 
       					(with-output-to-file tmpfile
         				(lambda ()
            				(sretrieve:make_file parent-dir execlude parent-dir)))))

                           (process-execute "/bin/tar" (append (list  "chfv" "-"  "-T" tmpfile)  (list "--ignore-failed-read")))    
                                  ;(run (pipe
                   		   ;(tar "chfv" "-" "." )
                   		   ;(begin (system (conc "cd " start-dir ";tar  xUf - "   execlude )))))
                            (system (conc "rm " tmpfile))    
                           (change-directory curr-dir))))) 
        (begin
           (let*((parent-dir (pathname-directory target-path))
                 (start-dir (current-directory))
                 (filename (if  (pathname-extension target-path)  
                                      (conc(pathname-file target-path) "." (pathname-extension target-path))
                                      (pathname-file target-path)))
                 (work-dir-file (conc (current-directory) "/" filename)))
                 (if  (file-exists? work-dir-file)
                    (begin
                       (print filename " already exist in your work dir.")
                               (print  "Nothing has been retrieved!!  "))
                    (begin
               (change-directory parent-dir)
                (process-execute "/bin/tar" (append (append (list  "chfv" "-") (list filename)) (list "--ignore-failed-read"))) 
                 ;(run (pipe
                  ; (tar "chfv" "-" ,filename)
                  ; (begin (system (conc "cd " start-dir ";tar xUf -")))))
                    (change-directory start-dir)))))))))))

(define (sretrieve:make_file path exclude base_path)
   (find-files 
     path
     action: (lambda (p res)
           (cond
                ((symbolic-link? p)   
                 (if (directory?(read-symbolic-link p)) 
                      (sretrieve:make_file p exclude base_path)
                      (print (string-substitute (conc base_path "/") "" p "-"))))
                 ((directory? p)              
                 ;;do nothing for dirs)
                 ) 
                (else 
                                        
                     (if (not (string-match (regexp exclude)  p ))
                        (print (string-substitute (conc base_path "/") "" p "-"))))))))

(define (sretrieve:shell-help)
(conc "Usage: " *exe-name* " [action [params ...]]

  ls    [target path]               	  : list contents of target area. The output of the cmd can be piped into other system cmd. eg ls <path> | grep txt
  cd    <target directory>	     	  : To change the current directory within the sretrive shell. 
  pwd				     	  : Prints the full pathname of the current directory within the sretrive shell.
  get   <file or directory path>     	  : download directory/files into the directory where sretrieve shell cmd was invoked   
  less  <file path>		     	  : Read input file to allows backward movement in the file as well as forward movement 
  cat   <file path>                  	  : show the contents of a file. The output of the cmd can be piped into other system cmd.

  sgrep <search path> <pattern> [options] : Similar to unix grep cmd But with diffrent parameter ordering. The output of the cmd can be piped into other system cmd. 
Part of the Megatest tool suite.
Learn more at http://www.kiatoa.com/fossils/megatest

Version: " megatest-fossil-hash)
)	
;(define (toplevel-command . args) #f)
(define (sretrieve:shell area)
 ; (print area)
  (use readline)
  (let* ((path      '())
	 (prompt    "sretrieve> ")
	 (args      (argv))
         (usr (current-user-name) )   
768
769
770
771
772
773
774


775
776
777
778
779
780
781
782
783
784
785
			  ((null? thepath)
                          (print "Error: Missing argument <path> to get"))
			  ((< plen 2)
                           (let* ((target-path (sauth-common:get-target-path path  (car thepath) top-areas base-path))
				 (restrictions (if (equal? target-path #f)
                                                 ""
                                               (sretrieve:shell-lookup base-path))))


                                 (sretrieve:get-shell-cmd target-path base-path restrictions iport)
                                  (sauthorize:do-as-calling-user
                              (lambda ()
			    (run-cmd (conc *sauth-path* "/sauthorize") (list "register-log" (conc "\"" inl "\"") (number->string (car user-obj))  (number->string (caddr area-obj))  "get")))) ))
			  (else
                            (print "Error: get cmd takes only one argument ")))))
                      ((exit)
                          (print "got exit"))  
                      ((help)
                          (print (sretrieve:shell-help)))
		      (else 







>
>



|







846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
			  ((null? thepath)
                          (print "Error: Missing argument <path> to get"))
			  ((< plen 2)
                           (let* ((target-path (sauth-common:get-target-path path  (car thepath) top-areas base-path))
				 (restrictions (if (equal? target-path #f)
                                                 ""
                                               (sretrieve:shell-lookup base-path))))
                               (if (not (equal? target-path #f))
                             (begin  
                                 (sretrieve:get-shell-cmd target-path base-path restrictions iport)
                                  (sauthorize:do-as-calling-user
                              (lambda ()
			    (run-cmd (conc *sauth-path* "/sauthorize") (list "register-log" (conc "\"" inl "\"") (number->string (car user-obj))  (number->string (caddr area-obj))  "get"))))))))
			  (else
                            (print "Error: get cmd takes only one argument ")))))
                      ((exit)
                          (print "got exit"))  
                      ((help)
                          (print (sretrieve:shell-help)))
		      (else 
825
826
827
828
829
830
831

832

833
834
835
836
837
838
839

840


841
842


843
844


845







846






847
848
849
850
851
852

853







854








855


856





857
858

859
860
861
862


















863


864

865



866



867








868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
		   (begin
		     (debug:print 0 "Reading package config " package-config)
		     (read-config package-config #f #t))
		   (make-hash-table))))
      (pop-directory)
      res)))


(define (sretrieve:process-action configdat action . args)

    (case (string->symbol action)
      ((get)
       (if (< (length args) 1)
	   (begin 
	     (debug:print 0 "ERROR: Missing arguments; " (string-intersperse args ", "))
	     (exit 1)))
       (let* ((remargs     (args:get-args args '("-m" "-i" "-package") '() args:arg-hash 0))

              (version     (car args))


	      (msg         (or (args:get-arg "-m") ""))
	      (package-type (or (args:get-arg "-package")


				default-area))
	      (exe-dir     (configf:lookup configdat "exe-info" "exe-dir")))


	 (debug:print 0 "retrieving " version " of " package-type " as tar data on stdout")







	 (sretrieve:get configdat user version msg)))






         ((cp)
            (if (< (length args) 1)
             (begin 
	     (debug:print 0 "ERROR: Missing arguments; " (string-intersperse args ", "))
	     (exit 1)))
          (let* ((remargs     (args:get-args args '("-m" "-i" "-package") '() args:arg-hash 0))

              (file     (car args))







	      (msg         (or (args:get-arg "-m") "")) )











	 (debug:print 0 "copinging " file " to current directory " )





	 (sretrieve:cp configdat user file msg)))
      ((ls)

            (if (< (length args) 1)
             (begin 
	     (debug:print 0 "ERROR: Missing arguments; " (string-intersperse args ", "))
	     (exit 1)))


















          (let* ((remargs     (args:get-args args '("-m" "-i" "-package") '() args:arg-hash 0))


              (dir     (car args))

	      (msg         (or (args:get-arg "-m") "")) )







	 (debug:print 0 "Listing files in " )








	 (sretrieve:ls configdat user dir msg)))
       ((shell)
          (if (< (length args) 1)
             (begin 
	     (print  "ERROR: Missing arguments area!!" )
	     (exit 1))
             (sretrieve:shell (car args)))
       ) 
      (else (debug:print 0 "Unrecognised command " action))))

(define (main)
  (let* ((args      (argv))
	 (prog      (car args))
	 (rema      (cdr args))
	 (exe-name  (pathname-file (car (argv))))
	 (exe-dir   (or (pathname-directory prog)
			(sretrieve:find exe-name (string-split (get-environment-variable "PATH") ":"))))
	 (configdat (sretrieve:load-config exe-dir exe-name)))
    ;; preserve the exe data in the config file
    (hash-table-set! configdat "exe-info" (list (list "exe-name" exe-name)
						(list "exe-dir"  exe-dir)))
    (cond
     ;; one-word commands
     ((eq? (length rema) 1)
      (case (string->symbol (car rema))
	((help -h -help --h --help)
	 (print sretrieve:help))
	((list-vars) ;; print out the ini file
	 (map print (sretrieve:get-areas configdat)))
	((ls)
	 (let* ((base-dir (configf:lookup configdat "settings" "base-dir")))
	   (if base-dir
	       (begin
		 (print "Files in " base-dir)
                 (sretrieve:do-as-calling-user
                    (lambda ()
		 (process-execute "/bin/ls" (list "-lrt" base-dir)))))
	       (print "ERROR: No base dir specified!"))))
	((log)
	 (sretrieve:db-do configdat (lambda (db)
				     (print "Logs : ")
				     (query (for-each-row
					     (lambda (row)
					       (apply print (intersperse row " | "))))
					    (sql db "SELECT * FROM actions")))))
	;((shell)
	; (sretrieve:shell))
	(else
	 (print "ERROR: Unrecognised command. Try \"sretrieve help\""))))
     ;; multi-word commands
     ((null? rema)(print sretrieve:help))
     ((>= (length rema) 2)
      (apply sretrieve:process-action configdat (car rema)(cdr rema)))
     (else (debug:print 0 "ERROR: Unrecognised command. Try \"sretrieve help\"")))))

(main)


      







>

>


|

|

|
>
|
>
>
|
|
>
>
|
|
>
>
|
>
>
>
>
>
>
>
|
>
>
>
>
>
>

|
|
|

|
>
|
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
|
>
>
|
>
>
>
>
>
|

>
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
|
>
|
>
>
>
|
>
>
>
|
>
>
>
>
>
>
>
>
|



|

|
<



















<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<












905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035

1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054




















1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
		   (begin
		     (debug:print 0 "Reading package config " package-config)
		     (read-config package-config #f #t))
		   (make-hash-table))))
      (pop-directory)
      res)))

;(define (toplevel-command . args) #f)
(define (sretrieve:process-action configdat action . args)
 ;  (use readline)
    (case (string->symbol action)
      ((get)
       (if (< (length args) 2)
	   (begin 
	     (print  "ERROR: Missing arguments; <area> <relative path>" )
	     (exit 1)))
       (let* ((remargs     (args:get-args args '("-m" ) '() args:arg-hash 0))
              (iport (make-readline-port ">"))
              (area     (car args))
              (usr (current-user-name))
              (area-obj  (get-obj-by-code area))
              (user-obj (get-user usr))
              (top-areas (sretrieve:get-accessable-projects area)) 
              (base-path (if (null? area-obj) 
                                      "" 
                                     (caddr (cdr area-obj))))
	      (sub-path       (if (null? remargs) 
                                       "" 
                                       (car remargs))))

          (if (null? area-obj)
          	    (begin 
             		(print "Area " area " does not exist")
          	         (exit 1)))
              (let* ((target-path (sauth-common:get-target-path '()  (conc area "/" sub-path) top-areas base-path))
				 (restrictions (if (equal? target-path #f)
                                                 ""
                                               (sretrieve:shell-lookup base-path))))
             (if (not (equal? target-path #f))
                 (begin  
                   (sauthorize:do-as-calling-user
                      (lambda ()
   		        (run-cmd (conc *sauth-path* "/sauthorize") (list "register-log" (conc "get " area " " sub-path) (number->string (car user-obj))  (number->string (caddr area-obj))  "get"))))
                        (sretrieve:get-shell-cmd-line target-path base-path restrictions  iport))))))
         ((cp)
             (if (< (length args) 2)
	   (begin 
	     (print  "ERROR: Missing arguments; <area> <relative path>" )
	     (exit 1)))
       (let* ((remargs     (args:get-args args '("-m" ) '() args:arg-hash 0))
              (iport (make-readline-port ">"))
              (area     (car args))
              (usr (current-user-name))
              (area-obj  (get-obj-by-code area))
              (user-obj (get-user usr))
              (top-areas (sretrieve:get-accessable-projects area)) 
              (base-path (if (null? area-obj) 
                                      "" 
                                     (caddr (cdr area-obj))))
	      (sub-path       (if (null? remargs) 
                                       "" 
                                       (car remargs))))
         ;  (print args)
          (if (null? area-obj)
          	    (begin 
             		(print "Area " area " does not exist")
          	         (exit 1)))
              (let* ((target-path (sauth-common:get-target-path '()  (conc area "/" sub-path) top-areas base-path))
				 (restrictions (if (equal? target-path #f)
                                                 ""
                                              (sretrieve:shell-lookup base-path))))
                          ;(print target-path) 
                          (if (not (equal? target-path #f))
                             (begin  
                              (sauthorize:do-as-calling-user
                              (lambda ()
			    (run-cmd (conc *sauth-path* "/sauthorize") (list "register-log" (conc "get " area " " sub-path) (number->string (car user-obj))  (number->string (caddr area-obj))  "get"))))
                            (sretrieve:get-shell-cmd-line target-path base-path restrictions  iport))))))
      ((ls)
          (cond
            ((< (length args) 1)
              (begin 
	      (print  "ERROR: Missing arguments; <area> ")
	      (exit 1)))
              ((equal? (length args) 1)
                 (let*  ((area     (car args))
                         (usr (current-user-name))
                         (area-obj  (get-obj-by-code area))
                         (user-obj (get-user usr))
                         (top-areas (sretrieve:get-accessable-projects area)) 
                         (base-path (if (null? area-obj) 
                                      "" 
                                     (caddr (cdr area-obj)))))
                  (if (null? area-obj)
          	    (begin 
             		(print "Area " area " does not exist")
          	         (exit 1)))
           	 (sauth-common:shell-ls-cmd '() area top-areas base-path  '())
                 (sauthorize:do-as-calling-user
                   (lambda ()
		    (run-cmd (conc *sauth-path* "/sauthorize") (list "register-log" "ls" (number->string (car user-obj)) (number->string (caddr area-obj)) "ls"))))))
             ((> (length args) 1)
               (let*  ((remargs     (args:get-args args '("-m" ) '() args:arg-hash 0))
                        (usr (current-user-name))
                        (user-obj (get-user usr))
                         (area     (car args)))
                         (let* ((area-obj  (get-obj-by-code area))
                               (top-areas (sretrieve:get-accessable-projects area)) 
                               (base-path (if (null? area-obj) 
                                      "" 
                                     (caddr (cdr area-obj))))
                                 
                               (sub-path (if (null? remargs) 
                                       area 
                                      (conc area "/" (car remargs)))))
                             ;(print "sub path "  sub-path)
                            (if (null? area-obj)
          	              (begin 
             		        (print "Area " area " does not exist")
          	                 (exit 1)))
                              (sauth-common:shell-ls-cmd `()  sub-path top-areas base-path '())
                            (sauthorize:do-as-calling-user
				(lambda ()
                       	       (run-cmd (conc *sauth-path* "/sauthorize") (list "register-log" (conc "ls " sub-path) (number->string (car user-obj)) (number->string (caddr area-obj)) "ls")))))))))

       ((shell)
          (if (< (length args) 1)
             (begin 
	     (print  "ERROR: Missing arguments <area>!!" )
	     (exit 1))
             (sretrieve:shell (car args)))) 

      (else (debug:print 0 "Unrecognised command " action))))

(define (main)
  (let* ((args      (argv))
	 (prog      (car args))
	 (rema      (cdr args))
	 (exe-name  (pathname-file (car (argv))))
	 (exe-dir   (or (pathname-directory prog)
			(sretrieve:find exe-name (string-split (get-environment-variable "PATH") ":"))))
	 (configdat (sretrieve:load-config exe-dir exe-name)))
    ;; preserve the exe data in the config file
    (hash-table-set! configdat "exe-info" (list (list "exe-name" exe-name)
						(list "exe-dir"  exe-dir)))
    (cond
     ;; one-word commands
     ((eq? (length rema) 1)
      (case (string->symbol (car rema))
	((help -h -help --h --help)
	 (print sretrieve:help))




















	(else
	 (print "ERROR: Unrecognised command. Try \"sretrieve help\""))))
     ;; multi-word commands
     ((null? rema)(print sretrieve:help))
     ((>= (length rema) 2)
      (apply sretrieve:process-action configdat (car rema)(cdr rema)))
     (else (debug:print 0 "ERROR: Unrecognised command. Try \"sretrieve help\"")))))

(main)