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

scripts       = scripts       or { }
scripts.cache = scripts.cache or { }

local function collect(path)
    local all = dir.glob(path .. "/**/*")
    local tmas, tmcs, rest = { }, { }, { }
    for i=1,#all do
        local name = all[i]
        local suffix = file.suffix(name)
        if suffix == "tma" then
            tmas[#tmas+1] = name
        elseif suffix == "tmc" then
            tmcs[#tmcs+1] = name
        else
            rest[#rest+1] = name
        end
    end
    return tmas, tmcs, rest, all
end

local function list(banner,path,tmas,tmcs,rest)
    logs.report("cache",string.format("%s: %s",banner,path))
    logs.report()
    logs.report("cache",string.format("tma   : %4i",#tmas))
    logs.report("cache",string.format("tmc   : %4i",#tmcs))
    logs.report("cache",string.format("rest  : %4i",#rest))
    logs.report("cache",string.format("total : %4i",#tmas+#tmcs+#rest))
    logs.report()
end

local function purge(banner,path,list,all)
    logs.report("cache",string.format("%s: %s",banner,path))
    logs.report()
    local n = 0
    for i=1,#list do
        local filename = list[i]
        if string.find(filename,"luatex%-cache") then -- safeguard
            if all then
                os.remove(filename)
                n = n + 1
            else
                local suffix = file.suffix(filename)
                if suffix == "tma" then
                    local checkname = file.replacesuffix(filename,"tma","tmc")
                    if lfs.isfile(checkname) then
                        os.remove(filename)
                        n = n + 1
                    end
                end
            end
        end
    end
    logs.report("cache",string.format("removed tma files : %i",n))
    logs.report()
end

function scripts.cache.purge()
    local writable = caches.getwritablepath()
    local tmas, tmcs, rest = collect(writable)
    list("writable path",writable,tmas,tmcs,rest)
    local n = purge("writable path",writable,tmas)
    list("writable path",writable,tmas,tmcs,rest)
end

function scripts.cache.erase()
    local writable = caches.getwritablepath()
    local tmas, tmcs, rest, all = collect(writable)
    list("writable path",writable,tmas,tmcs,rest)
    local n = purge("writable path",writable,all,true)
    list("writable path",writable,tmas,tmcs,rest)
end

function scripts.cache.list()
    local readables = caches.getreadablepaths()
    local writable = caches.getwritablepath()
    local tmas, tmcs, rest = collect(writable)
    list("writable path",writable,tmas,tmcs,rest)
    for i=1,#readables do
        local readable = readables[i]
        if readable ~= writable then
            local tmas, tmcs = collect(readable)
            list("readable path",readable,tmas,tmcs,rest)
        end
    end
end

logs.extendbanner("ConTeXt & MetaTeX Cache Management 0.10")

messages.help = [[
--purge               remove not used files
--erase               completely remove cache
--list                show cache

--all                 all (not yet implemented)
]]

if environment.argument("purge") then
    scripts.cache.purge()
elseif environment.argument("erase") then
    scripts.cache.erase()
elseif environment.argument("list") then
    scripts.cache.list()
else
    logs.help(messages.help)
end