| 1 |
#!/usr/bin/env texlua |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
local pcp = pcp or {} |
| 9 |
|
| 10 |
pcp.version = "2025A" |
| 11 |
pcp.date = "2025-02-20" |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
local lfs = require("lfs") |
| 18 |
|
| 19 |
kpse.set_program_name("kpsewhich") |
| 20 |
build_kpse_path = string.match(kpse.lookup("l3build.lua"),"(.*[/])") |
| 21 |
local function build_require(s) |
| 22 |
require(kpse.lookup("l3build-"..s..".lua", { path = build_kpse_path } ) ) |
| 23 |
end |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
build_require("file-functions") |
| 28 |
|
| 29 |
release_date = "2021-04-26" |
| 30 |
dofile("build.lua") |
| 31 |
|
| 32 |
build_require("variables") |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
local assert = assert |
| 39 |
local ipairs = ipairs |
| 40 |
local insert = table.insert |
| 41 |
local remove = table.remove |
| 42 |
local match = string.match |
| 43 |
local gsub = string.gsub |
| 44 |
|
| 45 |
local md5 = require("md5") |
| 46 |
|
| 47 |
local function md5sum(str) |
| 48 |
if str then return md5.sumhexa(str) end |
| 49 |
end |
| 50 |
|
| 51 |
local function filesum(name) |
| 52 |
local f = assert(io.open(name, "rb")) |
| 53 |
local s = f:read("*all") |
| 54 |
f:close() |
| 55 |
return md5sum(s) |
| 56 |
end |
| 57 |
|
| 58 |
local function readfile(name) |
| 59 |
local f = assert(io.open(name, "rb")) |
| 60 |
local s = f:read("*all") |
| 61 |
f:close() |
| 62 |
return s |
| 63 |
end |
| 64 |
|
| 65 |
local function writefile(name, sum) |
| 66 |
local f = assert(io.open(name, "w")) |
| 67 |
f:write(sum) |
| 68 |
f:close() |
| 69 |
end |
| 70 |
|
| 71 |
local function getfiles(path, pattern) |
| 72 |
local files = { } |
| 73 |
for entry in lfs.dir(path) do |
| 74 |
if match(entry, pattern) then |
| 75 |
insert(files, entry) |
| 76 |
end |
| 77 |
end |
| 78 |
return files |
| 79 |
end |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
imgext = imgext or ".png" |
| 86 |
|
| 87 |
local function getimgopt(imgext) |
| 88 |
local imgopt = "" |
| 89 |
if imgext == ".png" then |
| 90 |
imgopt = " -png " |
| 91 |
elseif imgext == ".ppm" then |
| 92 |
imgopt = " " |
| 93 |
elseif imgext == ".pgm" then |
| 94 |
imgopt = " -gray " |
| 95 |
elseif imgext == ".pbm" then |
| 96 |
imgopt = " -mono " |
| 97 |
else |
| 98 |
error("unsupported image extension" .. imgext) |
| 99 |
end |
| 100 |
return imgopt |
| 101 |
end |
| 102 |
|
| 103 |
local function pdftoimg(path, pdf) |
| 104 |
cmd = "pdftoppm " .. getimgopt(imgext) .. pdf .. " " .. jobname(pdf) |
| 105 |
run(path, cmd) |
| 106 |
end |
| 107 |
|
| 108 |
local function saveImgMd5(dir, imgname, md5file, newmd5) |
| 109 |
print("save md5 and image files for " .. imgname) |
| 110 |
cp(imgname, dir, testfiledir) |
| 111 |
writefile(md5file, newmd5) |
| 112 |
end |
| 113 |
|
| 114 |
local issave = false |
| 115 |
|
| 116 |
local function checkOnePdf(dir, job) |
| 117 |
local errorlevel |
| 118 |
local imgname = job .. imgext |
| 119 |
local md5file = testfiledir .. "/" .. job .. ".md5" |
| 120 |
local newmd5 = filesum(dir .. "/" .. imgname) |
| 121 |
if fileexists(md5file) then |
| 122 |
local oldmd5 = readfile(md5file) |
| 123 |
if newmd5 == oldmd5 then |
| 124 |
errorlevel = 0 |
| 125 |
print("md5 check passed for " .. imgname) |
| 126 |
else |
| 127 |
errorlevel = 1 |
| 128 |
print("md5 check failed for " .. imgname) |
| 129 |
local imgdiffexe = os.getenv("imgdiffexe") |
| 130 |
if imgdiffexe then |
| 131 |
local oldimg = abspath(testfiledir) .. "/" .. imgname |
| 132 |
local newimg = abspath(dir) .. "/" .. imgname |
| 133 |
local diffname = job .. ".diff.png" |
| 134 |
local cmd = imgdiffexe .. " " .. oldimg .. " " .. newimg |
| 135 |
.. " -compose src " .. diffname |
| 136 |
print("creating image diff file " .. diffname) |
| 137 |
run(dir, cmd) |
| 138 |
elseif issave == true then |
| 139 |
saveImgMd5(dir, imgname, md5file, newmd5) |
| 140 |
end |
| 141 |
end |
| 142 |
else |
| 143 |
errorlevel = 0 |
| 144 |
saveImgMd5(dir, imgname, md5file, newmd5) |
| 145 |
end |
| 146 |
return errorlevel |
| 147 |
end |
| 148 |
|
| 149 |
local function checkOneFolder(dir) |
| 150 |
print("checking folder " .. dir) |
| 151 |
local errorlevel = 0 |
| 152 |
local pattern = "%" .. pdfext .. "$" |
| 153 |
local files = getfiles(dir, pattern) |
| 154 |
for _, v in ipairs(files) do |
| 155 |
pdftoimg(dir, v) |
| 156 |
pattern = "^" .. jobname(v):gsub("%-", "%%-") .. "%-%d+%" .. imgext .. "$" |
| 157 |
local imgfiles = getfiles(dir, pattern) |
| 158 |
if #imgfiles == 1 then |
| 159 |
local imgname = jobname(v) .. imgext |
| 160 |
if fileexists(dir .. "/" .. imgname) then |
| 161 |
rm(dir, imgname) |
| 162 |
end |
| 163 |
ren(dir, imgfiles[1], imgname) |
| 164 |
local e = checkOnePdf(dir, jobname(v)) or 0 |
| 165 |
errorlevel = errorlevel + e |
| 166 |
else |
| 167 |
for _, i in ipairs(imgfiles) do |
| 168 |
local e = checkOnePdf(dir, jobname(i)) or 0 |
| 169 |
errorlevel = errorlevel + e |
| 170 |
end |
| 171 |
end |
| 172 |
end |
| 173 |
return errorlevel |
| 174 |
end |
| 175 |
|
| 176 |
local function cfgToDir(cfg) |
| 177 |
if cfg == "build" then |
| 178 |
return testdir |
| 179 |
else |
| 180 |
|
| 181 |
dofile(cfg .. ".lua") |
| 182 |
return testdir .. "-" .. cfg |
| 183 |
end |
| 184 |
end |
| 185 |
|
| 186 |
local function checkAllFolders(arglist) |
| 187 |
if arglist[1] == "-c" then |
| 188 |
if arglist[2] then |
| 189 |
return checkOneFolder(cfgToDir(arglist[2])) |
| 190 |
else |
| 191 |
print("missing config name for -c option") |
| 192 |
return 0 |
| 193 |
end |
| 194 |
else |
| 195 |
if #checkconfigs == 0 then |
| 196 |
return checkOneFolder(testdir) |
| 197 |
else |
| 198 |
local errorlevel = 0 |
| 199 |
for _, v in ipairs(checkconfigs) do |
| 200 |
local dir = cfgToDir(v) |
| 201 |
local e = checkOneFolder(dir) or 0 |
| 202 |
errorlevel = errorlevel + e |
| 203 |
end |
| 204 |
return errorlevel |
| 205 |
end |
| 206 |
end |
| 207 |
end |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
local helptext = [[ |
| 214 |
usage: ppmcheckpdf <action> [<options>] |
| 215 |
|
| 216 |
valid actions are: |
| 217 |
check Run tests without saving outputs of failed tests |
| 218 |
save Run tests and save outputs of failed tests |
| 219 |
help Print this message and exit |
| 220 |
version Print version information and exit |
| 221 |
|
| 222 |
valid options are: |
| 223 |
-c Set the config used for check or save action |
| 224 |
|
| 225 |
please report bug at https://github.com/lvjr/ppmcheckpdf |
| 226 |
]] |
| 227 |
|
| 228 |
local function help() |
| 229 |
print(helptext) |
| 230 |
return 0 |
| 231 |
end |
| 232 |
|
| 233 |
local function version() |
| 234 |
print("Ppmcheckpdf Version " .. pcp.version .. " (" .. pcp.date .. ")\n") |
| 235 |
return 0 |
| 236 |
end |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
local function pcpMain(pcparg) |
| 243 |
if pcparg[1] == nil then return help() end |
| 244 |
local action = remove(pcparg, 1) |
| 245 |
|
| 246 |
action = match(action, "^%-*(.*)$") |
| 247 |
if action == "check" then |
| 248 |
return checkAllFolders(pcparg) |
| 249 |
elseif action == "save" then |
| 250 |
issave = true |
| 251 |
return checkAllFolders(pcparg) |
| 252 |
elseif action == "help" then |
| 253 |
return help() |
| 254 |
elseif action == "version" then |
| 255 |
return version() |
| 256 |
else |
| 257 |
print("unknown action '" .. action .. "'\n") |
| 258 |
return help() |
| 259 |
end |
| 260 |
end |
| 261 |
|
| 262 |
local function main() |
| 263 |
return pcpMain(arg) |
| 264 |
end |
| 265 |
|
| 266 |
|
| 267 |
local errorlevel = main() |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
if os.type == "windows" then os.exit(errorlevel) end |