aboutsummaryrefslogtreecommitdiff
path: root/pypi/sdist-store/utils.scm
blob: 483e7308fab52f9cf6ca3774011e2eb47fd31501 (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
(define-module (pypi sdist-store utils)
  #:use-module (pyguile)
  #:use-module (logging logger)
  #:use-module (ice-9 binary-ports)
  #:use-module (ice-9 match)
  #:use-module (ice-9 pretty-print)
  #:use-module (ice-9 ftw)
  #:use-module (ice-9 regex)
  #:use-module ((ice-9 rdelim) #:select (read-line))
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-35)
  #:use-module (rnrs bytevectors)
  #:use-module (json)
  #:use-module (web uri)
  #:use-module (guix ui)
  #:use-module (guix utils)
  #:use-module (guix import utils)
  #:use-module ((guix download) #:prefix download:)
  #:use-module (guix import json)
  #:use-module (guix packages)
  #:use-module (guix upstream)
  #:use-module (guix licenses)
  #:use-module (guix build-system python)
  #:use-module (pypi api)
  #:use-module (pypi sdist)
  #:use-module (pypi sdist-store)
  #:use-module (pypi requirement)
  #:use-module (gnu packages python)
  #:export (ensure-unpacked-sdist-exists
            search-for-egg-info
            find-cycle-in-graph
            get-sdist-directory
            read-rfc822
            get-tmpdir))

(define (get-tmpdir)
  "/tmp/guix-pypi")

