Python Skript update.
[wortliste.git] / skripte / validate.lua
blob5d188b5443b3076421a60ab2060a849835bd480f
1 -- -*- coding: utf-8 -*-
3 --[[
5 Copyright 2012, 2013, 2014, 2015 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_regular_exceptions(fname_ex)
65 print("Verwende Ausnahmeliste " .. fname_ex)
68 -- Speichere Dateinamenargument der Option --blame.
69 local fname_db = opt.b
70 -- Lese von Standardeingabe oder aus Datei.
71 local fin_db = fname_db and assert(io.open(fname_db, "r")) or io.stdin
74 -- Prüfe Wortliste.
75 local info = hrecords.validate_file(fin_db)
78 -- Ausgabe.
79 if opt.s then
80 hrecords.output_record_statistics(info.cnt_rectypes)
81 end
82 print("gesamt ", info.cnt_total)
83 print("ungültig ", info.cnt_invalid)
86 -- Zeige fehlerhafte Commits.
87 if opt.b then
88 -- Erstelle git-blame-Kommando mit verketteten Zeilennummern.
89 local call = "git blame"
90 for lineno,_ in pairs(info.bad_lineno) do
91 call = call .. " -L " .. lineno .."," .. lineno
92 end
93 call = call .. " " .. fname_db
94 -- Tabelle, die Commits auf Zähler abbildet.
95 local bad_commits = {}
96 -- Tabelle, die Commits auf Datumswerte abbildet.
97 local commit_date = {}
98 -- Verarbeite Ergebnis des git-blame-Kommandos.
99 local f = assert(io.popen(call))
100 for line in f:lines() do
101 -- Extrahiere Commit-Hash und Datum.
102 local commit, date = string.match(line, "^(%w+) %(.-(%d%d%d%d%-%d%d%-%d%d)")
103 bad_commits[commit] = (bad_commits[commit] or 0) + 1
104 commit_date[commit] = date
106 f:close()
107 -- Sortiere Commits nach Datum.
108 local commits = {}
109 for commit,_ in pairs(commit_date) do
110 table.insert(commits, commit)
112 table.sort(commits, function(a,b) return commit_date[a] < commit_date[b] end)
113 -- Gebe Commits nach Datum sortiert aus.
114 for _,commit in ipairs(commits) do
115 io.stderr:write("Commit ", commit, " ", commit_date[commit], ": ", bad_commits[commit], "\n")
120 -- Ende mit Fehlerkode?
121 if info.cnt_invalid > 0 then os.exit(1) end