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