summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/base/data-sch.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/base/data-sch.lua')
-rw-r--r--Master/texmf-dist/tex/context/base/data-sch.lua80
1 files changed, 55 insertions, 25 deletions
diff --git a/Master/texmf-dist/tex/context/base/data-sch.lua b/Master/texmf-dist/tex/context/base/data-sch.lua
index 253adb9f6d7..41b941c5a13 100644
--- a/Master/texmf-dist/tex/context/base/data-sch.lua
+++ b/Master/texmf-dist/tex/context/base/data-sch.lua
@@ -6,26 +6,26 @@ if not modules then modules = { } end modules ['data-sch'] = {
license = "see context related readme files"
}
-local http = require("socket.http")
-local ltn12 = require("ltn12")
+local load = load
local gsub, concat, format = string.gsub, table.concat, string.format
local finders, openers, loaders = resolvers.finders, resolvers.openers, resolvers.loaders
-local trace_schemes = false trackers.register("resolvers.schemes",function(v) trace_schemes = v end)
-
+local trace_schemes = false trackers.register("resolvers.schemes",function(v) trace_schemes = v end)
local report_schemes = logs.reporter("resolvers","schemes")
-local resolvers = resolvers
+local http = require("socket.http")
+local ltn12 = require("ltn12")
-resolvers.schemes = resolvers.schemes or { }
-local schemes = resolvers.schemes
-schemes.threshold = 24 * 60 * 60
+local resolvers = resolvers
+local schemes = resolvers.schemes or { }
+resolvers.schemes = schemes
-directives.register("schemes.threshold", function(v) schemes.threshold = tonumber(v) or schemes.threshold end)
+local cleaners = { }
+schemes.cleaners = cleaners
-local cleaners = { }
+local threshold = 24 * 60 * 60
-schemes.cleaners = cleaners
+directives.register("schemes.threshold", function(v) threshold = tonumber(v) or threshold end)
function cleaners.none(specification)
return specification.original
@@ -46,14 +46,14 @@ directives.register("schemes.cleanmethod", function(v) cleaner = cleaners[v] or
function resolvers.schemes.cleanname(specification)
local hash = cleaner(specification)
if trace_schemes then
- report_schemes("hashing %s to %s",specification.original,hash)
+ report_schemes("hashing %a to %a",specification.original,hash)
end
return hash
end
local cached, loaded, reused, thresholds, handlers = { }, { }, { }, { }, { }
-local function runcurl(name,cachename) -- will use sockets instead or the curl library
+local function runcurl(name,cachename) -- we use sockets instead or the curl library when possible
local command = "curl --silent --create-dirs --output " .. cachename .. " " .. name
os.spawn(command)
end
@@ -65,19 +65,18 @@ local function fetch(specification)
local cachename = caches.setfirstwritablefile(cleanname,"schemes")
if not cached[original] then
statistics.starttiming(schemes)
- if not io.exists(cachename) or (os.difftime(os.time(),lfs.attributes(cachename).modification) >
- (thresholds[protocol] or schemes.threshold)) then
+ if not io.exists(cachename) or (os.difftime(os.time(),lfs.attributes(cachename).modification) > (thresholds[protocol] or threshold)) then
cached[original] = cachename
local handler = handlers[scheme]
if handler then
if trace_schemes then
- report_schemes("fetching '%s', protocol '%s', method 'built-in'",original,scheme)
+ report_schemes("fetching %a, protocol %a, method %a",original,scheme,"built-in")
end
logs.flush()
handler(specification,cachename)
else
if trace_schemes then
- report_schemes("fetching '%s', protocol '%s', method 'curl'",original,scheme)
+ report_schemes("fetching %a, protocol %a, method %a",original,scheme,"curl")
end
logs.flush()
runcurl(original,cachename)
@@ -86,19 +85,19 @@ local function fetch(specification)
if io.exists(cachename) then
cached[original] = cachename
if trace_schemes then
- report_schemes("using cached '%s', protocol '%s', cachename '%s'",original,scheme,cachename)
+ report_schemes("using cached %a, protocol %a, cachename %a",original,scheme,cachename)
end
else
cached[original] = ""
if trace_schemes then
- report_schemes("using missing '%s', protocol '%s'",original,scheme)
+ report_schemes("using missing %a, protocol %a",original,scheme)
end
end
loaded[scheme] = loaded[scheme] + 1
statistics.stoptiming(schemes)
else
if trace_schemes then
- report_schemes("reusing '%s', protocol '%s'",original,scheme)
+ report_schemes("reusing %a, protocol %a",original,scheme)
end
reused[scheme] = reused[scheme] + 1
end
@@ -112,14 +111,14 @@ end
local opener = openers.file
local loader = loaders.file
-local function install(scheme,handler,threshold)
+local function install(scheme,handler,newthreshold)
handlers [scheme] = handler
loaded [scheme] = 0
reused [scheme] = 0
finders [scheme] = finder
openers [scheme] = opener
loaders [scheme] = loader
- thresholds[scheme] = threshold or schemes.threshold
+ thresholds[scheme] = newthreshold or threshold
end
schemes.install = install
@@ -160,11 +159,42 @@ statistics.register("scheme handling time", function()
end
local n = nl + nr
if n > 0 then
- l = (nl > 0 and concat(l)) or "none"
- r = (nr > 0 and concat(r)) or "none"
+ l = nl > 0 and concat(l) or "none"
+ r = nr > 0 and concat(r) or "none"
return format("%s seconds, %s processed, threshold %s seconds, loaded: %s, reused: %s",
- statistics.elapsedtime(schemes), n, schemes.threshold, l, r)
+ statistics.elapsedtime(schemes), n, threshold, l, r)
else
return nil
end
end)
+
+-- We provide a few more helpers:
+
+----- http = require("socket.http")
+local httprequest = http.request
+local toquery = url.toquery
+
+-- local function httprequest(url)
+-- return os.resultof(format("curl --silent %q", url))
+-- end
+
+local function fetchstring(url,data)
+ local q = data and toquery(data)
+ if q then
+ url = url .. "?" .. q
+ end
+ local reply = httprequest(url)
+ return reply -- just one argument
+end
+
+schemes.fetchstring = fetchstring
+
+function schemes.fetchtable(url,data)
+ local reply = fetchstring(url,data)
+ if reply then
+ local s = load("return " .. reply)
+ if s then
+ return s()
+ end
+ end
+end