summaryrefslogtreecommitdiff
path: root/disfluid/maintainer/scan-source.scm
blob: 63b7a90cafe9612715676ee5106f2df0e37b5362 (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
;; disfluid, implementation of the Solid specification
;; Copyright (C) 2022  Vivien Kraus

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU Affero General Public License for more details.

;; You should have received a copy of the GNU Affero General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(define-module (disfluid maintainer scan-source)
  #:use-module (guix gexp)
  #:use-module (guix store)
  #:use-module (guix monads)
  #:use-module (guix derivations)
  #:use-module (guix modules)
  #:use-module (gnu packages version-control)
  #:use-module (gnu packages package-management)
  #:use-module (gnu packages base)
  #:use-module (ice-9 popen)
  #:use-module (ice-9 rdelim)
  #:use-module (ice-9 match)
  #:declarative? #t
  #:export (scan-source))

;; The goal of scan-source is to run in the root of a git repository,
;; and then extract the following information as an alist:

;; - the version information of the checked out source (key: 'version);
;; - the latest commit ID of the main branch (key: 'commit);
;; - the latest commit ID in the translations branch (key: 'translations-commit);
;; - the guix hash of the latest commit in the main branch (key: 'hash);
;; - the guix hash of the translations branch (key: translations-hash);
;; - an alist of file names (e.g. "doc/disfluid.texi") to timestamp of their last modification date (key: 'mtimes);
;; - the translation files as a file-like object, from po file name (e.g. "en.po") to whole file content as a string (key: 'po-files).

;; It uses a temporary directory in /tmp to do that, but the directory
;; gets cleaned up. No other file is modified.

(define scan-source-code
  (with-imported-modules
      (source-module-closure '((guix build utils)))
    #~(begin
        (use-modules (guix build utils)
                     (ice-9 rdelim)
                     (ice-9 popen)
                     (ice-9 receive)
                     (ice-9 match))
        (define* (first-line . command)
          (let ((port
                 (apply
                  open-pipe*
                  OPEN_READ
                  command)))
            (let ((line (read-line port)))
              (close-port port)
              line)))
        (define* (git . args)
          (apply first-line #$(file-append git "/bin/git") args))
        (define* (invoke-git . args)
          (apply invoke #$(file-append git "/bin/git") args))
        (define (git-archive ref output)
          (invoke-git "archive" ref "-o" output))
        (define (date . args)
          (apply first-line #$(file-append coreutils "/bin/date") args))
        (define (git-log-1-%ci-timestamp ref file)
          (let ((readable (git "log" ref "-1" "--format=%ci" "--" file)))
            (string->number (date "-d" readable "+%s"))))
        (define* (invoke-tar . args)
          (apply invoke #$(file-append tar "/bin/tar") args))
        (define (tar-xf archive)
          (invoke-tar "xf" archive))
        (define* (guix . args)
          (apply first-line #$(file-append guix "/bin/guix") args))
        (define (guix-hash dir)
          (guix "hash" "-x" "-S" "nar" dir))
        (let ((version (git "describe" "--tags" "--dirty"))
              (commit (git "rev-parse" "main"))
              (translations-commit (git "rev-parse" "translations"))
              (directory
               (mkdtemp "/tmp/analyze-source-XXXXXX")))
          (git-archive "main"
                       (string-append directory "/source.tar.gz"))
          (git-archive "translations"
                       (string-append directory "/translations.tar.gz"))
          (receive (hash translations-hash all-translations)
              (with-directory-excursion directory
                (mkdir-p "pure")
                (mkdir-p "translations")
                (let ((h
                       (with-directory-excursion "pure"
                         (tar-xf "../source.tar.gz")
                         (guix-hash (getcwd))))
                      (po-h
                       (with-directory-excursion "translations"
                         (tar-xf "../translations.tar.gz")
                         (guix-hash (getcwd)))))
                  (let ((all-translations
                         (with-directory-excursion "translations/po"
                           (let ((dir (opendir (getcwd))))
                             (let scan ((all '()))
                               (let ((next (readdir dir)))
                                 (cond
                                  ((eof-object? next)
                                   (begin
                                     (closedir dir)
                                     (reverse all)))
                                  ((or (string-suffix? ".po" next)
                                       (string-suffix? ".pot" next))
                                   (let ((lang (basename next))
                                         (content
                                          (call-with-input-file next
                                            (lambda (port)
                                              (read-delimited "" port)))))
                                     (scan `((,lang ,content) ,@all))))
                                  (else
                                   (scan all)))))))))
                    (delete-file-recursively "pure")
                    (delete-file-recursively "translations")
                    (values h po-h all-translations))))
            (delete-file (string-append directory "/source.tar.gz"))
            (delete-file (string-append directory "/translations.tar.gz"))
            (delete-file-recursively directory)
            (let ((mtimes
                   (let ((important-files
                          '(("main" . "doc/disfluid.texi")
                            ("translations" . "po/disfluid.pot"))))
                     (map
                      (match-lambda
                        ((ref . file)
                         `(,file . ,(git-log-1-%ci-timestamp ref file))))
                      important-files))))
              (write
               `((version . ,version)
                 (commit . ,commit)
                 (hash . ,hash)
                 (translations-commit . ,translations-commit)
                 (translations-hash . ,translations-hash)
                 (mtimes . ,mtimes)
                 (po-files . ,all-translations)))))))))

(define (scan-source)
  (let ((port
         (open-pipe*
          OPEN_READ
          (run-with-store (open-connection)
            (mlet %store-monad
                ((scan-source
                  (gexp->script "invoke-scan-source-script"
                    scan-source-code)))
              (mbegin %store-monad
                (built-derivations (list scan-source))
                (return (derivation->output-path scan-source))))))))
    (let ((ret (read port)))
      (close-port port)
      `((version . ,(assq-ref ret 'version))
        (commit . ,(assq-ref ret 'commit))
        (hash . ,(assq-ref ret 'hash))
        (translations-commit . ,(assq-ref ret 'translations-commit))
        (translations-hash . ,(assq-ref ret 'translations-hash))
        (mtimes . ,(assq-ref ret 'mtimes))
        (po-files
         . ,(file-union
             "local-translations"
             `(("po"
                ,(file-union
                  "po"
                  (map
                   (match-lambda
                     ((file.po content)
                      `(,file.po ,(plain-file file.po content))))
                   (assq-ref ret 'po-files)))))))))))