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