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