Wahrscheinlichere Alternative an erster Stelle.
[wortliste.git] / skripte / validate.lua
blobdfe67012b15ef98718a612e3c0d13192c288eabf
1 -- -*- coding: utf-8 -*-
3 --[[
5 Copyright 2012, 2013 Stephan Hennig
7 This program is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation, either version 3 of the License, or (at your
10 option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program. If not, see <http://www.gnu.org/licenses/>.
20 --]]
23 -- Lese Konfigurationdaten aus.
24 local path_dirsep, path_sep, path_subst = string.match(package.config, "(.-)\n(.-)\n(.-)\n")
25 -- Erweitere Modulsuchpfad.
26 package.path = package.path
27 .. path_sep .. "lua" .. path_dirsep .. path_subst .. ".lua"
28 .. path_sep .. "skripte" .. path_dirsep .. "lua" .. path_dirsep .. path_subst .. ".lua"
30 -- Lade Module aus Lua-Pfad.
31 local hrecords = require("helper_records")
33 -- Lade Module aus TEXMF-Baum.
34 kpse.set_program_name('luatex')
35 local alt_getopt = require("alt_getopt")
38 -- Erkläre zulässige Optionen.
39 local long_opts = {
40 blame = "b",
41 help = "h",
42 statistics = "s",
44 local opt = alt_getopt.get_opts(arg, "b:hs", long_opts)
46 -- Option --help
47 if opt.h then
48 print([[
49 Aufruf: texlua validate.lua [OPTIONEN]
50 Dieses Skript prüft eine Wortliste auf Wohlgeformtheit. Die Wortliste
51 wird von der Standardeingabe gelesen.
52 -b --blame file show erroneous commits
53 Reads records from file instead of stdin.
54 -h, --help print help
55 -s, --statistics output record statistics
58 os.exit()
59 end
62 -- Lese Ausnahmeliste.
63 local fname_ex = "wortliste.ausnahmen"
64 fname_ex = hrecords.read_exception_file(fname_ex)
65 print("Verwende Ausnahmeliste " .. fname_ex)
68 -- Lese von Standardeingabe.
69 local fin_db = io.stdin
70 -- Oder aus Datei.
71 local fname_db = opt.b
72 if fname_db then
73 fin_db = assert(io.open(fname_db, "r"))
74 end
77 -- Prüfe Wortliste.
78 local info = hrecords.validate_file(fin_db)
81 -- Ausgabe.
82 if opt.s then
83 hrecords.output_record_statistics(info.cnt_rectypes)
84 end
85 print("gesamt ", info.cnt_total)
86 print("ungültig ", info.cnt_invalid)
89 -- Zeige fehlerhafte Commits.
90 if opt.b then
91 -- Erstelle git-blame-Kommando mit verketteten Zeilennummern.
92 local call = "git blame"
93 for _,lineno in ipairs(info.bad_lineno) do
94 call = call .. " -L " .. lineno .."," .. lineno
95 end
96 call = call .. " " .. fname_db
97 -- Tabelle, die Commits auf Zähler abbildet.
98 local bad_commits = {}
99 -- Tabelle, die Commits auf Datumswerte abbildet.
100 local commit_date = {}
101 -- Verarbeite Ergebnis des git-blame-Kommandos.
102 local f = assert(io.popen(call))
103 for line in f:lines() do
104 -- Extrahiere Commit-Hash und Datum.
105 local commit, date = string.match(line, "^(%w+) %(.-(%d%d%d%d%-%d%d%-%d%d)")
106 bad_commits[commit] = (bad_commits[commit] or 0) + 1
107 commit_date[commit] = date
109 f:close()
110 -- Sortiere Commits nach Datum.
111 local commits = {}
112 for commit,_ in pairs(commit_date) do
113 table.insert(commits, commit)
115 table.sort(commits, function(a,b) return commit_date[a] < commit_date[b] end)
116 -- Gebe Commits nach Datum sortiert aus.
117 for _,commit in ipairs(commits) do
118 io.stderr:write("Commit ", commit, " ", commit_date[commit], ": ", bad_commits[commit], "\n")
123 -- Ende mit Fehlerkode?
124 if info.cnt_invalid > 0 then os.exit(1) end