summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/data-use.lua
blob: ccd44007c439b21d02eb599f9d6c92c710a8feb4 (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
if not modules then modules = { } end modules ['data-use'] = {
    version   = 1.001,
    comment   = "companion to luat-lib.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local format, lower, gsub, find = string.format, string.lower, string.gsub, string.find

local trace_locating = false  trackers.register("resolvers.locating", function(v) trace_locating = v end)

local report_mounts = logs.reporter("resolvers","mounts")

local resolvers = resolvers

-- we will make a better format, maybe something xml or just text or lua

resolvers.automounted = resolvers.automounted or { }

function resolvers.automount(usecache)
    local mountpaths = resolvers.cleanpathlist(resolvers.expansion('TEXMFMOUNT'))
    if (not mountpaths or #mountpaths == 0) and usecache then
        mountpaths = caches.getreadablepaths("mount")
    end
    if mountpaths and #mountpaths > 0 then
        resolvers.starttiming()
        for k=1,#mountpaths do
            local root = mountpaths[k]
            local f = io.open(root.."/url.tmi")
            if f then
                for line in f:lines() do
                    if line then
                        if find(line,"^[%%#%-]") then -- or %W
                            -- skip
                        elseif find(line,"^zip://") then
                            if trace_locating then
                                report_mounts("mounting %a",line)
                            end
                            table.insert(resolvers.automounted,line)
                            resolvers.usezipfile(line)
                        end
                    end
                end
                f:close()
            end
        end
        resolvers.stoptiming()
    end
end

-- status info

statistics.register("used config file", function() return caches.configfiles() end)
statistics.register("used cache path",  function() return caches.usedpaths() end)

-- experiment (code will move)

function statistics.savefmtstatus(texname,formatbanner,sourcefile,kind,banner) -- texname == formatname
    local enginebanner = status.banner
    if formatbanner and enginebanner and sourcefile then
        local luvname = file.replacesuffix(texname,"luv") -- utilities.lua.suffixes.luv
        local luvdata = {
            enginebanner  = enginebanner,
            formatbanner  = formatbanner,
            sourcehash    = md5.hex(io.loaddata(resolvers.findfile(sourcefile)) or "unknown"),
            sourcefile    = sourcefile,
            luaversion    = LUAVERSION,
            formatid      = LUATEXFORMATID,
            functionality = LUATEXFUNCTIONALITY,
        }
        io.savedata(luvname,table.serialize(luvdata,true))
        lua.registerfinalizer(function()
            if jit then
                logs.report("format banner","%s  lua: %s jit",banner,LUAVERSION)
            else
                logs.report("format banner","%s  lua: %s",banner,LUAVERSION)
            end
            logs.newline()
        end)
    end
end

-- todo: check this at startup and return (say) 999 as signal that the run
-- was aborted due to a wrong format in which case mtx-context can trigger
-- a remake

function statistics.checkfmtstatus(texname)
    local enginebanner = status.banner
    if enginebanner and texname then
        local luvname = file.replacesuffix(texname,"luv") -- utilities.lua.suffixes.luv
        if lfs.isfile(luvname) then
            local luv = dofile(luvname)
            if luv and luv.sourcefile then
                local sourcehash = md5.hex(io.loaddata(resolvers.findfile(luv.sourcefile)) or "unknown")
                local luvbanner = luv.enginebanner or "?"
                if luvbanner ~= enginebanner then
                    return format("engine mismatch (luv: %s <> bin: %s)",luvbanner,enginebanner)
                end
                local luvhash = luv.sourcehash or "?"
                if luvhash ~= sourcehash then
                    return format("source mismatch (luv: %s <> bin: %s)",luvhash,sourcehash)
                end
                local luvluaversion = luv.luaversion or 0
                local engluaversion = LUAVERSION or 0
                if luvluaversion ~= engluaversion then
                    return format("lua mismatch (luv: %s <> bin: %s)",luvluaversion,engluaversion)
                end
                local luvfunctionality = luv.functionality or 0
                local engfunctionality = status.development_id or 0
                if luvfunctionality ~= engfunctionality then
                    return format("functionality mismatch (luv: %s <> bin: %s)",luvfunctionality,engfunctionality)
                end
                local luvformatid = luv.formatid or 0
                local engformatid = status.format_id or 0
                if luvformatid ~= engformatid then
                    return format("formatid mismatch (luv: %s <> bin: %s)",luvformatid,engformatid)
                end
            else
                return "invalid status file"
            end
        else
            return "missing status file"
        end
    end
    return true
end