summaryrefslogtreecommitdiffstats
path: root/tcllib/support/devel/sak/validate/manpages.tcl
blob: fbb0e86251dd50d4fd912b16acaf28076e48efd1 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# -*- tcl -*-
# (C) 2008 Andreas Kupries <andreas_kupries@users.sourceforge.net>
##
# ###

package require  sak::animate
package require  sak::feedback
package require  sak::color

getpackage textutil::repeat textutil/repeat.tcl
getpackage doctools         doctools/doctools.tcl

namespace eval ::sak::validate::manpages {
    namespace import ::textutil::repeat::blank
    namespace import ::sak::color::*
    namespace import ::sak::feedback::!
    namespace import ::sak::feedback::>>
    namespace import ::sak::feedback::+=
    namespace import ::sak::feedback::=
    namespace import ::sak::feedback::=|
    namespace import ::sak::feedback::log
    namespace import ::sak::feedback::summary
    rename summary sum
}

# ###

proc ::sak::validate::manpages {modules mode stem tclv} {
    manpages::run $modules $mode $stem $tclv
    manpages::summary
    return
}

proc ::sak::validate::manpages::run {modules mode stem tclv} {
    sak::feedback::init $mode $stem
    sak::feedback::first log  "\[ Documentation \] ==============================================="
    sak::feedback::first unc  "\[ Documentation \] ==============================================="
    sak::feedback::first fail "\[ Documentation \] ==============================================="
    sak::feedback::first warn "\[ Documentation \] ==============================================="
    sak::feedback::first miss "\[ Documentation \] ==============================================="
    sak::feedback::first none "\[ Documentation \] ==============================================="

    # Preprocessing of module names to allow better formatting of the
    # progress output, i.e. vertically aligned columns

    # Per module we can distinguish the following levels of
    # documentation completeness and validity

    # Completeness:
    # - No package has documentation
    # - Some, but not all packages have documentation
    # - All packages have documentation.
    #
    # Validity, restricted to the set packages which have documentation:
    # - Documentation has errors and warnings
    # - Documentation has errors, but no warnings.
    # - Documentation has no errors, but warnings.
    # - Documentation has neither errors nor warnings.

    # Progress report per module: Packages it is working on.
    # Summary at module level:
    # - Number of packages, number of packages with documentation,
    # - Number of errors, number of warnings.

    # Full log:
    # - Lists packages without documentation.
    # - Lists packages with errors/warnings.
    # - Lists the exact errors/warnings per package, and location.

    # Global preparation: Pull information about all packages and the
    # modules they belong to.

    ::doctools::new dt -format desc -deprecated 1

    Count $modules
    MapPackages

    InitCounters
    foreach m $modules {
	# Skip tcllibc shared library, not a module.
	if {[string equal $m tcllibc]} continue

	InitModuleCounters
	!
	log "@@ Module $m"
	Head $m

	# Per module: Find all doctools manpages inside and process
	# them. We get errors, warnings, and determine the package(s)
	# they may belong to.

	# Per package: Have they doc files claiming them? After that,
	# are doc files left over (i.e. without a package)?

	ProcessPages    $m
	ProcessPackages $m
	ProcessUnclaimed
	ModuleSummary
    }

    dt destroy
    return
}

proc ::sak::validate::manpages::summary {} {
    Summary
    return
}

# ###

proc ::sak::validate::manpages::ProcessPages {m} {
    !claims
    dt configure -module $m
    foreach f [glob -nocomplain [file join [At $m] *.man]] {
	ProcessManpage $f
    }
    return
}

proc ::sak::validate::manpages::ProcessManpage {f} {
    =file              $f
    dt configure -file $f

    if {[catch {
	dt format [get_input $f]
    } msg]} {
	+e $msg
    } else {
	foreach {pkg _ _} $msg { +claim $pkg }
    }

    set warnings [dt warnings]
    if {![llength $warnings]} return

    foreach msg $warnings { +w $msg }
    return
}

