1 |
3761
|
aaronmk
|
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
|
2 |
|
|
root := $(selfDir_uZPPqC)..
|
3 |
|
|
include $(root)/lib/common.Makefile
|
4 |
|
|
|
5 |
|
|
|
6 |
783
|
aaronmk
|
##### Configuration
|
7 |
|
|
|
8 |
1921
|
aaronmk
|
# Command line
|
9 |
1190
|
aaronmk
|
log ?= $(if $(test),,1)
|
10 |
1257
|
aaronmk
|
profile ?=
|
11 |
1983
|
aaronmk
|
quiet ?=
|
12 |
1620
|
aaronmk
|
reverify ?= 1
|
13 |
5810
|
aaronmk
|
schema_only ?=
|
14 |
1990
|
aaronmk
|
use_staged ?= $(by_col)
|
15 |
1921
|
aaronmk
|
|
16 |
|
|
# Makefile
|
17 |
5427
|
aaronmk
|
exts ?= csv tsv txt dmp xml
|
18 |
783
|
aaronmk
|
test_n ?= 2
|
19 |
|
|
|
20 |
|
|
##### Vars/functions
|
21 |
|
|
|
22 |
1821
|
aaronmk
|
# Paths
|
23 |
5008
|
aaronmk
|
datasrc := $(patsubst .%,%,$(notdir $(realpath .)))
|
24 |
1821
|
aaronmk
|
bin := $(root)/bin
|
25 |
|
|
mappings := $(root)/mappings
|
26 |
|
|
|
27 |
368
|
aaronmk
|
# Make
|
28 |
640
|
aaronmk
|
SHELL := /bin/bash
|
29 |
1509
|
aaronmk
|
selfMake = $(MAKE) --makefile=../input.Makefile
|
30 |
1821
|
aaronmk
|
subMake = $(MAKE) $(@:$(root)/%=%) --directory=$(root)
|
31 |
404
|
aaronmk
|
+_ = $(+:_%=)
|
32 |
368
|
aaronmk
|
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
|
33 |
|
|
|
34 |
640
|
aaronmk
|
# Terminal
|
35 |
1184
|
aaronmk
|
termCols := $(shell tput cols)
|
36 |
640
|
aaronmk
|
esc := '['
|
37 |
|
|
reset := $(esc)'0m'
|
38 |
|
|
emph := $(esc)'7m '
|
39 |
|
|
endEmph := ' '$(reset)
|
40 |
|
|
|
41 |
5209
|
aaronmk
|
# User interaction
|
42 |
|
|
|
43 |
|
|
confirm = $(if $(shell read -p $(emph)"$(1)"$(endEmph)$$'$(if\
|
44 |
|
|
$(2),\n$(2))\nContinue? (y/n) ' REPLY; test "$$REPLY" = y && echo t),,\
|
45 |
|
|
$(error Aborting))
|
46 |
|
|
|
47 |
368
|
aaronmk
|
# Commands
|
48 |
1245
|
aaronmk
|
MKDIR = mkdir -p
|
49 |
|
|
mkdir = $(MKDIR) $(@D)
|
50 |
395
|
aaronmk
|
CP = cp -p
|
51 |
1184
|
aaronmk
|
diff = diff --unified=2
|
52 |
3721
|
aaronmk
|
diffIgnoreSpace = $(diff) --ignore-space-change
|
53 |
1184
|
aaronmk
|
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
|
54 |
|
|
--width=$(termCols),$(diff))
|
55 |
368
|
aaronmk
|
|
56 |
1918
|
aaronmk
|
# BIEN commands
|
57 |
4137
|
aaronmk
|
sortFilenames = $(shell $(bin)/sort_filenames $(1))
|
58 |
1524
|
aaronmk
|
selfMap = $(bin)/cols 0 0
|
59 |
6244
|
aaronmk
|
psqlAsBien := $(bin)/psql_script_vegbien
|
60 |
6186
|
aaronmk
|
psqlNoSearchPath := env no_search_path=1 $(psqlAsBien)
|
61 |
4408
|
aaronmk
|
# Usage: ($(inDatasrc); cat $(file))|$(psqlCmd)
|
62 |
6186
|
aaronmk
|
inDatasrc := echo 'SET search_path TO "$(datasrc)";'
|
63 |
353
|
aaronmk
|
|
64 |
1594
|
aaronmk
|
# SVN
|
65 |
|
|
addDir = $(if $(wildcard $(1)/),svn add --depth=empty $(1),svn mkdir $(1))
|
66 |
1771
|
aaronmk
|
setSvnIgnore = svn propset svn:ignore $(2) $(1)
|
67 |
1594
|
aaronmk
|
define addDirWithIgnore
|
68 |
|
|
$(addDir)
|
69 |
1771
|
aaronmk
|
$(setSvnIgnore)
|
70 |
1594
|
aaronmk
|
endef
|
71 |
|
|
|
72 |
4414
|
aaronmk
|
##### Environment
|
73 |
|
|
|
74 |
|
|
export PATH := $(bin):$(PATH)
|
75 |
|
|
|
76 |
783
|
aaronmk
|
##### General targets
|
77 |
250
|
aaronmk
|
|
78 |
418
|
aaronmk
|
all: _always maps ;
|
79 |
247
|
aaronmk
|
|
80 |
383
|
aaronmk
|
clean: _always
|
81 |
256
|
aaronmk
|
$(RM) $(all)
|
82 |
|
|
|
83 |
1742
|
aaronmk
|
remake: _always clean
|
84 |
|
|
+$(selfMake)
|
85 |
|
|
# re-run make so that cache of existing files is reset
|
86 |
|
|
|
87 |
5876
|
aaronmk
|
# Only remake if doesn't exist. This prevents unintentional remaking when the
|
88 |
|
|
# make script is newly checked out from svn (which sets the mod time to now) but
|
89 |
|
|
# the output is synced externally.
|
90 |
|
|
# Can't remove prereq to do this, because it determines when the rule applies.
|
91 |
|
|
make_script = $(if $(wildcard $@),,"time" ./$< >$@)
|
92 |
368
|
aaronmk
|
|
93 |
1604
|
aaronmk
|
%/: % _always ;
|
94 |
|
|
|
95 |
4379
|
aaronmk
|
%: %.make
|
96 |
5876
|
aaronmk
|
$(make_script)
|
97 |
4182
|
aaronmk
|
.PRECIOUS: % # save partial outputs of aborted src make scripts
|
98 |
1627
|
aaronmk
|
|
99 |
783
|
aaronmk
|
##### SVN
|
100 |
250
|
aaronmk
|
|
101 |
6156
|
aaronmk
|
noImportFile := _no_import
|
102 |
|
|
|
103 |
|
|
svnFilesGlob:= */{,$(noImportFile),{,.}{map,*terms,VegBIEN}.csv{,.*},*header.*,*.sql,test*.xml*}
|
104 |
6125
|
aaronmk
|
svnFilesGlob := {map.csv,*{schema,~}*.sql,{,*/}*.make,$(svnFilesGlob)}
|
105 |
6064
|
aaronmk
|
_svnFilesGlob := {_MySQL/{,*schema*.sql,*.make},_src/*.{url,pdf}}
|
106 |
5923
|
aaronmk
|
svnFiles = $(filter-out _% logs/%,$(call wildcard/,$(svnFilesGlob)))\
|
107 |
|
|
$(call wildcard/,$(_svnFilesGlob))
|
108 |
5878
|
aaronmk
|
|
109 |
4221
|
aaronmk
|
add: _always
|
110 |
4182
|
aaronmk
|
$(call setSvnIgnore,.,'*')
|
111 |
4459
|
aaronmk
|
$(call addDirWithIgnore,logs,$$'*.log.sql\n*.trace')
|
112 |
4124
|
aaronmk
|
$(call addDirWithIgnore,verify,'*.out')
|
113 |
4223
|
aaronmk
|
$(call addFile,import_order.txt)
|
114 |
6014
|
aaronmk
|
$(if $(wildcard _MySQL/),$(call addDirWithIgnore,_MySQL,'*'))
|
115 |
6064
|
aaronmk
|
$(if $(wildcard _src/),$(call addDirWithIgnore,_src,'*'))
|
116 |
5921
|
aaronmk
|
$(call add*,$(svnFiles))
|
117 |
766
|
aaronmk
|
|
118 |
4217
|
aaronmk
|
# Adds a new table subdir
|
119 |
|
|
%/add: _always
|
120 |
|
|
$(call addDirWithIgnore,$*,'*')
|
121 |
|
|
$(call addDirWithIgnore,$*/logs,$$'*.log.sql\n*.trace')
|
122 |
|
|
|
123 |
1969
|
aaronmk
|
##### Existing maps discovery
|
124 |
1955
|
aaronmk
|
|
125 |
4182
|
aaronmk
|
sortFile := import_order.txt
|
126 |
4181
|
aaronmk
|
|
127 |
6156
|
aaronmk
|
dontImport = $(wildcard $(1)/$(noImportFile))
|
128 |
|
|
|
129 |
4253
|
aaronmk
|
tables := $(if $(wildcard $(sortFile)),$(shell cat $(sortFile)))
|
130 |
|
|
# $(shell) replaces "\n" with " "
|
131 |
4182
|
aaronmk
|
allSubdirs := $(call wildcard/,*/)
|
132 |
4464
|
aaronmk
|
allTables := $(call sortFilenames,$(filter-out _% verify logs,$(allSubdirs:%/=%)))
|
133 |
4256
|
aaronmk
|
joinedTables := $(filter-out $(tables),$(allTables))
|
134 |
6170
|
aaronmk
|
allTables := $(strip $(joinedTables) $(tables))# move joined tables to beginning
|
135 |
4253
|
aaronmk
|
ifeq ($(tables),)# none specified in sort file
|
136 |
|
|
tables := $(allTables)
|
137 |
4181
|
aaronmk
|
endif
|
138 |
6156
|
aaronmk
|
importTables := $(foreach table,$(tables),$(if\
|
139 |
|
|
$(call dontImport,$(table)),,$(table)))
|
140 |
4137
|
aaronmk
|
|
141 |
5035
|
aaronmk
|
anyMap := %/map.csv %/VegBIEN.csv %/unmapped_terms.csv %/new_terms.csv
|
142 |
3574
|
aaronmk
|
|
143 |
4214
|
aaronmk
|
extsFilter := $(addprefix %.,$(exts))
|
144 |
|
|
dataOnly = $(filter $(extsFilter),$(1))
|
145 |
|
|
|
146 |
4182
|
aaronmk
|
anyTest = $*/test.%
|
147 |
4214
|
aaronmk
|
srcsOnly = $(filter-out $(anyMap) $(anyTest) %/logs,$(call dataOnly,$(1)))
|
148 |
4120
|
aaronmk
|
|
149 |
6071
|
aaronmk
|
srcDict := map.csv
|
150 |
|
|
|
151 |
4844
|
aaronmk
|
vocab := $(mappings)/VegCore.csv
|
152 |
4658
|
aaronmk
|
coreMap := $(mappings)/VegCore-VegBIEN.csv
|
153 |
4687
|
aaronmk
|
dict := $(mappings)/Veg+-VegCore.csv
|
154 |
783
|
aaronmk
|
|
155 |
6157
|
aaronmk
|
viaMaps := $(importTables:%=%/map.csv)
|
156 |
783
|
aaronmk
|
|
157 |
4117
|
aaronmk
|
autogenMaps := $(subst map.,VegBIEN.,$(viaMaps))
|
158 |
728
|
aaronmk
|
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
|
159 |
4182
|
aaronmk
|
$(wildcard */VegBIEN.csv))
|
160 |
728
|
aaronmk
|
|
161 |
1969
|
aaronmk
|
##### Sources
|
162 |
|
|
|
163 |
4182
|
aaronmk
|
srcs = $(call sortFilenames,$(call srcsOnly,$(wildcard $*/*)))
|
164 |
4450
|
aaronmk
|
nonHeaderSrcs = $(filter-out %/header.csv,$(srcs))
|
165 |
|
|
isRef = $(if $(nonHeaderSrcs),,1)
|
166 |
|
|
# empty subdir, so references an already-installed staging table
|
167 |
4452
|
aaronmk
|
isXml = $(filter %.xml,$(nonHeaderSrcs))
|
168 |
|
|
nonXml = $(if $(isXml),,1)
|
169 |
|
|
isCsv = $(if $(nonHeaderSrcs),$(if $(isXml),,1))
|
170 |
1969
|
aaronmk
|
# true if $(srcs) non-empty and contains no *.xml
|
171 |
4465
|
aaronmk
|
catSrcs = $(bin)/cat$(if $(nonXml),_csv) $(srcs)
|
172 |
1969
|
aaronmk
|
withCatSrcs = $(catSrcs:$(bin)/%=$(bin)/with_%) --
|
173 |
|
|
|
174 |
1986
|
aaronmk
|
# Usage: `make {--silent|-s} inputs/<datasrc>/cat` (don't echo make commands)
|
175 |
6157
|
aaronmk
|
cat: $(importTables:%=%/cat) _always ;
|
176 |
1969
|
aaronmk
|
|
177 |
4252
|
aaronmk
|
%/cat: _always
|
178 |
1969
|
aaronmk
|
$(catSrcs)
|
179 |
|
|
|
180 |
6363
|
aaronmk
|
##### Input data retrieval
|
181 |
|
|
|
182 |
|
|
# Must come before `%.sql: _MySQL/%.sql` to override it
|
183 |
|
|
%.sql: %.sql.make
|
184 |
|
|
$(make_script)
|
185 |
|
|
|
186 |
|
|
# The export must be created with:
|
187 |
|
|
# `--compatible=postgresql --add-locks=false --set-charset --no-create-info`
|
188 |
|
|
# Must come before `%.sql: _MySQL/%.sql` to override it
|
189 |
|
|
%.data.sql: _MySQL/%.data.sql
|
190 |
|
|
$(if $(wildcard $@),,$(bin)/my2pg.data <$< >$@)
|
191 |
|
|
|
192 |
|
|
# The export must be created with:
|
193 |
|
|
# `--compatible=postgresql --add-locks=false --set-charset`
|
194 |
|
|
# Add `--no-data` to create a schema-only export.
|
195 |
|
|
%.sql: _MySQL/%.sql
|
196 |
|
|
$(if $(wildcard $@),,$(bin)/my2pg <$< >$@)
|
197 |
|
|
|
198 |
4121
|
aaronmk
|
##### Staging tables installation
|
199 |
1921
|
aaronmk
|
|
200 |
5693
|
aaronmk
|
srcTable := %.src
|
201 |
|
|
|
202 |
6364
|
aaronmk
|
dbExportsWildcard = $(sort $(patsubst _MySQL/%.make,%,$(wildcard\
|
203 |
|
|
$(1) _MySQL/$(1).make)))
|
204 |
|
|
|
205 |
|
|
dbExports := $(call dbExportsWildcard,*schema*.sql)# schemas first
|
206 |
|
|
ifeq ($(schema_only),) # add rest of .sql files
|
207 |
|
|
dbExports += $(filter-out $(dbExports),$(call dbExportsWildcard,*.sql))
|
208 |
5810
|
aaronmk
|
endif
|
209 |
4438
|
aaronmk
|
dbExports := $(strip $(dbExports))# += adds extra whitespace
|
210 |
4425
|
aaronmk
|
allInstalls := $(if $(dbExports),sql) $(allTables)
|
211 |
1921
|
aaronmk
|
|
212 |
4413
|
aaronmk
|
install: _always schema $(allInstalls:%=%/install) ;
|
213 |
|
|
|
214 |
5209
|
aaronmk
|
uninstall: _always confirm_rm_schema rm_schema ;
|
215 |
4121
|
aaronmk
|
# rm_schema will also drop all staging tables
|
216 |
1921
|
aaronmk
|
|
217 |
|
|
reinstall: _always uninstall install ;
|
218 |
|
|
|
219 |
5209
|
aaronmk
|
confirm_rm_schema: _always
|
220 |
|
|
$(if $(filter TNRS,$(datasrc)),$(call confirm,WARNING: This will delete the\
|
221 |
|
|
TNRS cache!,To save it: make backups/TNRS.backup-remake))
|
222 |
|
|
|
223 |
4121
|
aaronmk
|
schema: _always
|
224 |
6186
|
aaronmk
|
-echo 'CREATE SCHEMA "$(datasrc)";'|$(psqlNoSearchPath)
|
225 |
1921
|
aaronmk
|
# ignore errors if schema exists
|
226 |
|
|
|
227 |
4121
|
aaronmk
|
rm_schema: _always
|
228 |
6186
|
aaronmk
|
echo 'DROP SCHEMA IF EXISTS "$(datasrc)" CASCADE;'|$(psqlNoSearchPath)
|
229 |
1921
|
aaronmk
|
|
230 |
5162
|
aaronmk
|
installLog := logs/install.log.sql
|
231 |
4453
|
aaronmk
|
|
232 |
5162
|
aaronmk
|
logInstall = $(if $(log),$(if $(quiet),$(2)$(1)$(installLog) 2>&1,2>&1|tee $(3)\
|
233 |
|
|
$(1)$(installLog)))
|
234 |
5692
|
aaronmk
|
logInstallRoot = $(call logInstall,,>)
|
235 |
5162
|
aaronmk
|
logInstall* = $(call logInstall,$*/,>)
|
236 |
|
|
logInstall*Add = $(call logInstall,$*/,>>,-a)# append to log
|
237 |
|
|
|
238 |
4413
|
aaronmk
|
# Must come before %/install to override it
|
239 |
4434
|
aaronmk
|
sql/install: $(dbExports)
|
240 |
6186
|
aaronmk
|
($(inDatasrc); cat $+|pg_dump_limit)|"time" $(psqlNoSearchPath) \
|
241 |
5796
|
aaronmk
|
--set=schema='"$(datasrc)"' $(logInstallRoot)
|
242 |
4413
|
aaronmk
|
|
243 |
5795
|
aaronmk
|
cleanup = $(if $(wildcard $*/cleanup.sql),($(inDatasrc); cat $*/cleanup.sql)\
|
244 |
6186
|
aaronmk
|
|"time" $(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add),\
|
245 |
|
|
(prefix=; . $(bin)/vegbien_dest; unset schemas; env schema=$(datasrc) table=$*\
|
246 |
|
|
$(bin)/csv2db) $(logInstall*Add))
|
247 |
4449
|
aaronmk
|
|
248 |
|
|
define exportHeader
|
249 |
|
|
$(cleanup)
|
250 |
6186
|
aaronmk
|
echo 'SELECT * FROM "$(datasrc)"."$*" LIMIT 0;'|$(psqlNoSearchPath) \
|
251 |
4449
|
aaronmk
|
--no-align --field-separator=, --pset=footer=off >$*/header.csv
|
252 |
|
|
endef
|
253 |
|
|
|
254 |
4260
|
aaronmk
|
# For staging tables which are derived by joining together other staging tables.
|
255 |
4263
|
aaronmk
|
%/install %/header.csv: %/create.sql _always
|
256 |
4527
|
aaronmk
|
($(inDatasrc); echo 'CREATE TABLE "$*" AS'; cat $<; echo ';')|"time" \
|
257 |
6186
|
aaronmk
|
$(psqlNoSearchPath) --echo-all --set=schema='"$(datasrc)"' --set=table='"$*"' \
|
258 |
5912
|
aaronmk
|
$(logInstall*)
|
259 |
4449
|
aaronmk
|
$(exportHeader)
|
260 |
5441
|
aaronmk
|
.PRECIOUS: %/header.csv
|
261 |
4260
|
aaronmk
|
|
262 |
4252
|
aaronmk
|
%/install: _always
|
263 |
4456
|
aaronmk
|
$(if $(isRef),$(exportHeader),$(if $(nonXml),$(import_install_)))
|
264 |
6078
|
aaronmk
|
$(if $(wildcard $*/postprocess.sql),($(inDatasrc); cat $*/postprocess.sql;)\
|
265 |
6186
|
aaronmk
|
|"time" $(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add))
|
266 |
5693
|
aaronmk
|
define import_install_
|
267 |
6186
|
aaronmk
|
(prefix=; . $(bin)/vegbien_dest; unset schemas; "time" nice -n +5\
|
268 |
5028
|
aaronmk
|
env schema=$(datasrc) table=$* $(bin)/csv2db $(catSrcs) $(logInstall*))
|
269 |
5693
|
aaronmk
|
$(if $(filter $(srcTable),$*),($(inDatasrc);\
|
270 |
|
|
echo 'ALTER TABLE "$(datasrc)"."$*" RENAME row_num TO "$*.row_num";')|"time"\
|
271 |
6186
|
aaronmk
|
$(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add))
|
272 |
5693
|
aaronmk
|
endef
|
273 |
5697
|
aaronmk
|
# table-scope src table's row_num col to allow joining it with other tables
|
274 |
1921
|
aaronmk
|
|
275 |
5819
|
aaronmk
|
%/uninstall: _always
|
276 |
6186
|
aaronmk
|
echo 'DROP TABLE IF EXISTS "$(datasrc)"."$*" CASCADE;'|$(psqlNoSearchPath)
|
277 |
5819
|
aaronmk
|
|
278 |
|
|
%/reinstall: _always %/uninstall %/install ;
|
279 |
|
|
|
280 |
5155
|
aaronmk
|
cleanup: _always $(tables:%=%/cleanup) ;
|
281 |
|
|
|
282 |
5184
|
aaronmk
|
# WARNING: This removes any index comments, due to a PostgreSQL bug.
|
283 |
|
|
# This occurs because ALTER TABLE recreates the index but not its comment.
|
284 |
5155
|
aaronmk
|
%/cleanup: _always
|
285 |
|
|
$(cleanup)
|
286 |
|
|
|
287 |
1777
|
aaronmk
|
##### Maps building
|
288 |
|
|
|
289 |
4345
|
aaronmk
|
# WARNING: You CANNOT make a subdir using `make inputs/<datasrc>/<subdir>/`.
|
290 |
|
|
# You must instead make the entire datasource dir: `make inputs/<datasrc>/`
|
291 |
|
|
|
292 |
4646
|
aaronmk
|
# Maps to (try to) build are added to this
|
293 |
1779
|
aaronmk
|
maps :=
|
294 |
|
|
|
295 |
4208
|
aaronmk
|
srcRoot = $(mappings)/root.sh
|
296 |
3569
|
aaronmk
|
mkSrcMap = $(catSrcs)|(. $(srcRoot); env datasrc=$(datasrc) $(bin)/src_map >$@)
|
297 |
1779
|
aaronmk
|
|
298 |
6074
|
aaronmk
|
define translate
|
299 |
|
|
$(bin)/in_place $< $(bin)/canon 1 $(1)
|
300 |
|
|
$(bin)/in_place $< $(bin)/translate 1 $(1)
|
301 |
|
|
endef
|
302 |
|
|
translate? = $(if $(wildcard $(1)),$(translate))
|
303 |
|
|
|
304 |
6076
|
aaronmk
|
$(srcDict):# empty target in case it doesn't exist
|
305 |
|
|
|
306 |
3572
|
aaronmk
|
# Via maps cleanup
|
307 |
4182
|
aaronmk
|
ifneq ($(filter %/.map.csv.last_cleanup,$(MAKECMDGOALS)),)
|
308 |
6076
|
aaronmk
|
%/.map.csv.last_cleanup: %/map.csv $(vocab) $(dict) $(coreMap) $(srcDict)
|
309 |
6074
|
aaronmk
|
$(call translate?,$(srcDict))
|
310 |
4844
|
aaronmk
|
$(bin)/in_place $< $(bin)/canon 1 $(vocab)
|
311 |
6077
|
aaronmk
|
$(call translate?,$(dict))
|
312 |
3572
|
aaronmk
|
touch $@
|
313 |
4601
|
aaronmk
|
+$(selfMake) $(<:%/map.csv=%/unmapped_terms.csv)
|
314 |
|
|
+$(selfMake) $(<:%/map.csv=%/new_terms.csv)
|
315 |
5253
|
aaronmk
|
.PRECIOUS: %/.map.csv.last_cleanup
|
316 |
3572
|
aaronmk
|
else
|
317 |
4644
|
aaronmk
|
%/map.csv: _always
|
318 |
|
|
$(if $(wildcard $@),,$(if $(nonXml),$(mkSrcMap)))
|
319 |
4182
|
aaronmk
|
+$(selfMake) $(@:%/map.csv=%/.map.csv.last_cleanup)
|
320 |
5254
|
aaronmk
|
.PRECIOUS: %/map.csv
|
321 |
3572
|
aaronmk
|
endif
|
322 |
|
|
|
323 |
4641
|
aaronmk
|
%/VegBIEN.csv: %/map.csv $(coreMap)
|
324 |
4656
|
aaronmk
|
<$< $(bin)/cat_cols 1 2|$(bin)/join $(coreMap)|$(bin)/sort_map >$@
|
325 |
1530
|
aaronmk
|
maps += $(autogenMaps)
|
326 |
|
|
|
327 |
4646
|
aaronmk
|
maps: $(maps) _always ;
|
328 |
1139
|
aaronmk
|
|
329 |
|
|
all += $(maps)
|
330 |
|
|
|
331 |
1742
|
aaronmk
|
##### Maps validation
|
332 |
|
|
|
333 |
4663
|
aaronmk
|
# `tail -n +2`: Remove header before running filter_out_ci because filter_out_ci
|
334 |
|
|
# only removes the header if it matches the vocabulary's header.
|
335 |
4601
|
aaronmk
|
|
336 |
4664
|
aaronmk
|
%/unmapped_terms.csv: %/map.csv $(coreMap)
|
337 |
4858
|
aaronmk
|
tail -n +2 $<|$(bin)/cols 1|$(bin)/filter_out_ci 0 $(coreMap) >$@
|
338 |
|
|
$(bin)/autoremove $@
|
339 |
4601
|
aaronmk
|
|
340 |
4844
|
aaronmk
|
%/new_terms.csv: %/map.csv $(vocab) $(dict) %/unmapped_terms.csv
|
341 |
4857
|
aaronmk
|
$(newTerms)
|
342 |
|
|
$(bin)/autoremove $@
|
343 |
|
|
newTerms = tail -n +2 $<|$(bin)/filter_out_ci 0 $(vocab)|$(bin)/filter_out_ci 0\
|
344 |
5926
|
aaronmk
|
$(dict) $(if $(wildcard $(word 4,$+)),|$(bin)/filter_out_ci 0 $(word 4,$+))\
|
345 |
5932
|
aaronmk
|
|grep -vE '^"?:' >$@; exit 0# because grep exits nonzero if no match
|
346 |
4601
|
aaronmk
|
|
347 |
6157
|
aaronmk
|
termsSubdirs := $(importTables)
|
348 |
4600
|
aaronmk
|
|
349 |
3764
|
aaronmk
|
include $(root)/lib/mappings.Makefile
|
350 |
1742
|
aaronmk
|
|
351 |
1509
|
aaronmk
|
##### External dependencies
|
352 |
|
|
|
353 |
|
|
$(root)/%: _always
|
354 |
|
|
+$(subMake)
|
355 |
1628
|
aaronmk
|
.PRECIOUS: $(root)/% # let ext. dir's Makefile decide whether to delete on error
|
356 |
1509
|
aaronmk
|
|
357 |
783
|
aaronmk
|
##### Mapping
|
358 |
368
|
aaronmk
|
|
359 |
4182
|
aaronmk
|
+maps = $(filter %/map.csv %/VegBIEN.csv $(mappings)/%,$(+_))
|
360 |
4746
|
aaronmk
|
map2db = env in_database=vegbien in_schema=$(datasrc) in_table=$*\
|
361 |
4748
|
aaronmk
|
out_database=vegbien $(root)/map $(+maps)
|
362 |
368
|
aaronmk
|
|
363 |
783
|
aaronmk
|
##### Import to VegBIEN
|
364 |
772
|
aaronmk
|
|
365 |
1853
|
aaronmk
|
profileTest = $(if $(profile),$(if $(test),1))
|
366 |
|
|
profileOnly = -env profile_to=/dev/fd/3 $(map2db) 3>&1 1>&2|\
|
367 |
|
|
$(bin)/profile_stats /dev/fd/0
|
368 |
|
|
|
369 |
5457
|
aaronmk
|
log_ = $*/logs/$(if $(n),n=$(n).,)$(version).log.sql
|
370 |
3317
|
aaronmk
|
trace = $(log_:.log.sql=.trace)
|
371 |
1853
|
aaronmk
|
import = -$(if $(profileTest),$(profileOnly),(set -x; "time" env commit=1\
|
372 |
1190
|
aaronmk
|
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
|
373 |
3616
|
aaronmk
|
$(if $(n),,&>$(log_)))$(if $(log),$(if $(n), 2>&1|tee -a $(log_))))
|
374 |
1088
|
aaronmk
|
# don't abort on import errors, which often relate to invalid input data
|
375 |
624
|
aaronmk
|
|
376 |
6156
|
aaronmk
|
import: $(importTables:%=%/import) _always ;
|
377 |
718
|
aaronmk
|
|
378 |
4252
|
aaronmk
|
%/import: %/VegBIEN.csv _always
|
379 |
718
|
aaronmk
|
$(import)
|
380 |
772
|
aaronmk
|
# default:
|
381 |
4252
|
aaronmk
|
%/import: _always ;
|
382 |
718
|
aaronmk
|
|
383 |
790
|
aaronmk
|
##### Log files from import
|
384 |
|
|
|
385 |
4182
|
aaronmk
|
logs := $(wildcard */logs/*.log.sql */logs/*.trace)
|
386 |
339
|
aaronmk
|
|
387 |
383
|
aaronmk
|
rm_logs: _always
|
388 |
339
|
aaronmk
|
$(RM) $(logs)
|
389 |
|
|
|
390 |
783
|
aaronmk
|
##### Verification of import
|
391 |
264
|
aaronmk
|
|
392 |
5782
|
aaronmk
|
verifyTables := $(patsubst verify/%.ref,%,$(wildcard verify/*.ref))
|
393 |
1194
|
aaronmk
|
|
394 |
5782
|
aaronmk
|
verify: $(verifyTables:%=%/verify) _always ;
|
395 |
|
|
|
396 |
4252
|
aaronmk
|
%/verify: verify/%.ref verify/%.out _always
|
397 |
1184
|
aaronmk
|
-$(diffVerbose) $(+_)
|
398 |
1088
|
aaronmk
|
# don't abort on verification errors, which are expected during development
|
399 |
1194
|
aaronmk
|
# default:
|
400 |
4252
|
aaronmk
|
%/verify: verify/%.out _always
|
401 |
1200
|
aaronmk
|
$(if $(shell test -e $< && echo t),cat $<)
|
402 |
1199
|
aaronmk
|
# don't run if verify/%.out's default do-nothing action was used
|
403 |
1200
|
aaronmk
|
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
|
404 |
368
|
aaronmk
|
|
405 |
6186
|
aaronmk
|
psqlExport := "time" $(psqlNoSearchPath) --no-align --field-separator=$$'\t'\
|
406 |
4401
|
aaronmk
|
--pset=footer=off --pset=null=NULL
|
407 |
5976
|
aaronmk
|
# Note that using $(inDatasrc) will not work with datasources whose tables are
|
408 |
|
|
# the same name as VegBIEN tables (likely only VegBank), because the datasource
|
409 |
|
|
# is first in the search_path.
|
410 |
|
|
verify = $(if $(reverify),($(inDatasrc); cat $<)|$(psqlExport)\
|
411 |
|
|
--set=datasource="'$(datasrc)'" >$@)
|
412 |
514
|
aaronmk
|
|
413 |
1199
|
aaronmk
|
verify/%.out: $(mappings)/verify.%.sql _always
|
414 |
1192
|
aaronmk
|
$(verify)
|
415 |
5973
|
aaronmk
|
.PRECIOUS: verify/%.out # save partial output in case of error to help debugging
|
416 |
1194
|
aaronmk
|
# default:
|
417 |
1199
|
aaronmk
|
verify/%.out: _always ;
|
418 |
369
|
aaronmk
|
|
419 |
1199
|
aaronmk
|
all += $(wildcard verify/*.out)
|
420 |
1192
|
aaronmk
|
|
421 |
4402
|
aaronmk
|
%.ref: %.ref.sql
|
422 |
4409
|
aaronmk
|
($(inDatasrc); cat $<)|$(psqlExport) >$@
|
423 |
5981
|
aaronmk
|
.PRECIOUS: %.ref # there must always be a .ref for the make rules to work
|
424 |
4402
|
aaronmk
|
|
425 |
1667
|
aaronmk
|
##### Editing import
|
426 |
|
|
|
427 |
4121
|
aaronmk
|
rotate: _always
|
428 |
6206
|
aaronmk
|
echo "UPDATE source SET shortname = shortname||'.$(version)'\
|
429 |
|
|
WHERE shortname = '$(datasrc)';"|$(psqlAsBien)
|
430 |
1667
|
aaronmk
|
|
431 |
4121
|
aaronmk
|
rm: _always
|
432 |
6206
|
aaronmk
|
echo "DELETE FROM source WHERE shortname = '$(datasrc)';"|$(psqlAsBien)
|
433 |
1667
|
aaronmk
|
|
434 |
783
|
aaronmk
|
##### Testing
|
435 |
368
|
aaronmk
|
|
436 |
4274
|
aaronmk
|
testRefOutput = $(subst .by_col,,$(1))
|
437 |
1986
|
aaronmk
|
testRef = $(testRefOutput).ref
|
438 |
|
|
hasOwnRef = $(filter $@,$(call testRefOutput,$@))
|
439 |
|
|
# filter returns non-empty if they are equal
|
440 |
630
|
aaronmk
|
|
441 |
4246
|
aaronmk
|
# `rm $@`: Remove outputs of successful tests to reduce clutter
|
442 |
4272
|
aaronmk
|
# `$(foreach use_staged...)`: Run with use_staged=1
|
443 |
897
|
aaronmk
|
define runTest
|
444 |
776
|
aaronmk
|
@echo "Testing $(abspath $@)..."
|
445 |
4272
|
aaronmk
|
>$@ env test=1 n=$(test_n) $(1) $(foreach use_staged,1,$(map2db))
|
446 |
4246
|
aaronmk
|
@(set -x; $(diffIgnoreSpace) $(call testRef,$@) $@) 2>&1 && rm $@ || { e=$$?;\
|
447 |
780
|
aaronmk
|
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
|
448 |
777
|
aaronmk
|
$(if $(hasOwnRef),\
|
449 |
3179
|
aaronmk
|
{\
|
450 |
|
|
read -p $(emph)'Accept new test output? (y/n)'$(endEmph) REPLY;\
|
451 |
|
|
if test "$$REPLY" = y; then\
|
452 |
|
|
(set -x; $(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile);\
|
453 |
|
|
exit 0;\
|
454 |
|
|
fi;\
|
455 |
|
|
};,\
|
456 |
777
|
aaronmk
|
echo $(emph)"Note: The preceding failed test is compared to another test's\
|
457 |
|
|
output"$(endEmph);\
|
458 |
|
|
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
|
459 |
|
|
)\
|
460 |
652
|
aaronmk
|
exit $$e;}
|
461 |
628
|
aaronmk
|
endef
|
462 |
|
|
|
463 |
876
|
aaronmk
|
tests :=
|
464 |
|
|
|
465 |
4269
|
aaronmk
|
# Requires staging tables. To create them, run `make inputs/<datasrc>/install`.
|
466 |
|
|
# Non-flat-file inputs fall back to mimicking a successful test
|
467 |
4182
|
aaronmk
|
%/test.xml: %/VegBIEN.csv _always
|
468 |
4452
|
aaronmk
|
$(if $(nonXml),$(call runTest,by_col=))
|
469 |
4182
|
aaronmk
|
tests += %/test.xml
|
470 |
627
|
aaronmk
|
|
471 |
4274
|
aaronmk
|
%/test.by_col.xml: %/VegBIEN.csv _always
|
472 |
4831
|
aaronmk
|
$(if $(nonXml),$(call runTest,by_col=1))
|
473 |
4275
|
aaronmk
|
|
474 |
|
|
# Only run column-based tests if column-based mode enabled, because these tests
|
475 |
|
|
# are much slower than the row-based tests for small numbers of rows
|
476 |
|
|
ifneq ($(by_col),)
|
477 |
4274
|
aaronmk
|
tests += %/test.by_col.xml
|
478 |
4275
|
aaronmk
|
endif
|
479 |
4274
|
aaronmk
|
|
480 |
6157
|
aaronmk
|
testOutputs := $(foreach test,$(tests),$(importTables:%=$(test)))
|
481 |
724
|
aaronmk
|
|
482 |
1361
|
aaronmk
|
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
|
483 |
|
|
|
484 |
876
|
aaronmk
|
test: _always $(testOutputs) ;
|
485 |
|
|
|
486 |
4182
|
aaronmk
|
all += $(wildcard %/test*.xml)
|
487 |
876
|
aaronmk
|
|
488 |
634
|
aaronmk
|
# Accepts a test output: make <test_output_path>-ok
|
489 |
626
|
aaronmk
|
%-ok: _always
|
490 |
4383
|
aaronmk
|
mv $* $(call testRef,$*)
|
491 |
502
|
aaronmk
|
|
492 |
4120
|
aaronmk
|
accept-all: _always
|
493 |
3579
|
aaronmk
|
+yes|$(selfMake) test
|
494 |
|
|
|
495 |
3133
|
aaronmk
|
##### Documentation
|
496 |
|
|
|
497 |
4494
|
aaronmk
|
steps = $(selfMake) -s $*/import test=1 by_col=1 verbosity=2 n=100\
|
498 |
3385
|
aaronmk
|
2>&1|$(bin)/debug2redmine >$@
|
499 |
3188
|
aaronmk
|
|
500 |
4182
|
aaronmk
|
%/logs/steps.by_col.log.sql: _always
|
501 |
3188
|
aaronmk
|
+$(steps)
|