Debian Codegraph @

examples/copyrewrite.scm

raw
1 ;; Rewrite "copyright" line of the source files to make it current.
2 ;; This is a quick "throwaway" script. It is grossly inefficient,
3 ;; but it does the job. An example of something you write in half
4 ;; an hour to finish some chores.
5
6 (use file.util)
7 (use util.match)
8 (use srfi.19)
9
10 (define (usage)
11 (print "Usage: gosh copyrewrite.scm <directory> <author> <email>")
12 (exit 0))
13
14 (define (main args)
15 (match (cdr args)
16 [(dir author email)
17 (directory-fold dir
18 (^[path seed]
19 (when (or (#/\.(c|h|scm|stub|in|texi)$/ path)
20 (member (sys-basename path)
21 '("COPYING" "genstub" "geninsn")))
22 (check-file path author email)))
23 #f)]
24 [_ (usage)])
25 0)
26
27 (define (check-file path author email)
28 (define check-rx
29 (string->regexp #"[cC]opyright\\s*(\\D+)?(\\d+)(-\\d+)?\\s+(by\\s+)?~|author|"))
30 (define current-year (date-year (current-date)))
31 (define (file->string-list+ path)
32 (call-with-input-file path
33 (lambda (in)
34 (unwind-protect
35 (port->string-list (open-coding-aware-port in))
36 (close-input-port in)))))
37 (define (rewrite line)
38 (let* ([m (check-rx line)]
39 [start-year (x->integer (m 2))]
40 [copyr (or (m 1) "")]
41 [by (or (m 4) " ")]
42 [years (if (= start-year current-year)
43 start-year
44 #"~|start-year|-~|current-year|")]
45 [em (if (equal? (path-extension path) "texi")
46 (regexp-replace #/@/ email "@@")
47 email)])
48 #"~(m 'before)Copyright ~|copyr|~|years| ~|by|~|author| <~|em|>"))
49
50 (and-let* ([input (file->string-list+ path)]
51 [matched (find check-rx input)])
52 (print "Rewriting " path "...")
53 (call-with-temporary-file
54 (^[out tmp]
55 (for-each (lambda (line)
56 (display (if (eq? line matched) (rewrite line) line) out)
57 (newline out))
58 input)
59 (replace-file path tmp))
60 :directory (sys-dirname path))))
61
62 (define (replace-file path tmp)
63 (sys-chmod tmp (file-perm path))
64 (sys-rename tmp path))