1 |
14817
|
aaronmk
|
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
|
2 |
|
|
root := $(selfDir_uZPPqC)..
|
3 |
|
|
include $(root)/lib/common.Makefile
|
4 |
|
|
|
5 |
|
|
|
6 |
|
|
##### Configuration
|
7 |
|
|
|
8 |
|
|
# Command line
|
9 |
|
|
continue ?=
|
10 |
|
|
debug ?=
|
11 |
|
|
full_import ?=
|
12 |
|
|
import_source ?= 1
|
13 |
|
|
is_view ?=
|
14 |
|
|
log ?= $(if $(test),,1)
|
15 |
|
|
noclobber ?=
|
16 |
|
|
profile ?=
|
17 |
|
|
quiet ?=
|
18 |
|
|
reverify ?= 1
|
19 |
|
|
schema_only ?=
|
20 |
|
|
use_staged ?= $(by_col)
|
21 |
|
|
|
22 |
14818
|
aaronmk
|
export null_strs# don't initialize, so that it will use the default
|
23 |
|
|
|
24 |
14817
|
aaronmk
|
# Makefile
|
25 |
|
|
exts ?= csv tsv tab txt dat dmp
|
26 |
|
|
test_n ?= 2
|
27 |
|
|
|
28 |
|
|
##### Vars/functions
|
29 |
|
|
|
30 |
|
|
# Paths
|
31 |
|
|
datasrc := $(patsubst .%,%,$(notdir $(realpath .)))
|
32 |
|
|
bin := $(root)/bin
|
33 |
|
|
mappings := $(root)/mappings
|
34 |
|
|
|
35 |
|
|
# Make
|
36 |
|
|
SHELL := /bin/bash
|
37 |
|
|
selfMake = $(MAKE) --makefile=../input.Makefile
|
38 |
|
|
subMake = $(MAKE) "$(@:$(root)/%=%)" --directory=$(root)
|
39 |
|
|
+_ = $(+:_%=)
|
40 |
|
|
# used to filter the output of embedded $(shell make ...) invocations
|
41 |
|
|
filter_make := grep -vF -e lib -e ".Makefile'."
|
42 |
|
|
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
|
43 |
|
|
|
44 |
|
|
# Terminal
|
45 |
|
|
termCols := $(shell tput cols)
|
46 |
|
|
|
47 |
|
|
# Commands
|
48 |
|
|
MKDIR = mkdir -p
|
49 |
|
|
mkdir = $(MKDIR) $(@D)
|
50 |
|
|
diff = diff --unified=2
|
51 |
|
|
diffIgnoreSpace = $(diff) --ignore-space-change
|
52 |
|
|
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
|
53 |
|
|
--width=$(termCols),$(diff))
|
54 |
|
|
|
55 |
|
|
# BIEN commands
|
56 |
|
|
sortFilenames = $(shell $(bin)/sort_filenames $(1))
|
57 |
|
|
selfMap = $(bin)/cols 0 0
|
58 |
|
|
psqlAsBien := $(bin)/psql_verbose_vegbien
|
59 |
|
|
psqlNoSearchPath := env no_search_path=1 $(psqlAsBien)
|
60 |
|
|
# Usage: ($(inDatasrc); cat $(file))|$(psqlCmd)
|
61 |
|
|
inDatasrc := echo 'SET search_path TO "$(datasrc)";'
|
62 |
|
|
|
63 |
|
|
# SVN
|
64 |
|
|
setSvnIgnore = svn propset svn:ignore $(2) $(1)
|
65 |
|
|
define addDirWithIgnore
|
66 |
|
|
$(addDir)
|
67 |
|
|
$(setSvnIgnore)
|
68 |
|
|
endef
|
69 |
|
|
|
70 |
|
|
##### Environment
|
71 |
|
|
|
72 |
|
|
export PATH := $(bin):$(PATH)
|
73 |
|
|
|
74 |
|
|
##### General targets
|
75 |
|
|
|
76 |
|
|
all: _always maps ;
|
77 |
|
|
|
78 |
|
|
clean: _always
|
79 |
|
|
$(RM) $(all)
|
80 |
|
|
|
81 |
|
|
remake: _always clean
|
82 |
|
|
+$(selfMake)
|
83 |
|
|
# re-run make so that cache of existing files is reset
|
84 |
|
|
|
85 |
|
|
# Only remake if doesn't exist. This prevents unintentional remaking when the
|
86 |
|
|
# make script is newly checked out from svn (which sets the mod time to now) but
|
87 |
|
|
# the output is synced externally.
|
88 |
|
|
# Can't remove prereq to do this, because it determines when the rule applies.
|
89 |
|
|
make_script = $(if $(wildcard $@),,"time" ./$< >$@)
|
90 |
|
|
|
91 |
|
|
%/: %/map.csv _always ;
|
92 |
|
|
|
93 |
|
|
%/: % _always ;
|
94 |
|
|
|
95 |
|
|
%: %.make
|
96 |
|
|
$(make_script)
|
97 |
|
|
.PRECIOUS: % # save partial outputs of aborted src make scripts
|
98 |
|
|
|
99 |
|
|
##### Tables discovery
|
100 |
|
|
|
101 |
|
|
sortFile := import_order.txt
|
102 |
|
|
noImportFile := _no_import
|
103 |
|
|
|
104 |
|
|
dontImport = $(wildcard $(noImportFile))$(wildcard $(1)/$(noImportFile))$(if\
|
105 |
|
|
$(import_source),,$(filter Source,$(1)))
|
106 |
|
|
|
107 |
|
|
ifeq ($(sort_file_updated),)# keep $(sortFile) up-to-date
|
108 |
|
|
$(shell sort_file_updated=1 $(selfMake) $(sortFile)|$(filter_make) >&2)
|
109 |
|
|
export sort_file_updated=1
|
110 |
|
|
endif
|
111 |
|
|
|
112 |
|
|
sort_file_tables := $(if $(wildcard $(sortFile)),$(shell cat $(sortFile)))
|
113 |
|
|
# $(shell) replaces "\n" with " "
|
114 |
|
|
tables := $(sort_file_tables)
|
115 |
|
|
allSubdirs := $(call wildcard/,*/)
|
116 |
|
|
allTables := $(call sortFilenames,$(filter-out _% verify logs,$(allSubdirs:%/=%)))
|
117 |
|
|
joinedTables := $(filter-out $(tables),$(allTables))
|
118 |
|
|
allTables := $(strip $(joinedTables) $(tables))# move joined tables to beginning
|
119 |
|
|
has_visible_files = $(call wildcard/,$(1)/*)
|
120 |
|
|
allTables := $(foreach table,$(allTables),$(if\
|
121 |
|
|
$(call has_visible_files,$(table)),$(table)))
|
122 |
|
|
ifeq ($(tables),)# none specified in sort file
|
123 |
|
|
tables := $(allTables)
|
124 |
|
|
endif
|
125 |
|
|
importTables := $(foreach table,$(tables),$(if\
|
126 |
|
|
$(call dontImport,$(table)),,$(table)))
|
127 |
|
|
|
128 |
|
|
list_tables: _always # use `make -s` to avoid echoing commands
|
129 |
|
|
@for table in $(tables); do echo "$$table"; done
|
130 |
|
|
|
131 |
|
|
$(sortFile): _always
|
132 |
|
|
$(if $(filter-out $(sort_file_tables),$(tables)),$(selfMake) -s list_tables >$@)
|
133 |
|
|
# add any missing tables to $(sortFile)
|
134 |
|
|
|
135 |
|
|
##### SVN
|
136 |
|
|
|
137 |
|
|
svnFilesGlob:= */{,{,.}{data,map,VegBIEN}.csv{,.*},*header.*,*.sql,test.xml.ref}
|
138 |
14846
|
aaronmk
|
svnFilesGlob := {*.log,map.csv,*{grants,validations,~}*.sql,{,*/}{*.make,*terms.csv},$(svnFilesGlob)}
|
139 |
14817
|
aaronmk
|
_svnFilesGlob := {_MySQL/{,*.make},{,*/}{$(noImportFile),*{run,Makefile},*.{md5,url},*README.TXT}}
|
140 |
|
|
svnFiles = $(filter-out _% logs/% %.data.sql,$(call wildcard/,$(svnFilesGlob)))\
|
141 |
|
|
$(call wildcard/,$(_svnFilesGlob))
|
142 |
|
|
|
143 |
|
|
add: _always $(if $(call dontImport,.),,add!) ;
|
144 |
|
|
|
145 |
|
|
# To update all inputs with these settings: make inputs/add
|
146 |
|
|
add!: _always Source/add $(allTables:%=%/add)
|
147 |
|
|
$(call setSvnIgnore,.,'*')
|
148 |
|
|
$(call addDirWithIgnore,logs,$$'*.gz\n*.log.sql\n*.trace')
|
149 |
14843
|
aaronmk
|
$(call addDirWithIgnore,verify,$$'*.csv\n*.log\n*.out\n*.tsv\n*.txt\n*.xls\n*.xlsx\n*.zip')
|
150 |
14817
|
aaronmk
|
$(call addFile,import_order.txt)
|
151 |
|
|
$(if $(wildcard _MySQL/),$(call addDirWithIgnore,_MySQL,'*'))
|
152 |
|
|
$(if $(wildcard _src/),$(call addDirWithIgnore,_src,'*'))
|
153 |
|
|
$(if $(wildcard _archive/),$(call addDirWithIgnore,_archive,'*'))
|
154 |
|
|
# invoke externally to clear $$(wildcard) cache before expanding $$(svnFiles)
|
155 |
|
|
$(selfMake) add_files
|
156 |
|
|
|
157 |
|
|
add_files: _always
|
158 |
|
|
$(call add*,$(svnFiles))
|
159 |
|
|
|
160 |
|
|
# Adds a new table subdir
|
161 |
|
|
%/add: _always
|
162 |
|
|
$(call addDirWithIgnore,$*,'*')
|
163 |
|
|
$(call addDirWithIgnore,$*/logs,$$'*.gz\n*.log.sql\n*.trace')
|
164 |
|
|
|
165 |
|
|
##### Existing maps discovery
|
166 |
|
|
|
167 |
|
|
anyMap := %/map.csv %/VegBIEN.csv %/unmapped_terms.csv %/new_terms.csv
|
168 |
|
|
|
169 |
|
|
exts := $(call ci,$(exts))
|
170 |
|
|
extsFilter := $(addprefix %.,$(exts))
|
171 |
|
|
dataOnly = $(filter $(extsFilter),$(1))
|
172 |
|
|
|
173 |
|
|
anyTest = $*/test.%
|
174 |
|
|
srcsOnly = $(filter-out $(anyMap) $(anyTest) %/logs,$(call dataOnly,$(1)))
|
175 |
|
|
|
176 |
|
|
srcDict := map.csv
|
177 |
|
|
|
178 |
|
|
vocab := $(mappings)/VegCore.vocab.csv
|
179 |
|
|
thesaurus := $(mappings)/VegCore.thesaurus.csv
|
180 |
|
|
coreMap := $(mappings)/VegCore-VegBIEN.csv
|
181 |
|
|
|
182 |
|
|
viaMaps := $(tables:%=%/map.csv)
|
183 |
|
|
|
184 |
|
|
autogenMaps := $(subst map.,VegBIEN.,$(viaMaps))
|
185 |
|
|
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
|
186 |
|
|
$(wildcard */VegBIEN.csv))
|
187 |
|
|
|
188 |
|
|
##### Sources
|
189 |
|
|
|
190 |
|
|
srcs = $(call sortFilenames,$(call srcsOnly,$(wildcard $*/*)))
|
191 |
|
|
nonHeaderSrcs = $(filter-out %/header.csv %/header.txt,$(srcs))
|
192 |
|
|
isRef = $(if $(nonHeaderSrcs),,1)
|
193 |
|
|
# empty subdir, so references an already-installed staging table
|
194 |
|
|
catSrcs = $(bin)/cat_csv $(nonHeaderSrcs)
|
195 |
|
|
withCatSrcs = $(catSrcs:$(bin)/%=$(bin)/with_%) --
|
196 |
|
|
|
197 |
|
|
%/list_srcs: _always # use `make -s` to avoid echoing commands
|
198 |
|
|
echo $(nonHeaderSrcs)
|
199 |
|
|
|
200 |
|
|
# Run with `make -s` to avoid echoing make commands
|
201 |
|
|
cat: $(importTables:%=%/cat) _always ;
|
202 |
|
|
|
203 |
|
|
%/cat: _always
|
204 |
|
|
$(catSrcs)
|
205 |
|
|
|
206 |
|
|
##### Input data retrieval
|
207 |
|
|
|
208 |
|
|
# Must come before `%.sql: _MySQL/%.sql` to override it
|
209 |
|
|
%.sql: %.sql.make
|
210 |
|
|
$(make_script)
|
211 |
|
|
|
212 |
|
|
# The export must be created with:
|
213 |
|
|
# `--compatible=postgresql --add-locks=false --set-charset --no-create-info`
|
214 |
|
|
# Must come before `%.sql: _MySQL/%.sql` to override it
|
215 |
|
|
%.data.sql: _MySQL/%.data.sql
|
216 |
|
|
$(if $(wildcard $@),,$(bin)/my2pg.data <$< >$@)
|
217 |
|
|
|
218 |
|
|
# The export must be created with:
|
219 |
|
|
# `--compatible=postgresql --add-locks=false --set-charset`
|
220 |
|
|
# Add `--no-data` to create a schema-only export.
|
221 |
|
|
%.sql: _MySQL/%.sql
|
222 |
|
|
$(if $(wildcard $@),,$(bin)/my2pg <$< >$@)
|
223 |
|
|
|
224 |
|
|
##### Staging tables installation
|
225 |
|
|
|
226 |
|
|
srcTable := %.src
|
227 |
|
|
|
228 |
|
|
dbExportsWildcard = $(sort $(patsubst _MySQL/%.make,%,$(wildcard\
|
229 |
|
|
$(1) _MySQL/$(1).make)))
|
230 |
|
|
|
231 |
14825
|
aaronmk
|
dbExports := $(call dbExportsWildcard,*schema*.sql *data*.sql)
|
232 |
|
|
# schema/data in order, and before clean_up.sql, etc
|
233 |
14817
|
aaronmk
|
ifeq ($(schema_only),) # add rest of .sql files
|
234 |
|
|
dbExports += $(filter-out $(dbExports),$(call dbExportsWildcard,*.sql))
|
235 |
|
|
endif
|
236 |
|
|
dbExports := $(filter-out grants.sql,$(dbExports))
|
237 |
|
|
dbExports := $(strip $(dbExports))# += adds extra whitespace
|
238 |
|
|
allInstalls := $(if $(dbExports),sql) $(allTables)
|
239 |
|
|
|
240 |
|
|
datasrc_schema_exists = $(shell schema=$(datasrc)\
|
241 |
|
|
$(root)/lib/runscripts/local.run pg_schema_exists && echo t)
|
242 |
|
|
|
243 |
|
|
install: _always
|
244 |
|
|
$(if $(wildcard ./run),./run install,$(if $(wildcard table.run),$(if\
|
245 |
|
|
$(datasrc_schema_exists),,+$(selfMake) install_oldstyle)))
|
246 |
|
|
+$(selfMake) validate/install
|
247 |
|
|
# table.run: only run this for datasource dirs
|
248 |
|
|
|
249 |
|
|
install_oldstyle: _always schema $(allInstalls:%=%/install) ;
|
250 |
|
|
|
251 |
|
|
uninstall: _always confirm_rm_schema rm_schema ;
|
252 |
|
|
# rm_schema will also drop all staging tables
|
253 |
|
|
|
254 |
|
|
reinstall: _always uninstall install ;
|
255 |
|
|
|
256 |
|
|
confirm_rm_schema: _always
|
257 |
|
|
$(if $(filter TNRS,$(datasrc)),$(call confirm,WARNING: This will delete the\
|
258 |
|
|
TNRS cache!,To save it: make backups/TNRS.backup-remake))
|
259 |
|
|
|
260 |
|
|
schema: _always
|
261 |
|
|
-echo 'CREATE SCHEMA "$(datasrc)";'|$(psqlNoSearchPath)
|
262 |
|
|
# ignore errors if schema exists
|
263 |
|
|
|
264 |
|
|
rm_schema: _always
|
265 |
|
|
echo 'DROP SCHEMA IF EXISTS "$(datasrc)" CASCADE;'|$(psqlNoSearchPath)
|
266 |
|
|
|
267 |
|
|
installLog := logs/install.log.sql
|
268 |
|
|
|
269 |
|
|
log_dir = $(1)logs
|
270 |
|
|
has_log_dir = $(call and,$(wildcard $(log_dir)),$(call not,$(noclobber)))
|
271 |
|
|
logInstall = $(if $(has_log_dir),$(if $(quiet),$(2)$(1)$(installLog)\
|
272 |
|
|
2>&1,2>&1|tee $(3) $(1)$(installLog)))
|
273 |
|
|
logInstallRoot = $(call logInstall,,>)
|
274 |
|
|
logInstall* = $(call logInstall,$*/,>)
|
275 |
|
|
logInstall*Add = $(call logInstall,$*/,>>,-a)# append to log
|
276 |
|
|
|
277 |
|
|
# Must come before %/install to override it
|
278 |
|
|
sql/install: $(dbExports)
|
279 |
|
|
set -o pipefail; ($(inDatasrc); $(if $(wildcard schema.sql),cat schema.sql;)\
|
280 |
|
|
cat $(filter-out schema.sql grants.sql,$+)|pg_dump_limit $(if $(wildcard\
|
281 |
|
|
grants.sql),; cat grants.sql))|"time" env no_search_path=1 \
|
282 |
|
|
$(bin)/psql_$(if $(debug),verbose,script)_vegbien --set=schema='"$(datasrc)"' \
|
283 |
|
|
$(logInstallRoot)
|
284 |
|
|
|
285 |
|
|
# $debug option runs the *.sql import verbosely, to display which statements are
|
286 |
|
|
# being run. this should only be used for SQL files that use COPY FROM to import
|
287 |
|
|
# data, to avoid echoing pages of insert statements.
|
288 |
14884
|
aaronmk
|
cleanup = echo [[[[[[[[$$verbosity >&2; set -o pipefail; \
|
289 |
14817
|
aaronmk
|
$(if $(wildcard $*/cleanup.sql),($(inDatasrc); cat $*/cleanup.sql)\
|
290 |
|
|
|"time" $(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add),\
|
291 |
|
|
(export schema=$(datasrc) table=$*; . $(bin)/vegbien_dest; unset schemas; \
|
292 |
|
|
$(bin)/csv2db) $(logInstall*Add))
|
293 |
|
|
|
294 |
|
|
%/header.csv:
|
295 |
|
|
set -o pipefail; \
|
296 |
|
|
echo 'COPY (SELECT * FROM "$(datasrc)"."$*" LIMIT 0) TO STDOUT CSV HEADER;'|\
|
297 |
|
|
env no_search_path=1 $(bin)/psql_script_vegbien >$*/header.csv
|
298 |
|
|
|
299 |
|
|
exportHeader = $(selfMake) "$*/header.csv"
|
300 |
|
|
|
301 |
|
|
# Don't try to edit a view. Must come before %/install to override it.
|
302 |
|
|
%_view/install: _always ;
|
303 |
|
|
|
304 |
|
|
%.sql/run: _always
|
305 |
|
|
$(if $(wildcard $(@D)),($(inDatasrc); cat $(@D))|(cd '$(*D)';\
|
306 |
|
|
"time" env no_search_path=1 ../$(bin)/psql_verbose_vegbien \
|
307 |
|
|
--set=table='"$(*D)"' --set=table_str=\''"$(*D)"'\'))
|
308 |
|
|
|
309 |
|
|
%/postprocess.sql: $(thesaurus) _always
|
310 |
|
|
$(if $(wildcard $*/run),$(bin)/in_place $@ env text=1 $(bin)/repl $<)
|
311 |
|
|
|
312 |
|
|
%/postprocess: _always
|
313 |
|
|
$(if\
|
314 |
|
|
$(wildcard $*/run),$*/run postprocess,$(selfMake) "$*/postprocess.sql/run")
|
315 |
|
|
|
316 |
|
|
%/map_table: _always
|
317 |
|
|
$(if $(wildcard $*/run),$*/run map_table)
|
318 |
|
|
|
319 |
|
|
# For staging tables which are derived by joining together other staging tables.
|
320 |
|
|
%/install: %/create.sql _always
|
321 |
|
|
set -o pipefail; \
|
322 |
|
|
($(inDatasrc); echo 'CREATE TABLE "$*" AS'; cat $<; echo ';')|"time" \
|
323 |
|
|
$(psqlNoSearchPath) --echo-all --set=schema='"$(datasrc)"' --set=table='"$*"' \
|
324 |
|
|
$(logInstall*)
|
325 |
|
|
$(exportHeader)
|
326 |
14827
|
aaronmk
|
$(cleanup)
|
327 |
14817
|
aaronmk
|
$(selfMake) "$*/postprocess"
|
328 |
|
|
|
329 |
|
|
%/install: _always
|
330 |
14884
|
aaronmk
|
echo [[[[[[[[$$verbosity >&2
|
331 |
14817
|
aaronmk
|
$(import_install_)
|
332 |
|
|
$(exportHeader)
|
333 |
14827
|
aaronmk
|
$(cleanup)
|
334 |
14817
|
aaronmk
|
$(selfMake) "$*/postprocess"
|
335 |
|
|
define import_install_
|
336 |
|
|
set -o pipefail; (. $(bin)/vegbien_dest; unset schemas; "time" $(nice)\
|
337 |
|
|
env schema=$(datasrc) table=$* $(bin)/csv2db $(catSrcs) $(logInstall*))
|
338 |
|
|
$(if $(filter $(srcTable),$*),($(inDatasrc);\
|
339 |
|
|
echo 'ALTER TABLE "$(datasrc)"."$*" RENAME row_num TO "$*.row_num";')|"time"\
|
340 |
|
|
$(psqlNoSearchPath) --echo-all --set=table='"$*"' $(logInstall*Add))
|
341 |
|
|
endef
|
342 |
|
|
# table-scope src table's row_num col to allow joining it with other tables
|
343 |
|
|
|
344 |
|
|
%/uninstall: _always
|
345 |
|
|
echo 'DROP $(if\
|
346 |
|
|
$(is_view),VIEW,TABLE) IF EXISTS "$(datasrc)"."$*" CASCADE;'|$(psqlNoSearchPath)
|
347 |
|
|
|
348 |
|
|
%/reinstall: _always %/uninstall %/install ;
|
349 |
|
|
|
350 |
|
|
postprocess: _always $(allTables:%=%/postprocess) ;
|
351 |
|
|
|
352 |
|
|
cleanup: _always $(tables:%=%/cleanup) ;
|
353 |
|
|
|
354 |
|
|
# WARNING: This removes any index comments, due to a PostgreSQL bug.
|
355 |
|
|
# This occurs because ALTER TABLE recreates the index but not its comment.
|
356 |
|
|
%/cleanup: _always
|
357 |
|
|
$(cleanup)
|
358 |
|
|
|
359 |
|
|
##### Maps building
|
360 |
|
|
|
361 |
|
|
# Maps to (try to) build are added to this
|
362 |
|
|
maps :=
|
363 |
|
|
|
364 |
|
|
srcRoot = $(mappings)/root.sh
|
365 |
|
|
mkSrcMap = (. $(srcRoot); env datasrc=$(datasrc) $(bin)/src_map <$*/header.csv\
|
366 |
|
|
>$@)
|
367 |
|
|
|
368 |
|
|
translate = $(if $(wildcard $(1)),$(bin)/in_place $< $(bin)/translate_ci 1 $(1))
|
369 |
|
|
|
370 |
|
|
$(srcDict):# empty target in case it doesn't exist
|
371 |
|
|
|
372 |
14867
|
aaronmk
|
.PRECIOUS: %/map.csv
|
373 |
|
|
# save map.csv if errors occur in unmapped_terms.csv, new_terms.csv
|
374 |
|
|
|
375 |
14817
|
aaronmk
|
# Via maps cleanup
|
376 |
|
|
ifneq ($(filter %/.map.csv.last_cleanup,$(MAKECMDGOALS)),)
|
377 |
|
|
%/.map.csv.last_cleanup: %/map.csv $(vocab) $(thesaurus) $(coreMap) $(srcDict)
|
378 |
|
|
$(call translate,$(srcDict))
|
379 |
|
|
$(bin)/in_place $< $(bin)/canon 1 $(vocab)
|
380 |
|
|
$(call translate,$(thesaurus))
|
381 |
|
|
$(bin)/in_place $< $(bin)/fix_line_endings
|
382 |
|
|
touch $@
|
383 |
|
|
+$(selfMake) $(<:%/map.csv=%/unmapped_terms.csv)
|
384 |
|
|
+$(selfMake) $(<:%/map.csv=%/new_terms.csv)
|
385 |
|
|
.PRECIOUS: %/.map.csv.last_cleanup
|
386 |
|
|
else
|
387 |
|
|
%/map.csv: _always
|
388 |
|
|
$(if $(wildcard $@),,$(mk_map_csv))
|
389 |
|
|
+$(selfMake) $(@:%/map.csv=%/.map.csv.last_cleanup)
|
390 |
|
|
define mk_map_csv
|
391 |
|
|
+$(selfMake) "$*/header.csv"
|
392 |
|
|
$(mkSrcMap)
|
393 |
|
|
endef
|
394 |
|
|
endif
|
395 |
|
|
|
396 |
|
|
%/VegBIEN.csv: %/map.csv $(coreMap)
|
397 |
|
|
$(if $(wildcard $*/run),-ln -s "../$(coreMap)" "$@"\
|
398 |
|
|
,<$< $(bin)/cat_cols 1 2|$(bin)/join $(coreMap)|$(bin)/sort_map >$@)
|
399 |
|
|
# ignore errors if symlink exists
|
400 |
|
|
maps += $(autogenMaps)
|
401 |
|
|
|
402 |
|
|
maps: $(maps) _always ;
|
403 |
|
|
|
404 |
|
|
all += $(maps)
|
405 |
|
|
|
406 |
|
|
##### Maps validation
|
407 |
|
|
|
408 |
|
|
# `tail -n +2`: Remove header before running filter_out_ci because filter_out_ci
|
409 |
|
|
# only removes the header if it matches the vocabulary's header.
|
410 |
|
|
|
411 |
|
|
%/unmapped_terms.csv: %/map.csv $(coreMap)
|
412 |
|
|
tail -n +2 $<|$(bin)/cols 1|$(bin)/filter_out_ci 0 $(coreMap) >$@
|
413 |
|
|
$(bin)/autoremove $@
|
414 |
|
|
|
415 |
|
|
%/new_terms.csv: %/map.csv $(vocab) $(thesaurus) %/unmapped_terms.csv
|
416 |
|
|
$(newTerms)
|
417 |
|
|
$(bin)/autoremove $@
|
418 |
|
|
newTerms = tail -n +2 $<|$(bin)/filter_out_ci 0 $(vocab)|$(bin)/filter_out_ci 0\
|
419 |
|
|
$(thesaurus) $(if $(wildcard $(word 4,$+)),|$(bin)/filter_out_ci 0 $(word 4,$+))\
|
420 |
|
|
|grep -vE -e '^"?:' -e 'UNUSED' >$@; exit 0 # grep exits nonzero if no match
|
421 |
|
|
|
422 |
|
|
termsSubdirs := $(tables)
|
423 |
|
|
|
424 |
|
|
include $(root)/lib/mappings.Makefile
|
425 |
|
|
|
426 |
|
|
##### External dependencies
|
427 |
|
|
|
428 |
|
|
$(root)/%: _always
|
429 |
|
|
+$(subMake)
|
430 |
|
|
.PRECIOUS: $(root)/% # let ext. dir's Makefile decide whether to delete on error
|
431 |
|
|
|
432 |
|
|
##### Mapping
|
433 |
|
|
|
434 |
|
|
+maps = $(filter %/map.csv %/VegBIEN.csv $(mappings)/%,$(+_))
|
435 |
|
|
map2db = env in_database=vegbien in_schema=$(datasrc) in_table=$*\
|
436 |
|
|
out_database=vegbien source=$(datasrc).new $(root)/map $(+maps)
|
437 |
|
|
|
438 |
|
|
##### Import to VegBIEN
|
439 |
|
|
|
440 |
|
|
import_temp: $(importTables:%=%/import) _always ;
|
441 |
|
|
|
442 |
|
|
import: _always import_temp publish validate ; # removes temp suffix when done
|
443 |
|
|
|
444 |
|
|
rm: _always
|
445 |
|
|
echo "SELECT datasource_rm('$(datasrc)');"|"time" $(psqlAsBien)
|
446 |
|
|
|
447 |
|
|
reimport: _always import ; # import replaces the previous import
|
448 |
|
|
|
449 |
|
|
profileTest = $(if $(profile),$(if $(test),1))
|
450 |
|
|
profileOnly = -env profile_to=/dev/fd/3 $(map2db) 3>&1 1>&2|\
|
451 |
|
|
$(bin)/profile_stats /dev/fd/0
|
452 |
|
|
|
453 |
|
|
log_ = $*/logs/$(if $(n),n=$(n).,)$(version).log.sql
|
454 |
|
|
|
455 |
|
|
# Displays the import log file path
|
456 |
|
|
# Run with `make -s` to avoid echoing make commands
|
457 |
|
|
%/log_file: _always
|
458 |
|
|
echo $$(pwd)/$(log_)
|
459 |
|
|
|
460 |
|
|
trace = $(log_:.log.sql=.trace)
|
461 |
|
|
restart_row = $(shell set -x; grep -F Partition: $(log_)|tail -1|$(sed)\
|
462 |
|
|
's/^.* rows ([[:digit:]]+)-.*$$/\1/')
|
463 |
|
|
import = $(if $(profileTest),$(profileOnly),(set -x; date; "time" env commit=1\
|
464 |
|
|
$(if $(profile),profile_to=$(trace)) $(if $(continue),start=$(restart_row))\
|
465 |
|
|
$(map2db))$(if $(log), >>$(log_) 2>&1))
|
466 |
|
|
|
467 |
|
|
import? = $(if $(call and,$(full_import),$(call dontImport,.)),,$(import))
|
468 |
|
|
|
469 |
|
|
%/import_temp: %/VegBIEN.csv _always
|
470 |
|
|
$(if $(full_import),-)$(import?)
|
471 |
|
|
# don't abort on import errors, which often relate to invalid input data
|
472 |
|
|
# default:
|
473 |
|
|
%/import_temp: _always ;
|
474 |
|
|
|
475 |
|
|
%/import: %/import_temp _always ;
|
476 |
|
|
|
477 |
|
|
#### Taxonomic scrubbing
|
478 |
|
|
|
479 |
|
|
scrub: _always
|
480 |
|
|
$(selfMake) $(root)/scrub & echo $$!
|
481 |
|
|
# using & (background process) also ignores TNRS errors, so that TNRS bugs do
|
482 |
|
|
# not prevent the remaining tables from being imported even if TNRS can't be run
|
483 |
|
|
|
484 |
|
|
# Removes TNRS taxondeterminations
|
485 |
|
|
unscrub: _always
|
486 |
|
|
echo "SELECT delete_scrubbed_taxondeterminations('$(datasrc)');"|\
|
487 |
|
|
"time" env no_query_results=1 $(psqlAsBien)
|
488 |
|
|
|
489 |
|
|
rescrub: _always unscrub scrub ;
|
490 |
|
|
|
491 |
|
|
import_scrub: _always import scrub ;
|
492 |
|
|
|
493 |
|
|
reimport_scrub: _always reimport scrub ;
|
494 |
|
|
|
495 |
|
|
%/import_scrub: _always %/import scrub ;
|
496 |
|
|
|
497 |
|
|
##### Log files from import
|
498 |
|
|
|
499 |
|
|
logs := $(wildcard */logs/*.log.sql */logs/*.trace)
|
500 |
|
|
|
501 |
|
|
rm_logs: _always
|
502 |
|
|
$(RM) $(logs)
|
503 |
|
|
|
504 |
|
|
##### Verification of import
|
505 |
|
|
|
506 |
|
|
### new-style aggregating validations (also work w/ old-style import)
|
507 |
|
|
|
508 |
|
|
# must be in input.Makefile instead of table.run because some datasources that
|
509 |
|
|
# we validate still use old-style import
|
510 |
|
|
|
511 |
|
|
# validations.sql must be in a subdir so it won't get run by sql/install
|
512 |
|
|
validate/install: _always verify/validations.sql/run ;
|
513 |
|
|
|
514 |
|
|
validate: _always
|
515 |
|
|
echo "SELECT remake_diff_tables('$(datasrc)');"\
|
516 |
|
|
|$(psqlAsBien)$(if $(log), >>.$(log_) 2>&1)
|
517 |
|
|
|
518 |
|
|
### old-style aggregating validations (not related to old-style import)
|
519 |
|
|
|
520 |
|
|
verifyTables := $(patsubst verify/%.ref,%,$(wildcard verify/*.ref))
|
521 |
|
|
|
522 |
|
|
verify: $(verifyTables:%=%/verify) _always ;
|
523 |
|
|
|
524 |
|
|
%/verify: verify/%.ref verify/%.out _always
|
525 |
|
|
-$(diffVerbose) $(+_)
|
526 |
|
|
# don't abort on verification errors, which are expected during development
|
527 |
|
|
# default:
|
528 |
|
|
%/verify: verify/%.out _always
|
529 |
|
|
$(if $(shell test -e $< && echo t),cat $<)
|
530 |
|
|
# don't run if verify/%.out's default do-nothing action was used
|
531 |
|
|
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
|
532 |
|
|
|
533 |
|
|
psqlExport := "time" $(psqlNoSearchPath) --no-align --field-separator=$$'\t'\
|
534 |
|
|
--pset=footer=off --pset=null=NULL
|
535 |
|
|
# Note that using $(inDatasrc) will not work with datasources whose tables are
|
536 |
|
|
# the same name as VegBIEN tables (likely only VegBank), because the datasource
|
537 |
|
|
# is first in the search_path.
|
538 |
|
|
verify = $(if $(reverify),($(inDatasrc); cat $<)|$(psqlExport)\
|
539 |
|
|
--set=datasource="'$(datasrc)'" >$@)
|
540 |
|
|
|
541 |
|
|
verify/%.out: verify/%.out.sql _always
|
542 |
|
|
$(verify)
|
543 |
|
|
.PRECIOUS: verify/%.out # save partial output in case of error to help debugging
|
544 |
|
|
# default:
|
545 |
|
|
verify/%.out: _always ;
|
546 |
|
|
|
547 |
|
|
all += $(wildcard verify/*.out)
|
548 |
|
|
|
549 |
|
|
%.ref: %.ref.sql
|
550 |
|
|
($(inDatasrc); cat $<)|$(psqlExport) >$@
|
551 |
|
|
.PRECIOUS: %.ref # there must always be a .ref for the make rules to work
|
552 |
|
|
|
553 |
|
|
##### Editing import
|
554 |
|
|
|
555 |
|
|
rename/%: _always
|
556 |
|
|
echo "UPDATE source SET shortname = '$*' WHERE shortname = '$(datasrc)';"\
|
557 |
|
|
|$(psqlAsBien)
|
558 |
|
|
|
559 |
|
|
%/publish: _always # usage: make inputs/src/src.version/publish
|
560 |
|
|
echo "SELECT datasource_publish('$*');"|$(psqlAsBien)
|
561 |
|
|
|
562 |
|
|
publish: _always $(datasrc).new/publish ; # usage: make inputs/src/publish
|
563 |
|
|
|
564 |
|
|
##### Testing
|
565 |
|
|
|
566 |
|
|
testRefOutput = $(subst .by_col,,$(1))
|
567 |
|
|
testRef = $(testRefOutput).ref
|
568 |
|
|
hasOwnRef = $(filter $@,$(call testRefOutput,$@))
|
569 |
|
|
# filter returns non-empty if they are equal
|
570 |
|
|
|
571 |
|
|
# `rm $@`: Remove outputs of successful tests to reduce clutter
|
572 |
|
|
# `$(foreach use_staged...)`: Run with use_staged=1
|
573 |
|
|
define runTest
|
574 |
|
|
@echo "Testing $(abspath $@)..."
|
575 |
|
|
>$@ env test=1 n=$(test_n) $(1) $(foreach use_staged,1,$(map2db))
|
576 |
|
|
$(if $(hasOwnRef),,-)@(set -x; $(diffIgnoreSpace) $(call testRef,$@) $@) 2>&1\
|
577 |
|
|
&& rm $@ || { e=$$?; $(if $(wildcard $(call testRef,$@)),,cat $@;)\
|
578 |
|
|
$(if $(hasOwnRef),\
|
579 |
|
|
{\
|
580 |
|
|
read -p $(emph)'Accept new test output? (y/n)'$(endEmph) REPLY;\
|
581 |
|
|
if test "$$REPLY" = y; then\
|
582 |
|
|
(set -x; $(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile);\
|
583 |
|
|
exit 0;\
|
584 |
|
|
fi;\
|
585 |
|
|
};,\
|
586 |
|
|
echo $(emph)"Note: The preceding failed test is compared to another test's\
|
587 |
|
|
output"$(endEmph);\
|
588 |
|
|
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
|
589 |
|
|
)\
|
590 |
|
|
exit $$e;}
|
591 |
|
|
endef
|
592 |
|
|
|
593 |
|
|
tests :=
|
594 |
|
|
|
595 |
|
|
%/test: %/test.xml $(if $(by_col),%/test.by_col.xml) _always ;
|
596 |
|
|
|
597 |
|
|
# Requires staging tables. To create them, run `make inputs/<datasrc>/install`.
|
598 |
|
|
# Non-flat-file inputs fall back to mimicking a successful test
|
599 |
|
|
%/test.xml: %/VegBIEN.csv _always
|
600 |
|
|
$(call runTest,by_col=)
|
601 |
|
|
tests += %/test.xml
|
602 |
|
|
|
603 |
|
|
%/test.by_col.xml: %/VegBIEN.csv _always
|
604 |
|
|
$(call runTest,by_col=1)
|
605 |
|
|
|
606 |
|
|
# Only run column-based tests if column-based mode enabled, because these tests
|
607 |
|
|
# are much slower than the row-based tests for small numbers of rows
|
608 |
|
|
ifneq ($(by_col),)
|
609 |
|
|
tests += %/test.by_col.xml
|
610 |
|
|
endif
|
611 |
|
|
|
612 |
|
|
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
|
613 |
|
|
|
614 |
|
|
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
|
615 |
|
|
|
616 |
|
|
test: _always $(testOutputs) ;
|
617 |
|
|
|
618 |
|
|
all += $(wildcard %/test*.xml)
|
619 |
|
|
|
620 |
|
|
# Accepts a test output: make <test_output_path>-ok
|
621 |
|
|
%-ok: _always
|
622 |
|
|
mv $* $(call testRef,$*)
|
623 |
|
|
|
624 |
|
|
accept-all: _always
|
625 |
|
|
+yes|$(selfMake) test
|
626 |
|
|
|
627 |
|
|
##### Documentation
|
628 |
|
|
|
629 |
|
|
steps = $(selfMake) -s $*/import test=1 by_col=1 verbosity=2 n=100\
|
630 |
|
|
2>&1|$(bin)/debug2redmine >$@
|
631 |
|
|
|
632 |
|
|
%/logs/steps.by_col.log.sql: _always
|
633 |
|
|
+$(steps)
|