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