/usr/share/racket/collects/pkg/name.rkt is in racket-common 6.1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #lang racket/base
(require racket/list
racket/contract
racket/format
racket/string
net/url)
(provide
package-source-format?
(contract-out
[package-source->name+type (->* (string? (or/c #f package-source-format?))
(#:complain (-> string? string? any)
#:must-infer-name? boolean?
#:link-dirs? boolean?)
(values (or/c #f string?) (or/c #f package-source-format?)))]
[package-source->name (->* (string?)
((or/c #f package-source-format?))
(or/c #f string?))]
[package-source->path (->* (string?)
((or/c #f 'file 'dir 'link 'static-link))
path?)]))
(define rx:package-name #rx"^[-_a-zA-Z0-9]+$")
(define rx:archive #rx"[.](plt|zip|tar|tgz|tar[.]gz)$")
(define package-source-format?
(or/c 'name 'file 'dir 'github 'file-url 'dir-url 'link 'static-link))
(define (validate-name name complain inferred?)
(and name
(cond
[(regexp-match? rx:package-name name)
name]
[(equal? name "")
(complain (~a (if inferred? "inferred " "")
"package name is empty"))
#f]
[else
(complain (~a (if inferred? "inferred " "")
"package name includes disallowed characters"))
#f])))
(define (extract-archive-name name+ext complain)
(validate-name
(path->string
(if (regexp-match #rx#"[.]tar[.]gz$" (if (path? name+ext)
(path->bytes name+ext)
name+ext))
(path-replace-suffix (path-replace-suffix name+ext #"") #"")
(path-replace-suffix name+ext #"")))
complain
#t))
(define (last-non-empty p)
(cond
[(null? p) #f]
[else (or (last-non-empty (cdr p))
(and (not (equal? "" (path/param-path (car p))))
(path/param-path (car p))))]))
(define-syntax-rule (cor v complain)
(or v (begin complain #f)))
(define (package-source->name+type s type
#:link-dirs? [link-dirs? #f]
#:complain [complain-proc void]
#:must-infer-name? [must-infer-name? #f])
;; returns (values inferred-name inferred-type);
;; if `type' is given it should be returned, but name can be #f;
;; type should not be #f for a non-#f name
(define (complain msg)
(complain-proc s msg))
(define complain-name
(if must-infer-name? complain void))
(define (parse-path s)
(cond
[(if type
(eq? type 'file)
(and (path-string? s)
(regexp-match rx:archive s)))
(unless (path-string? s)
(complain "ill-formed path"))
(unless (regexp-match rx:archive s)
(complain "path does not end with a recognized archive suffix"))
(define-values (base name+ext dir?) (if (path-string? s)
(split-path s)
(values #f #f #f)))
(define name (and name+ext (extract-archive-name name+ext complain-name)))
(values name 'file)]
[(if type
(or (eq? type 'dir)
(eq? type 'link)
(eq? type 'static-link))
(path-string? s))
(unless (path-string? s)
(complain "ill-formed path"))
(define-values (base name dir?) (if (path-string? s)
(split-path s)
(values #f #f #f)))
(define dir-name (and (cor (path? name)
(if (not name)
(complain "no elements in path")
(complain "ending path element is not a name")))
(path->string name)))
(values (validate-name dir-name complain-name #t)
(or type (and dir-name (if link-dirs? 'link 'dir))))]
[else
(complain "ill-formed path")
(values #f #f)]))
(cond
[(if type
(eq? type 'name)
(regexp-match? rx:package-name s))
(validate-name s complain #f)
(values (and (regexp-match? rx:package-name s) s) 'name)]
[(and (eq? type 'github)
(not (regexp-match? #rx"^git(?:hub)?://" s)))
(package-source->name+type
(string-append "git://github.com/" s)
'github)]
[(if type
(or (eq? type 'github)
(eq? type 'file-url)
(eq? type 'dir-url))
(regexp-match? #rx"^(https?|github|git)://" s))
(define url (with-handlers ([exn:fail? (lambda (exn) #f)])
(string->url s)))
(define-values (name name-type)
(if url
(let ([p (url-path url)])
(cond
[(if type
(eq? type 'github)
(or (equal? (url-scheme url) "github")
(equal? (url-scheme url) "git")))
(unless (or (equal? (url-scheme url) "github")
(equal? (url-scheme url) "git"))
(complain "URL scheme is not 'git' or 'github'"))
(define name
(and (cor (pair? p)
(complain "URL path is empty"))
(cor (equal? "github.com" (url-host url))
(complain "URL host is not 'github.com'"))
(if (equal? (url-scheme url) "git")
;; git://
(and (cor (or (= (length p) 2)
(and (= (length p) 3)
(equal? "" (path/param-path (caddr p)))))
(complain "URL does not have two path elements (name and repo)"))
(let ([a (assoc 'path (url-query url))])
(define sub (and a (cdr a) (string-split (cdr a) "/")))
(if (pair? sub)
(validate-name (last sub) complain-name #t)
(let ([s (path/param-path (cadr p))])
(validate-name (regexp-replace #rx"[.]git$" s "") complain-name #t)))))
;; github://
(let ([p (if (equal? "" (path/param-path (last p)))
(reverse (cdr (reverse p)))
p)])
(and (cor ((length p) . >= . 3)
(complain "URL does not have at least three path elements"))
(validate-name
(if (= (length p) 3)
(path/param-path (second (reverse p)))
(last-non-empty p))
complain-name
#t))))))
(values name (or type 'github))]
[(if type
(eq? type 'file-url)
(and (pair? p)
(path/param? (last p))
(regexp-match? rx:archive (path/param-path (last p)))))
(unless (pair? p)
(complain "URL path is empty"))
(when (pair? p)
(unless (path/param? (last p))
(complain "URL's last path element is missing"))
(unless (regexp-match? rx:archive (path/param-path (last p)))
(complain "URL does not end with a recognized archive suffix")))
(values (and (pair? p)
(extract-archive-name (last-non-empty p) complain-name))
'file-url)]
[else
(unless (pair? p)
(complain "URL path is empty"))
(when (pair? p)
(unless (path/param? (last p))
(complain "URL's last path element is missing")))
(values (validate-name (last-non-empty p) complain-name #t) 'dir-url)]))
(values #f #f)))
(values (validate-name name complain-name #f) (or type (and name-type)))]
[(and (not type)
(regexp-match #rx"^file://(.*)$" s))
=> (lambda (m) (parse-path (cadr m)))]
[(and (not type)
(regexp-match? #rx"^[a-zA-Z]*://" s))
(complain "unreognized URL scheme")
(values #f #f)]
[else
(parse-path s)]))
(define (package-source->name s [given-type #f])
(define-values (name type) (package-source->name+type s given-type))
name)
(define (package-source->path s [type #f])
((if (memq type '(dir link static-link))
path->directory-path
values)
(cond
[(regexp-match? #rx"^file://" s)
(url->path (string->url s))]
[else
(string->path s)])))
|