(define (ensure-unpacked-sdist-exists name version api-root)
  (let ((tmpdir (get-tmpdir))
        (sdist-directory (get-sdist-directory name version)))
    (begin
      (unless (file-exists? tmpdir)
        (mkdir tmpdir))
      (unless (file-exists? sdist-directory)
        (let* ((data (source-release (pypi-fetch name api-root) version))
               (filename (assoc-ref* data "filename"))
               (full-path (string-append tmpdir "/" filename))
               (source-url (assoc-ref* data "url")))
          (begin
            (if (not (file-exists? full-path))
                (url-fetch source-url full-path))
            (if
              (string-contains filename ".zip")
              (let
                ((exit-code
                   (system* "unzip" "-qq" full-path "-d" tmpdir)))
                (if (not (zero? exit-code))
                  (begin
                    (log-msg 'ERROR "Attempted to unzip " full-path)
                    (error (_ "'unzip' failed with exit code ~a\n")
                             exit-code))
                  #f))
              (let* ((compression-type
                      (cond
                        ((string-contains filename "gz") "z")
                        ((string-contains filename "bz2") "j")
                        ((begin
                           (error "unknown compression type")
                           #f))))
                    (exit-code
                      (system* "tar" (string-append compression-type "xf") full-path "--directory" tmpdir "--no-same-owner")))
                (if (not (zero? exit-code))
                  (begin
                    (error (_ "'tar' failed with exit code ~a\n")
                             exit-code)
                    #f))))
            (rename-file
              (string-append tmpdir "/" (tarball-directory source-url))
              sdist-directory)))))))

(define (get-expected-sdist-name name version)
  (string-append (string-downcase name) "-" version))

(define (get-sdist-directory name version)
  (string-append (get-tmpdir) "/" (get-expected-sdist-name name version)))

(define (tarball-directory url)
  ;; Given the URL of the package's tarball, return the name of the directory
  ;; that will be created upon decompressing it. If the filetype is not
  ;; supported, return #f.
  ;; TODO: Support more archive formats.
  (let ((basename (substring url (+ 1 (string-rindex url #\/)))))
    (cond
     ((string-suffix? ".tar.gz" basename)
      (string-drop-right basename 7))
     ((string-suffix? ".tar.bz2" basename)
      (string-drop-right basename 8))
     ((string-suffix? ".zip" basename)
      (string-drop-right basename 4))
     (else
      (begin
        (warning (_ "Unsupported archive format: \
nnot determine package dependencies"))
        #f)))))

(define (search-for-egg-info startname depth)
  (if (= depth 0)
    '()
    (let ((possibles (scandir
                       startname
                       (lambda (name) (string-contains name ".egg-info")))))
      (if (> (length possibles) 0)
          (map
            (lambda (p) (string-append startname "/" p))
            possibles)
          (apply
            append
            (map
              (lambda (sn) (search-for-egg-info
                             (string-append startname "/" sn) (- depth 1)))
              (scandir
                startname
                (lambda (d) (eq?
                              'directory
                              (stat:type
                                (stat (string-append startname "/" d))))))))))))

(define-condition-type &missing-source-error &error
  missing-source-error?
  (package  missing-source-error-package))

(define (get-archive-suffix name)
  (find
    (lambda (suffix)
      (string-suffix? suffix name))
    archive-preference))

(define archive-preference '(".tar.gz" ".tar.bz2" ".zip"))

(define (source-release pypi-package version)
  (let ((releases (assoc-ref* pypi-package "releases" version)))
    (or (and
          releases
          (find (lambda (release)
                (string=? "sdist" (assoc-ref release "packagetype")))
              releases))
        (raise (condition (&missing-source-error
                           (package pypi-package)))))))

;;; RFC822 Parsing

(define header-name-rx (make-regexp "^([^:]+):[ \t]*"))
(define header-cont-rx (make-regexp "^[ \t]+"))

(define (drain-message port)
  (let loop ((line (read-line port)) (acc '()))
    (cond ((eof-object? line)
           (reverse acc))
          (else
           (loop (read-line port) (cons line acc))))))

(define (parse-message port)
  (let* ((body-lines #f)
         (body #f)
         (headers '())
         (add-header! (lambda (reversed-hlines)
                        (let* ((hlines (reverse reversed-hlines))
                               (first (car hlines)))
                          (if (not (= (string-length first) 0))
                            (let* ((m (regexp-exec header-name-rx first))
                                   (name (string->symbol (match:substring m 1)))
                                   (data (string-join
                                          (cons (substring first (match:end m))
                                                (cdr hlines))
                                          " ")))
                              (set! headers (acons name data headers))))))))
    ;; "From " is only one line
    (let loop ((line (read-line port)) (current-header #f))
      (cond ((eof-object? line)
             (and current-header (add-header! current-header))
             (set! body-lines (drain-message port)))
            ((regexp-exec header-cont-rx line)
             => (lambda (m)
                  (loop (read-line port)
                        (cons (match:suffix m) current-header))))
            (else
             (and current-header (add-header! current-header))
             (loop (read-line port) (list line)))))
    (set! headers (reverse headers))
    (lambda (component)
      (case component
        ((body-lines) body-lines)
        ((headers) headers)
        ((body) (or body
                    (begin (set! body (string-join body-lines "\n" 'suffix))
                           body)))
        (else (error "bad component:" component))))))

(define (read-rfc822 port)
  (parse-message port))

(define (display-rfc822 parse)
  (cond ((parse 'from) => (lambda (from) (format #t "From ~A\n" from))))
  (for-each (lambda (header)
              (format #t "~A: ~A\n" (car header) (cdr header)))
            (parse 'headers))
  (format #t "\n~A" (parse 'body)))

;; Graph utils

(define (find-cycle-in-graph graph)
  (recurse-and-return-cycle-if-found
    graph
    (car (hash-map->list
           (lambda (k v) k)
           graph))
    '()))

(define (recurse-and-return-cycle-if-found graph node visited-nodes)
  (if (member node visited-nodes)
    (memq node (reverse visited-nodes))
    (let
      ((links (hash-ref graph node)))
      (if links
        (any
          (lambda (next-node)
            (recurse-and-return-cycle-if-found
              graph next-node (cons node visited-nodes)))
          links)
        #f))))