Project

General

Profile

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