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