stowm 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,121 @@
1
+ ;; #-*- mode: nendo; syntax: scheme -*-;;
2
+ ;; Some parser library functions
3
+
4
+ (use srfi-9)
5
+ (require "uri")
6
+
7
+ (define stowm-ext-list '(
8
+ ("tar[.]gz" . gz)
9
+ ("tgz" . gz)
10
+ ("tar[.]bz2" . bz2)
11
+ ("tar[.]xz" . xz)
12
+ ("tar[.][zZ]" . z)))
13
+
14
+ (define stowm-scheme-list '(http https ftp))
15
+
16
+ (define stowm-rx-project-name "([a-zA-Z][a-zA-Z0-9]+)")
17
+ (define stowm-rx-version "(.+)")
18
+ (define stowm-rx-ext (+ "(" (string-join
19
+ (map car stowm-ext-list)
20
+ "|") ")"))
21
+ (define (stowm-split-filename filename)
22
+ ;; detect ext
23
+ (let1 filename-and-ext
24
+ (cond ((stowm-regex-match (+ "(.+)[.]" stowm-rx-ext "$") filename)
25
+ => (lambda (x)
26
+ (cons (second x)
27
+ (third x))))
28
+ (else
29
+ (cons filename #f)))
30
+ (let ([filename (car filename-and-ext)]
31
+ [ext (cdr filename-and-ext)])
32
+ (cond
33
+ ((stowm-regex-match (+ "^" stowm-rx-project-name "[-]" stowm-rx-version "$") filename)
34
+ => (lambda (x)
35
+ (list (second x) (third x) ext)))
36
+ ((stowm-regex-match (+ "^" stowm-rx-project-name "$") filename)
37
+ => (lambda (x)
38
+ (list (second x) #f ext)))
39
+ (else
40
+ '(#f #f #f))))))
41
+
42
+ (define (stowm-split-path path)
43
+ (let1 lst (to-list (File.split path))
44
+ (let ([dirname (first lst)]
45
+ [filename (second lst)])
46
+ (list dirname filename))))
47
+
48
+ (define (stowm-get-ext-type ext-string)
49
+ (let1 lst (filter-map
50
+ (lambda (entry)
51
+ (if (stowm-regex-match (car entry) ext-string)
52
+ (cdr entry)
53
+ #f))
54
+ stowm-ext-list)
55
+ (if (< 0 (length lst))
56
+ (first lst)
57
+ #f)))
58
+
59
+ (define (stowm-get-scheme-type scheme-string)
60
+ (if scheme-string
61
+ (let1 sym (string->symbol scheme-string)
62
+ (if (memq sym stowm-scheme-list)
63
+ sym
64
+ 'unknown))
65
+ 'empty))
66
+
67
+
68
+ (define-record-type <parsed>
69
+ (parsed scheme scheme-type host port path dir filename proj ver package ext arc-type)
70
+ parsed?
71
+ (scheme scheme)
72
+ (scheme-type scheme-type)
73
+ (host host)
74
+ (port port)
75
+ (path path)
76
+ (dir dir)
77
+ (filename filename)
78
+ (proj proj)
79
+ (ver ver)
80
+ (package package)
81
+ (ext ext)
82
+ (arc-type arc-type))
83
+
84
+ ;; This parser can handle like:
85
+ ;; *.tar.gz
86
+ ;; *.tar.bz2
87
+ ;; *.tar.xz
88
+ ;; http://hostname/xxx/yyy/*.tar.gz
89
+ ;;
90
+ ;; return: <parsed> record
91
+ (define (stowm-parse-url url-string)
92
+ (let1 url (URI.parse url-string)
93
+ (let1 lst (stowm-split-path url.path)
94
+ (let1 fields (stowm-split-filename (second lst))
95
+ (parsed
96
+ url.scheme ;; => "http"
97
+ (stowm-get-scheme-type url.scheme) ;; => 'http
98
+ url.host ;; => "www.example.com"
99
+ url.port ;; => 80
100
+ url.path ;; => "/xxx/yyy/aaaa-1.2.3.tar.gz"
101
+ (first lst) ;; => "/xxx/yyy"
102
+ (second lst) ;; => "aaaa-1.2.3.tar.gz"
103
+ (or (first fields) "") ;; => "aaaa"
104
+ (or (second fields) "");; => "1.2.3"
105
+ (+ (or (first fields) "")
106
+ "-"
107
+ (or (second fields) "")) ;; => "aaaa-1.2.3"
108
+ (or (third fields) "");; => "tar.gz"
109
+ (stowm-get-ext-type (or (third fields) ""))
110
+ )))))
111
+
112
+ (define (is-valid-url? url-string)
113
+ (let1 parsed (stowm-parse-url url-string)
114
+ (if (arc-type parsed)
115
+ (if (eq? 'unknown (scheme-type parsed))
116
+ #f
117
+ (if (< 0 (. (ver parsed) size))
118
+ #t
119
+ #f))
120
+ #f)))
121
+