grph-fil.lua /size: 6681 b    last modification: 2023-12-21 09:44
1if not modules then modules = { } end modules ['grph-fil'] = {
2    version   = 1.001,
3    comment   = "companion to grph-fig.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local type = type
10
11local trace_run  = false  trackers.register("graphic.runfile",function(v) trace_run = v end)
12local report_run = logs.reporter("graphics","run")
13
14local isfile        = lfs.isfile
15local replacesuffix = file.replacesuffix
16local addsuffix     = file.addsuffix
17local checksum      = file.checksum
18
19-- Historically running files is part of graphics processing, so this is why it
20-- sits here but is part of the job namespace.
21
22local allocate = utilities.storage.allocate
23
24local collected = allocate()
25local tobesaved = allocate()
26
27local jobfiles = {
28    collected = collected,
29    tobesaved = tobesaved,
30    forcerun  = false, -- maybe a directive some day
31}
32
33job.files = jobfiles
34
35local inputsuffix  = "tex"
36local resultsuffix = "pdf"
37
38local function initializer()
39    tobesaved = jobfiles.tobesaved
40    collected = jobfiles.collected
41end
42
43job.register('job.files.collected', tobesaved, initializer)
44
45-- When there is a runpath specified, we're already there, so then we only need to
46-- pass the orginal path. But we pass it because it will prevent prepending the
47-- current direction to the given name.
48
49local contextrunner = sandbox.registerrunner {
50    name     = "hashed context run",
51    program  = "context",
52    template = [[%options% %?path: --path=%path% ?% %?runpath: --runpath=%runpath% ?% %filename%]],
53    checkers = {
54        options  = "string",
55        filename = "readable",
56        path     = "string",
57        runpath  = "string",
58    }
59}
60
61-- we can also use:
62--
63-- local jobvariables = job.variables
64-- jobvariables.getchecksum(tag)
65-- jobvariables.makechecksum(data)
66-- jobvariables.setchecksum(tag,checksum)
67
68-- The runpath features makes things more complex than needed, so we need to wrap
69-- that some day in a helper. This is also very sensitive for both being set!
70
71function jobfiles.run(action)
72    local filename = action.filename
73    if filename and filename ~= "" then
74        local result      = action.result
75        local runner      = action.runner or contextrunner
76        local path        = action.path
77if not isfile(filename) and path and path ~= "" then
78    filename = file.join(path,filename)
79end
80        local oldchecksum = collected[filename]
81        local newchecksum = checksum(filename)
82-- print(filename,oldchecksum,newchecksum)
83        local tobedone    = false
84        local forcerun    = action.forcerun or jobfiles.forcerun
85        if not result then
86            result = replacesuffix(filename,resultsuffix)
87            action.result = result
88        end
89        if forcerun then
90            tobedone = true
91            if trace_run then
92                report_run("processing file, changes in %a, %s",filename,"processing forced")
93            end
94        end
95        if not tobedone and not oldchecksum then
96            tobedone = true
97            if trace_run then
98                report_run("processing file, changes in %a, %s",filename,"no checksum yet")
99            end
100        end
101        if not tobedone and oldchecksum ~= newchecksum then
102            tobedone = true
103            if trace_run then
104                report_run("processing file, changes in %a, %s",filename,"checksum mismatch")
105            end
106        end
107        if not tobedone and not isfile(result) then
108            tobedone = true
109            if trace_run then
110                report_run("processing file, changes in %a, %s",filename,"no result file")
111            end
112        end
113        if tobedone then
114            local kind = type(runner)
115            if kind == "function" then
116                if trace_run then
117                    report_run("processing file, command: %s",action.name or "unknown")
118                end
119                -- We can have a sandbox.registerrunner here in which case we need to make
120                -- sure that we don't feed a function into the checker. So one cannot use a
121                -- variable named "runner" in the template but that's no big deal.
122                local r = action.runner
123                action.runner = nil
124                runner(action)
125                action.runner = r
126            elseif kind == "string" then
127                -- can be anything but we assume it gets checked by the sandbox
128                if trace_run then
129                    report_run("processing file, command: %s",runner)
130                end
131                os.execute(runner)
132            else
133                report_run("processing file, changes in %a, %s",filename,"no valid runner")
134            end
135        elseif trace_run then
136            report_run("processing file, no changes in %a, %s",filename,"not processed")
137        end
138        tobesaved[filename] = newchecksum
139    else
140        -- silently ignore error
141    end
142end
143
144--
145
146local done = { }
147
148local function analyzed(name,options)
149    local usedname   = addsuffix(name,inputsuffix)      -- we assume tex if not set
150    local resultname = replacesuffix(name,resultsuffix) -- we assume tex if not set
151    local pathname   = file.pathpart(usedname)
152    local path       = environment.arguments.path -- sic, no runpath
153    local runpath    = environment.arguments.runpath
154    local resultname = replacesuffix(name,resultsuffix) -- we assume tex if not set
155    if runpath and runpath ~= "" then
156        -- not really needed but probably more robust for local leftovers
157        resultname = file.join(runpath,file.basename(resultname))
158    end
159    if path ~= "" then
160        if path then
161            path = file.join(path,pathname)
162        else
163            path = pathname
164        end
165        usedname = file.basename(usedname)
166    end
167    return {
168        options  = options,
169        path     = path,
170        filename = usedname,
171        result   = resultname,
172        runpath  = runpath,
173    }
174end
175
176function jobfiles.context(name,options) -- runpath ?
177    if type(name) == "table" then
178        local result = { }
179        for i=1,#name do
180            result[#result+1] = jobfiles.context(name[i],options)
181        end
182        return result
183    elseif name ~= "" then
184        local action = analyzed(name,options)
185        local result = action.result
186        if not done[result] then
187            jobfiles.run(action)
188            done[result] = true
189        end
190        return result
191    else
192        return { }
193    end
194end
195
196interfaces.implement {
197    name      = "runcontextjob",
198    arguments = "2 strings",
199    actions   = { jobfiles.context, context }
200}
201