summaryrefslogtreecommitdiff
path: root/modules/language/python/module/os/path.scm
blob: 0c1abde49456ba3a985d29d291b1cc246dc233b7 (about) (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
(define-module (language python module os path)
  #:use-module (language python module os)
  #:use-module (language python module pwd)
  #:use-module (language python module errno)
  #:use-module (language python module stat)
  #:use-module (language python for)
  #:use-module (language python try)
  #:use-module (system foreign)
  #:use-module (parser stis-parser)
  #:use-module (language python exceptions)
  #:use-module (oop pf-objects)
  #:use-module (ice-9 match)
  #:export (abspath basename commonpath commonprefix dirname exists
                    lexists expanduser expandvars getatime getmtime
                    getctime getsize isabs isfile isdir islink ismount
                    normcase normpath realpath relpath samefile sameopenfile
                    samestat split splitdrive splitext splitunc
                    supports_unicode_filenames))

(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))

(define-syntax-rule (ca code)
  (catch #t
    (lambda () code)
    (lambda x (raise OSError x))))

(define (path-it path)
  (aif it (ref path '__fspath__)
       (it)
       path))


(define (abspath path)
  (let ((path (path-it path)))
    (if (eq? (string-ref path 0) #\/)
        (normpath path)
        (normpath (string-append (getcwd) "/" path)))))
    
(define (basename path) (ca ((@ (guile) basename) (path-it path))))

(define (commonprefix paths)
  (letrec ((l (for ((p : paths)) ((l '()))
                   (cons (string->list (normpath p)) l)
                   #:final l))
           (f (lambda (l)
                (let lp ((l l) (r #f) (d '()))
                     (match l
                       ((() . _) '())
                       (((x . u) . l)
                        (if r
                            (if (equal? r x)
                                (lp l r (cons u d))
                                '())
                            (lp l x (cons u d))))
                       (()
                        (if r (cons r (f d)) '())))))))
    (list->string (f l))))

(define (commonpath paths)
  (define kind
    (for ((p : paths)) ((e #f))
         (let ((x (if (isabs (path-it p)) 'abs 'rel)))
           (if e
               (if (eq? e x)
                   e
                   (raise ValueError "Not all paths of the same type"))
               x))
         #:final e))

  (if (not kind) (raise ValueError "No paths"))
  
  (letrec ((l (for ((p : paths)) ((l '()))
                   (cons (string-split (normpath p) #\/) l)
                   #:final l))
           (f (lambda (l)
                (let lp ((l l) (r #f) (d '()))
                     (match l
                       ((() . _) '())
                       (((x . u) . l)
                        (if r
                            (if (equal? r x)
                                (lp l r (cons u d))
                                '())
                            (lp l x (cons u d))))
                       (()
                        (if r (cons r (f d)) '())))))))
    (if (equal? kind 'abs)
        (string-append "/" (string-join (f l) "/"))
        (string-join (f l) "/"))))

                               
(define (dirname p) (ca ((@ (guile) dirname) (path-it p))))
(define (exists p)
  (if (number? p)
      (catch #t
        (lambda () ((@ (guile) readlink) (format #f "/proc/self/fd/~a" p)))
        (lambda x #f))
      (catch #t
        (lambda () ((@ (guile) stat) (path-it p)) #t)
        (lambda x #f))))
      
(define (lexists p)
  (if (number? p)
      (catch #t
        (lambda () ((@ (guile) readlink) (format #f "/proc/self/fd/~a" p)))
        (lambda x #f))
      (catch #t
        (lambda () ((@ (guile) lstat) (path-it p)) #t)
        (lambda x #f))))


(define (expanduser p)
  (define (lookup-user u)
    (ref (getpwnam u) 'pw_dir))

  (define (lookup-self)
    (ref (getpwuid (getuid)) 'pw_dir))

  (define path (path-it p))
  
  (if (eq? (string-ref path 0) #\~)
      (if (and (> (string-length path) 1) (not (eq? (string-ref path 1) #\/)))
          (let* ((l (string-split path "/"))
                 (a (car l))
                 (u (list->string (cdr (string->list a)))))
            (string-join (cons (lookup-user u) (cdr l)) "/"))
          (let ((pw (getenv "PATH")))
            (if pw
                (string-join (append (string-split pw "/")
                                     (cdr (string-split path #\/)))
                             "/")
                (string-join (append (string-split (lookup-self) "/")
                                     (cdr (string-split path #\/)))))))))

(define f-var (mk-token
               (f-or!
                (f-seq (f-tag! "$") (f+ (f-reg! "[a-zA-Z0-9]")))
                (f-seq (f-tag! "$") (f-tag "{") (f+ (f-not! (f-tag "}")))
                       (f-tag "}")))))

(define f-other (mk-token (f* (f-not! f-var))))

(define f-f (f-cons f-other (ff* (f-seq f-var f-other))))

(define (expandvars p)
  (let lp ((l (parse (path-it p) f-f)) (r '()))
    (match l
      ((a b . l)
       (lp l (cons* (getenv b) a r)))
      ((a)
       (apply string-append (reverse (cons a r))))
      (()
       (apply string-append (reverse r))))))
    

(define (getatime p)
  (ca (stat:atime ((@ (guile) stat) (path-it p)))))

(define (getmtime p)
  (ca (stat:mtime ((@ (guile) stat) (path-it p)))))

(define (getctime p)
  (ca (stat:ctime ((@ (guile) stat) (path-it p)))))

(define (getsize p)
  (ca (stat:size ((@ (guile) stat) (path-it p)))))

(define (isabs p)
  (eq? (string-ref (path-it p) 0) #\/))

  
(define (isfile p)
  (ca (S_ISREG (stat:mode ((@ (guile) stat) (path-it p))))))

(define (isdir p)
  (ca (S_ISDIR (stat:mode ((@ (guile) stat) (path-it p))))))

(define (ismount p)
  (ca 
   (let* ((p (path-it p))
          (q (string-append p "/..")))
     (not (= (stat:dev ((@ (guile) stat) p))
             (stat:dev ((@ (guile) stat) q)))))))


(define (normcase x) x)

(define islink   (@@ (language python module os) path:islink))
(define join     (@@ (language python module os) path:join))
(define normpath (@@ (language python module os) path:normpath))
(define samestat (@@ (language python module os) path:samestat))

(define realpath
  (let ((free (pointer->procedure void
                                  (dynamic-func "free" (dynamic-link))
                                  (list '*)))
        (f    (pointer->procedure '*
                                  (dynamic-func "realpath" (dynamic-link))
                                  (list '* long))))
    (lambda (p)
      (let ((s (ca (f (string->pointer (path-it p)) 0))))
        (if (eq? (pointer-address s) 0)
            (raise OSError (format #f "realpath fails with errnp ~a" (errno)))
            (let ((ret (pointer->string s)))
              (free s)
              ret))))))

(define* (relpath p #:optional (start curdir))
  (define (reduce f s l)
    (if (pair? l)
        (reduce f (f (car l) s) (cdr l))
        s))
  (define l1 (string-split (realpath (path-it p)) #\/))
  (define l2 (string-split (realpath start) #\/))
  (define red (lambda (x s) (cons ".." s)))
  (let lp ((l1 l1) (l2 l2))
    (match (cons l1 l2)
      (((x . l1) . (y . l2))
       (if (equal? x y)
           (lp l1 l2)
           (string-join
            (append 
             (reduce red '() (cons y l2))
             l1)
            "/")))
      ((() . l)
       (string-join (reduce red '() l) "/"))
      ((l)
       (string-join l "/")))))

(define (samefile p1 p2)
  (samestat (stat p1) (stat p2)))

(define (sameopenfile p1 p2)
  (samestat (stat p1) (stat p2)))

(define (split p)
  (let ((l (string-split (path-it p) #\/)))
    (match l
      ((_ ... "")
       (list (path-it p) ""))
      ((x)
       (list "" x))
      ((l ... x)
       (list (string-join (append l (list "")) "/") x)))))

(define (splitdrive p)
  (let* ((l  (string-split (path-it p) #\/)))         
    (let lp ((l l) (r '()))
      (let ((p1 (string-join (reverse r) "/")))
        (if (ismount p1)
            (list p1 (string-join (cons "" l) "/"))
            (if (pair? l)
                (lp (cdr l) (cons (car l) r))
                (list "" (string-join (reverse r) "/"))))))))
          
(define (splitext p)
  (let ((x (string-split (path-it p) #\.)))
    (match x
      (("" y . l)
       (if (pair? l)
           (let* ((r (reverse l))
                  (e (car r))
                  (l (cons* "" y (reverse (cdr l)))))
             (list (string-join l ".") (string-append "." e)))
           (list x "")))
      ((y . l)
       (if (pair? l)
           (let* ((r (reverse l))
                  (e (car r))
                  (l (cons* y (reverse (cdr l)))))
             (list (string-join l ".") (string-append "." e)))
           (list x ""))))))

           
(define (splitunc p) (splitdrive p))

(define supports_unicode_filenames #t)