forked from lualatex/lualibs
-
Notifications
You must be signed in to change notification settings - Fork 6
/
import-from-context.lua
executable file
·185 lines (161 loc) · 4.75 KB
/
import-from-context.lua
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env texlua
--[===[--
import-from-context.lua -- copy renamed copies from the context installation.
Part of the Lualibs package.
https://github.com/u-fischer/lualibs
made by adapting the existing whatsnew script.
!!!!!!!!!!!!!!!
needs a CONTEXTPATH environment variable that points to tex\texmf-context
!!!!!!!!!!!!!!!
Copyright 2013, 2018 Philipp Gesang, Ulrike Fischer
License: GPL v2.0
--]===]--
-- for the import function, to get file.copy working:
kpse.set_program_name "luatex"
require "lualibs"
-----------------------------------------------------------------------
--- configuration
-----------------------------------------------------------------------
local prefixsep = "-"
local namespace = "lualibs"
local luasuffix = ".lua"
local contextpath = os.env["CONTEXTPATH"] or ("/home/phg/context/tex/texmf-context") -- UF changed
local basedir = contextpath .. "/tex/context/base/mkiv" -- UF 20.09.2018
print(basedir)
local cmd_diff = [[diff "%s" "%s"]]
-----------------------------------------------------------------------
--- locals
-----------------------------------------------------------------------
local iopopen = io.popen
local stringexplode = string.explode
local stringformat = string.format
local stringsub = string.sub
local tableunpack = table.unpack
-----------------------------------------------------------------------
--- files
-----------------------------------------------------------------------
--- (prefix, keep) hash_t
local namespaces = { { "l" , false },
{ "util" , true },
{ "trac" , true }, }
--- (prefix, name list) hash_t
local filenames = {
["l"] = {
"boolean",
"dir",
"file",
"function",
"io",
"lpeg",
"lua",
"math",
"md5",
"number",
"os",
"package",
"set",
"string",
"table",
"unicode",
"url",
},
["trac"] = { "inf" },
["util"] = {
"deb",
"dim",
"jsn",
"lua",
"prs",
"sac",
"sta",
"sto",
"str",
"tab",
"tpl",
"zip",
},
}
-----------------------------------------------------------------------
--- helpers
-----------------------------------------------------------------------
--- string -> string -> bool -> (string, string)
local mknames = function (pfx, name, keeppfx)
local libname = basedir .. "/" .. pfx
.. prefixsep .. name .. luasuffix
local ourname = prefixsep .. name .. luasuffix
if keeppfx == true then
ourname = prefixsep .. pfx .. ourname
end
ourname = namespace .. ourname
ourname = "./" .. ourname
return ourname, libname
end
--- string -> (int * int)
local count_changes = function (str)
local added, removed = 0, 0
for n, line in next, stringexplode(str, "\n") do
local first = stringsub(line, 1, 1)
if first == "<" then
removed = removed + 1
elseif first == ">" then
added = added + 1
end
end
return added, removed
end
--- string -> string -> (int * int)
local run_diff = function (f1, f2)
local cmd = stringformat(cmd_diff, f1, f2)
local res = iopopen(cmd, "r")
local dat = res:read"*all"
res:close()
return count_changes(dat)
end
-----------------------------------------------------------------------
--- main
-----------------------------------------------------------------------
local libs_done = 0
local total_added = 0
local total_removed = 0
for n, namespace in next, namespaces do
local pfx, keeppfx = tableunpack(namespace)
local libs = filenames[pfx]
for i=1, #libs do
libs_done = libs_done + 1
local current = libs[i]
local from, to = mknames(pfx, current, keeppfx)
if lfs.isfile(from) and lfs.isfile(to) then
local added, removed = run_diff (from, to)
if added > 0 or removed > 0 then
total_added = total_added + added
total_removed = total_removed + removed
print(stringformat(
"(library %q (plus %d) (minus %d))",
from, added, removed
))
file.copy (to, from) -- added UF
print("copy from ", to,"to ",from) -- debug
end
else
if not lfs.isfile(from) then
print("cannot read file", from)
elseif not lfs.isfile(to) then
print("cannot read file", to)
end
goto fail
end
end
end
::done::
if total_added == 0 and total_removed == 0 then
print "stagnation"
else
print(stringformat(
"(progress (n-files %d) (added %d) (removed %d))",
libs_done, total_added, total_removed
))
end
os.exit(0)
::fail::
print "fatal error"
os.exit(1)