proc ::sak::validate::manpages::ProcessPackages {m} {
    !used
    if {![HasPackages $m]} return

    foreach p [ThePackages $m] {
	+pkg $p
	if {[claimants $p]} {
	    +doc $p
	} else {
	    nodoc $p
	}
    }
    return
}

proc ::sak::validate::manpages::ProcessUnclaimed {} {
    variable claims
    if {![array size claims]} return
    foreach p [lsort -dict [array names claims]] {
	foreach fx $claims($p) { +u $fx }
    }
    return
}

###

proc ::sak::validate::manpages::=file {f} {
    variable current [file tail $f]
    = "$current ..."
    return
}

###

proc ::sak::validate::manpages::!claims {} {
    variable    claims
    array unset claims *
    return
}

proc ::sak::validate::manpages::+claim {pkg} {
    variable current
    variable claims
    lappend  claims($pkg) $current
    return
}

proc ::sak::validate::manpages::claimants {pkg} {
    variable claims
    expr { [info exists claims($pkg)] && [llength $claims($pkg)] }
}


###

proc ::sak::validate::manpages::!used {} {
    variable    used
    array unset used *
    return
}

proc ::sak::validate::manpages::+use {pkg} {
    variable used
    variable claims
    foreach fx $claims($pkg) { set used($fx) . }
    unset claims($pkg)
    return
}

###

proc ::sak::validate::manpages::MapPackages {} {
    variable    pkg
    array unset pkg *

    !
    += Package
    foreach {pname pdata} [ipackages] {
	= "$pname ..."
	foreach {pver pmodule} $pdata break
	lappend pkg($pmodule) $pname
    }
    !
    =| {Packages mapped ...}
    return
}

proc ::sak::validate::manpages::HasPackages {m} {
    variable pkg
    expr { [info exists pkg($m)] && [llength $pkg($m)] }
}

proc ::sak::validate::manpages::ThePackages {m} {
    variable pkg
    return [lsort -dict $pkg($m)]
}

###

proc ::sak::validate::manpages::+pkg {pkg} {
    variable mtotal ; incr mtotal
    variable total  ; incr total
    return
}

proc ::sak::validate::manpages::+doc {pkg} {
    variable mhavedoc ; incr mhavedoc
    variable havedoc  ; incr havedoc
    = "$pkg Ok"
    +use $pkg
    return
}

proc ::sak::validate::manpages::nodoc {pkg} {
    = "$pkg Bad"
    log "@@ WARN  No documentation: $pkg"
    return
}

###

proc ::sak::validate::manpages::+w {msg} {
    variable mwarnings ; incr mwarnings
    variable warnings  ; incr warnings
    variable current
    foreach {a b c} [split $msg \n] break
    log "@@ WARN  $current: [Trim $a] [Trim $b] [Trim $c]"
    return
}

proc ::sak::validate::manpages::+e {msg} {
    variable merrors ; incr merrors
    variable errors  ; incr errors
    variable current
    log "@@ ERROR $current $msg"
    return
}

proc ::sak::validate::manpages::+u {f} {
    variable used
    if {[info exists used($f)]} return
    variable munclaimed ; incr munclaimed
    variable unclaimed  ; incr unclaimed
    set used($f) .
    log "@@ WARN  Unclaimed documentation file: $f"
    return
}

###

proc ::sak::validate::manpages::Count {modules} {
    variable maxml 0
    !
    foreach m [linsert $modules 0 Module] {
	= "M $m"
	set l [string length $m]
	if {$l > $maxml} {set maxml $l}
    }
    =| "Validate documentation (existence, errors, warnings) ..."
    return
}

proc ::sak::validate::manpages::Head {m} {
    variable maxml
    += ${m}[blank [expr {$maxml - [string length $m]}]]
    return
}

###

proc ::sak::validate::manpages::InitModuleCounters {} {
    variable mtotal     0
    variable mhavedoc   0
    variable munclaimed 0
    variable merrors    0
    variable mwarnings  0
    return
}

proc ::sak::validate::manpages::ModuleSummary {} {
    variable mtotal
    variable mhavedoc
    variable munclaimed
    variable merrors
    variable mwarnings

    set complete [F $mhavedoc]/[F $mtotal]
    set not      "! [F [expr {$mtotal - $mhavedoc}]]"
    set err      "E [F $merrors]"
    set warn     "W [F $mwarnings]"
    set unc      "U [F $munclaimed]"

    if {$munclaimed} {
	set unc [=cya $unc]
	>> unc
    }
    if {!$mhavedoc && $mtotal} {
	set complete [=red $complete]
	set not      [=red $not]
	>> none
    } elseif {$mhavedoc < $mtotal} {
	set complete [=yel $complete]
	set not      [=yel $not]
	>> miss
    }
    if {$merrors} {
	set err  [=red $err]
	set warn [=yel $warn]
	>> fail
    } elseif {$mwarnings} {
	set warn [=yel $warn]
	>> warn
    }

    =| "~~ $complete $not $unc $err $warn"
    return
}

###

proc ::sak::validate::manpages::InitCounters {} {
    variable total     0
    variable havedoc   0
    variable unclaimed 0
    variable errors    0
    variable warnings  0
    return
}

proc ::sak::validate::manpages::Summary {} {
    variable total
    variable havedoc
    variable unclaimed
    variable errors
    variable warnings

    set tot   [F $total]
    set doc   [F $havedoc]
    set udc   [F [expr {$total - $havedoc}]]

    set unc   [F $unclaimed]
    set per   [format %6.2f [expr {$havedoc*100./$total}]]
    set uper  [format %6.2f [expr {($total - $havedoc)*100./$total}]]
    set err   [F $errors]
    set wrn   [F $warnings]

    if {$errors}    { set err [=red $err] }
    if {$warnings}  { set wrn [=yel $wrn] }
    if {$unclaimed} { set unc [=cya $unc] }

    if {!$havedoc && $total} {
	set doc [=red $doc]
	set udc [=red $udc]
    } elseif {$havedoc < $total} {
	set doc [=yel $doc]
	set udc [=yel $udc]
    }

    sum ""
    sum "Documentation statistics"
    sum "#Packages:     $tot"
    sum "#Documented:   $doc (${per}%)"
    sum "#Undocumented: $udc (${uper}%)"
    sum "#Unclaimed:    $unc"
    sum "#Errors:       $err"
    sum "#Warnings:     $wrn"
    return
}

###

proc ::sak::validate::manpages::F {n} { format %6d $n }

proc ::sak::validate::manpages::Trim {text} {
    regsub {^[^:]*:} $text {} text
    return [string trim $text]
}

###

proc ::sak::validate::manpages::At {m} {
    global distribution
    return [file join $distribution modules $m]
}

# ###

namespace eval ::sak::validate::manpages {
    # Max length of module names and patchlevel information.
    variable maxml 0

    # Counters across all modules
    variable total     0 ; # Number of packages overall.
    variable havedoc   0 ; # Number of packages with documentation.
    variable unclaimed 0 ; # Number of manpages not claimed by a specific package.
    variable errors    0 ; # Number of errors found in all documentation.
    variable warnings  0 ; # Number of warnings found in all documentation.

    # Same counters, per module.
    variable mtotal     0
    variable mhavedoc   0
    variable munclaimed 0
    variable merrors    0
    variable mwarnings  0

    # Name of currently processed manpage
    variable current ""

    # Map from packages to files claiming to document them.
    variable  claims
    array set claims {}

    # Set of files taken by packages, as array
    variable  used
    array set used {}

    # Map from modules to packages contained in them
    variable  pkg
    array set pkg {}
}

##
# ###

package provide sak::validate::manpages 1.0