1 |
783
|
aaronmk
|
##### Configuration
|
2 |
|
|
|
3 |
1921
|
aaronmk
|
# Command line
|
4 |
1190
|
aaronmk
|
log ?= $(if $(test),,1)
|
5 |
1257
|
aaronmk
|
profile ?=
|
6 |
1983
|
aaronmk
|
quiet ?=
|
7 |
1620
|
aaronmk
|
reverify ?= 1
|
8 |
1990
|
aaronmk
|
use_staged ?= $(by_col)
|
9 |
1921
|
aaronmk
|
|
10 |
|
|
# Makefile
|
11 |
|
|
tablesSort ?= plots organisms stems specimens
|
12 |
1386
|
aaronmk
|
exts ?= csv tsv txt xml
|
13 |
783
|
aaronmk
|
test_n ?= 2
|
14 |
|
|
|
15 |
|
|
##### Vars/functions
|
16 |
|
|
|
17 |
640
|
aaronmk
|
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
|
18 |
250
|
aaronmk
|
|
19 |
1821
|
aaronmk
|
# Paths
|
20 |
|
|
datasrc := $(notdir $(realpath .))
|
21 |
|
|
root := $(selfDir_uZPPqC)..
|
22 |
|
|
bin := $(root)/bin
|
23 |
|
|
mappings := $(root)/mappings
|
24 |
|
|
|
25 |
368
|
aaronmk
|
# Make
|
26 |
640
|
aaronmk
|
SHELL := /bin/bash
|
27 |
1509
|
aaronmk
|
selfMake = $(MAKE) --makefile=../input.Makefile
|
28 |
1821
|
aaronmk
|
subMake = $(MAKE) $(@:$(root)/%=%) --directory=$(root)
|
29 |
404
|
aaronmk
|
+_ = $(+:_%=)
|
30 |
368
|
aaronmk
|
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
|
31 |
|
|
|
32 |
1742
|
aaronmk
|
# OS
|
33 |
|
|
os := $(shell uname)
|
34 |
|
|
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
|
35 |
|
|
|
36 |
|
|
# Formatting
|
37 |
|
|
SED = sed -$(if $(filter Darwin,$(os)),E,r)
|
38 |
|
|
|
39 |
790
|
aaronmk
|
# System
|
40 |
|
|
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
|
41 |
|
|
|
42 |
640
|
aaronmk
|
# Terminal
|
43 |
1184
|
aaronmk
|
termCols := $(shell tput cols)
|
44 |
640
|
aaronmk
|
esc := '['
|
45 |
|
|
reset := $(esc)'0m'
|
46 |
|
|
emph := $(esc)'7m '
|
47 |
|
|
endEmph := ' '$(reset)
|
48 |
|
|
|
49 |
368
|
aaronmk
|
# Commands
|
50 |
1245
|
aaronmk
|
MKDIR = mkdir -p
|
51 |
|
|
mkdir = $(MKDIR) $(@D)
|
52 |
395
|
aaronmk
|
CP = cp -p
|
53 |
1184
|
aaronmk
|
diff = diff --unified=2
|
54 |
|
|
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
|
55 |
|
|
--width=$(termCols),$(diff))
|
56 |
368
|
aaronmk
|
|
57 |
1918
|
aaronmk
|
# BIEN commands
|
58 |
1524
|
aaronmk
|
selfMap = $(bin)/cols 0 0
|
59 |
1081
|
aaronmk
|
psqlOpts := --set ON_ERROR_STOP=1 --quiet
|
60 |
1289
|
aaronmk
|
psqlAsBien := $(bin)/psql_vegbien $(psqlOpts)
|
61 |
353
|
aaronmk
|
|
62 |
1594
|
aaronmk
|
# SVN
|
63 |
|
|
addDir = $(if $(wildcard $(1)/),svn add --depth=empty $(1),svn mkdir $(1))
|
64 |
1771
|
aaronmk
|
setSvnIgnore = svn propset svn:ignore $(2) $(1)
|
65 |
1594
|
aaronmk
|
define addDirWithIgnore
|
66 |
|
|
$(addDir)
|
67 |
1771
|
aaronmk
|
$(setSvnIgnore)
|
68 |
1594
|
aaronmk
|
endef
|
69 |
|
|
|
70 |
783
|
aaronmk
|
##### General targets
|
71 |
250
|
aaronmk
|
|
72 |
418
|
aaronmk
|
all: _always maps ;
|
73 |
247
|
aaronmk
|
|
74 |
1260
|
aaronmk
|
.SUFFIXES: # turn off built-in suffix rules
|
75 |
|
|
.SECONDARY: # don't automatically delete intermediate files
|
76 |
1354
|
aaronmk
|
.DELETE_ON_ERROR: # delete target if recipe fails
|
77 |
247
|
aaronmk
|
|
78 |
383
|
aaronmk
|
_always:
|
79 |
|
|
.PHONY: _always
|
80 |
250
|
aaronmk
|
|
81 |
383
|
aaronmk
|
clean: _always
|
82 |
256
|
aaronmk
|
$(RM) $(all)
|
83 |
|
|
|
84 |
1742
|
aaronmk
|
remake: _always clean
|
85 |
|
|
+$(selfMake)
|
86 |
|
|
# re-run make so that cache of existing files is reset
|
87 |
|
|
|
88 |
1627
|
aaronmk
|
make_script = ./$< >$@
|
89 |
368
|
aaronmk
|
|
90 |
1604
|
aaronmk
|
%/: % _always ;
|
91 |
|
|
|
92 |
1627
|
aaronmk
|
# Must come before `%: %.make` to override it
|
93 |
|
|
src/%: src/%.make _always
|
94 |
3317
|
aaronmk
|
(set -x; $(make_script)) 2>>$<.log.sql
|
95 |
1627
|
aaronmk
|
.PRECIOUS: src/% # save partial outputs of aborted src make scripts
|
96 |
|
|
|
97 |
|
|
%: %.make _always
|
98 |
|
|
$(make_script)
|
99 |
|
|
|
100 |
783
|
aaronmk
|
##### SVN
|
101 |
250
|
aaronmk
|
|
102 |
1594
|
aaronmk
|
add: _always
|
103 |
1771
|
aaronmk
|
$(call setSvnIgnore,.,$$'')
|
104 |
1594
|
aaronmk
|
$(call addDirWithIgnore,src,$$'*')
|
105 |
|
|
$(call addDirWithIgnore,maps,$$'.~*')
|
106 |
|
|
$(call addDirWithIgnore,import,$$'*')
|
107 |
|
|
$(call addDirWithIgnore,test,$$'*.out\n*.xml')
|
108 |
|
|
$(call addDirWithIgnore,verify,$$'*.out')
|
109 |
766
|
aaronmk
|
|
110 |
1969
|
aaronmk
|
##### Existing maps discovery
|
111 |
1955
|
aaronmk
|
|
112 |
|
|
inputFiles := $(wildcard $(exts:%=src/*.%))
|
113 |
|
|
ifeq ($(inputFiles),)
|
114 |
|
|
dbExport := $(firstword $(wildcard src/db.*.sql))
|
115 |
|
|
endif
|
116 |
|
|
|
117 |
1289
|
aaronmk
|
srcMap := maps/src.%.csv
|
118 |
1139
|
aaronmk
|
fullViaMap := maps/%.full.csv
|
119 |
1289
|
aaronmk
|
directMap := maps/VegBIEN.%.csv
|
120 |
|
|
allViaMaps := $(filter-out $(srcMap) $(fullViaMap) $(directMap),\
|
121 |
1139
|
aaronmk
|
$(wildcard maps/*.csv))
|
122 |
728
|
aaronmk
|
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
|
123 |
783
|
aaronmk
|
|
124 |
775
|
aaronmk
|
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
|
125 |
1524
|
aaronmk
|
coreSelfMap := $(mappings)/$(via).self.%.csv
|
126 |
783
|
aaronmk
|
|
127 |
749
|
aaronmk
|
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
|
128 |
1139
|
aaronmk
|
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard maps/$(via).*.csv))
|
129 |
783
|
aaronmk
|
|
130 |
728
|
aaronmk
|
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
|
131 |
|
|
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
|
132 |
|
|
$(wildcard maps/VegBIEN.*.csv))
|
133 |
772
|
aaronmk
|
tables := $(directMaps:maps/VegBIEN.%.csv=%)
|
134 |
728
|
aaronmk
|
|
135 |
1969
|
aaronmk
|
##### Sources
|
136 |
|
|
|
137 |
|
|
srcs = $(shell $(bin)/sort_filenames $(wildcard $(exts:%=src/*.$*.%)))
|
138 |
|
|
isCsv = $(if $(srcs),$(if $(filter %.xml,$(srcs)),,1))
|
139 |
|
|
# true if $(srcs) non-empty and contains no *.xml
|
140 |
|
|
catSrcs = $(bin)/cat$(if $(isCsv),_csv) $(srcs)
|
141 |
|
|
withCatSrcs = $(catSrcs:$(bin)/%=$(bin)/with_%) --
|
142 |
|
|
|
143 |
1986
|
aaronmk
|
# Usage: `make {--silent|-s} inputs/<datasrc>/cat` (don't echo make commands)
|
144 |
1969
|
aaronmk
|
cat: $(addprefix cat-,$(tables)) _always ;
|
145 |
|
|
|
146 |
|
|
cat-%: _always
|
147 |
|
|
$(catSrcs)
|
148 |
|
|
|
149 |
1921
|
aaronmk
|
##### Installation
|
150 |
|
|
|
151 |
|
|
install: _always src/install import/install ;
|
152 |
|
|
|
153 |
|
|
uninstall: _always import/uninstall src/uninstall ;
|
154 |
|
|
|
155 |
|
|
reinstall: _always uninstall install ;
|
156 |
|
|
|
157 |
|
|
##### Staging tables
|
158 |
|
|
|
159 |
|
|
import/install: _always import/schema $(addprefix import/install-,$(tables)) ;
|
160 |
|
|
|
161 |
|
|
import/uninstall: _always import/rm_schema ;
|
162 |
|
|
# rm_schema will also drop all staging tables
|
163 |
|
|
|
164 |
|
|
import/schema: _always
|
165 |
|
|
-echo 'CREATE SCHEMA "$(datasrc)";'|$(psqlAsBien)
|
166 |
|
|
# ignore errors if schema exists
|
167 |
|
|
|
168 |
|
|
import/rm_schema: _always
|
169 |
|
|
echo 'DROP SCHEMA IF EXISTS "$(datasrc)" CASCADE;'|$(psqlAsBien)
|
170 |
|
|
|
171 |
|
|
import/install-%: _always
|
172 |
1955
|
aaronmk
|
$(if $(isCsv),$(import_install_))
|
173 |
3476
|
aaronmk
|
import_install_ = (prefix=; . $(bin)/vegbien_dest; "time" nice -n +5\
|
174 |
|
|
env schema=$(datasrc) table=$* $(bin)/csv2db $(catSrcs)\
|
175 |
3317
|
aaronmk
|
$(if $(log),$(if $(quiet),&>,2>&1|tee )import/install-$*.log.sql))
|
176 |
1921
|
aaronmk
|
|
177 |
1777
|
aaronmk
|
##### Maps building
|
178 |
|
|
|
179 |
1789
|
aaronmk
|
# Maps to (try to) build are added to one of these
|
180 |
1779
|
aaronmk
|
maps :=
|
181 |
1789
|
aaronmk
|
createOnlyMaps :=
|
182 |
1779
|
aaronmk
|
|
183 |
1775
|
aaronmk
|
srcMaps := $(wildcard maps/src.*.csv)
|
184 |
1289
|
aaronmk
|
|
185 |
3569
|
aaronmk
|
srcRoot = $(mappings)/roots/$*.sh
|
186 |
|
|
mkSrcMap = $(catSrcs)|(. $(srcRoot); env datasrc=$(datasrc) $(bin)/src_map >$@)
|
187 |
1779
|
aaronmk
|
|
188 |
3567
|
aaronmk
|
# Src maps cleanup
|
189 |
|
|
ifneq ($(filter maps/.%.last_cleanup,$(MAKECMDGOALS)),)
|
190 |
|
|
maps/.src.%.csv.last_cleanup: maps/src.%.csv
|
191 |
|
|
$(bin)/in_place $< $(bin)/cols '*'
|
192 |
|
|
touch $@
|
193 |
|
|
else
|
194 |
1779
|
aaronmk
|
# Autogen src maps with known table names
|
195 |
3567
|
aaronmk
|
maps/src.%.csv: _always
|
196 |
|
|
$(if $(wildcard $@),,$(if $(isCsv),$(mkSrcMap)))
|
197 |
|
|
+$(if $(isCsv),$(selfMake) $(@:maps/%=maps/.%.last_cleanup))
|
198 |
|
|
# only build if doesn't exist
|
199 |
1779
|
aaronmk
|
# only build if CSV srcs exist for that table name
|
200 |
3567
|
aaronmk
|
endif
|
201 |
|
|
|
202 |
1779
|
aaronmk
|
# Try all table names
|
203 |
1789
|
aaronmk
|
createOnlyMaps += $(tablesSort:%=maps/src.%.csv)
|
204 |
1779
|
aaronmk
|
|
205 |
1273
|
aaronmk
|
# Must come before $(root)/% to override it
|
206 |
1524
|
aaronmk
|
$(coreSelfMap): _always
|
207 |
1273
|
aaronmk
|
-+$(subMake)
|
208 |
1524
|
aaronmk
|
# ignore errors if $(coreSelfMap) does not exist
|
209 |
1273
|
aaronmk
|
|
210 |
3561
|
aaronmk
|
joinSrcMap = $(if $(hasSrc),$(bin)/in_place $@ $(bin)/intersect $(word 3,$+) 0)
|
211 |
|
|
hasSrc = $(shell test -s $(word 3,$+) && echo t)
|
212 |
1526
|
aaronmk
|
|
213 |
1273
|
aaronmk
|
makeFullCsv = $(if $(shell test -e $(word 2,$+) && echo t),\
|
214 |
3561
|
aaronmk
|
env ignore=1 $(bin)/union <$(wordlist 1,2,$+)|,<$< )$(bin)/sort_map >$@
|
215 |
1524
|
aaronmk
|
# can't use $(wildcard) because it won't recheck file after $(coreSelfMap) runs
|
216 |
1273
|
aaronmk
|
|
217 |
3572
|
aaronmk
|
# Must come before maps/$(via).%.csv to override it
|
218 |
3561
|
aaronmk
|
maps/$(via).%.full.csv: maps/$(via).%.csv $(coreSelfMap) $(srcMap)
|
219 |
1273
|
aaronmk
|
$(makeFullCsv)
|
220 |
1526
|
aaronmk
|
$(joinSrcMap)
|
221 |
1139
|
aaronmk
|
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
|
222 |
|
|
|
223 |
3572
|
aaronmk
|
# Via maps cleanup
|
224 |
|
|
ifneq ($(filter maps/.%.last_cleanup,$(MAKECMDGOALS)),)
|
225 |
|
|
maps/.$(via).%.csv.last_cleanup: maps/$(via).%.csv $(coreMap)
|
226 |
|
|
$(bin)/in_place $< env ignore=1 $(bin)/subtract $(word 2,$+) 0 1 2
|
227 |
|
|
touch $@
|
228 |
|
|
# Include comment column so commented mappings are never removed
|
229 |
|
|
else
|
230 |
|
|
maps/$(via).%.csv: _always
|
231 |
|
|
+$(selfMake) $(@:maps/%=maps/.%.last_cleanup)
|
232 |
|
|
endif
|
233 |
|
|
|
234 |
1530
|
aaronmk
|
maps/VegBIEN.%.csv: maps/$(via).%.full.csv $(coreMap)
|
235 |
|
|
$(bin)/join <$+|$(bin)/sort_map >$@
|
236 |
|
|
maps += $(autogenMaps)
|
237 |
|
|
|
238 |
1789
|
aaronmk
|
maps: $(createOnlyMaps) $(maps) _always ;
|
239 |
1139
|
aaronmk
|
|
240 |
|
|
all += $(maps)
|
241 |
|
|
|
242 |
1742
|
aaronmk
|
##### Maps validation
|
243 |
|
|
|
244 |
|
|
missing_mappings: _always missing_join_mappings missing_input_mappings ;
|
245 |
|
|
|
246 |
|
|
missing_%_mappings: _always # stem is one of join|input
|
247 |
|
|
@echo $(emph)"Missing $* mappings:"$(endEmph)
|
248 |
|
|
@+$(selfMake) remake 2>&1\
|
249 |
1800
|
aaronmk
|
|$(SED) -n 's/^.*No.* $* mapping for ([0-9A-Za-z_-]+).*$$/\1/p'\
|
250 |
1742
|
aaronmk
|
$(if $(filter join,$*),|$(bin)/ucase_first 0)|sort|uniq
|
251 |
|
|
|
252 |
1509
|
aaronmk
|
##### External dependencies
|
253 |
|
|
|
254 |
|
|
$(root)/%: _always
|
255 |
|
|
+$(subMake)
|
256 |
1628
|
aaronmk
|
.PRECIOUS: $(root)/% # let ext. dir's Makefile decide whether to delete on error
|
257 |
1509
|
aaronmk
|
|
258 |
783
|
aaronmk
|
##### Mapping
|
259 |
368
|
aaronmk
|
|
260 |
775
|
aaronmk
|
+maps = $(filter maps/% $(mappings)/%,$(+_))
|
261 |
1451
|
aaronmk
|
<in = $(firstword $(filter-out $(+maps),$(+_)))
|
262 |
|
|
map = $(if $(<in),<$(<in),\
|
263 |
1777
|
aaronmk
|
$(if $(srcs),$(withCatSrcs),\
|
264 |
1451
|
aaronmk
|
$(if $(mapEnv),env $(mapEnv),\
|
265 |
3387
|
aaronmk
|
$(error No input file src/*.$*.{$(exts)}))))\
|
266 |
1451
|
aaronmk
|
$(root)/map $(+maps)
|
267 |
3387
|
aaronmk
|
map2db = $(if $(use_staged),env in_database=vegbien in_schema=$(datasrc)\
|
268 |
|
|
in_table=$*) env out_database=vegbien $(map)
|
269 |
368
|
aaronmk
|
|
270 |
783
|
aaronmk
|
##### Import to VegBIEN
|
271 |
772
|
aaronmk
|
|
272 |
1378
|
aaronmk
|
ifneq ($(dbExport)$(inputFiles),)
|
273 |
772
|
aaronmk
|
|
274 |
1853
|
aaronmk
|
profileTest = $(if $(profile),$(if $(test),1))
|
275 |
|
|
profileOnly = -env profile_to=/dev/fd/3 $(map2db) 3>&1 1>&2|\
|
276 |
|
|
$(bin)/profile_stats /dev/fd/0
|
277 |
|
|
|
278 |
3317
|
aaronmk
|
log_ = import/$*$(if $(n),.n=$(n),).$(date).log.sql
|
279 |
|
|
trace = $(log_:.log.sql=.trace)
|
280 |
1853
|
aaronmk
|
import = -$(if $(profileTest),$(profileOnly),(set -x; "time" env commit=1\
|
281 |
1190
|
aaronmk
|
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
|
282 |
3382
|
aaronmk
|
$(if $(n),,&>>$(log_)))$(if $(log),$(if $(n), 2>&1|tee -a $(log_))))
|
283 |
1088
|
aaronmk
|
# don't abort on import errors, which often relate to invalid input data
|
284 |
624
|
aaronmk
|
|
285 |
1193
|
aaronmk
|
import: $(addprefix import-,$(tables)) _always ;
|
286 |
718
|
aaronmk
|
|
287 |
|
|
import-%: maps/VegBIEN.%.csv _always
|
288 |
|
|
$(import)
|
289 |
772
|
aaronmk
|
# default:
|
290 |
|
|
import-%: _always ;
|
291 |
718
|
aaronmk
|
|
292 |
772
|
aaronmk
|
else
|
293 |
|
|
import: _always ;
|
294 |
|
|
endif
|
295 |
|
|
|
296 |
790
|
aaronmk
|
##### Log files from import
|
297 |
|
|
|
298 |
3317
|
aaronmk
|
logs := $(wildcard import/*.log.sql import/*.trace)
|
299 |
339
|
aaronmk
|
|
300 |
383
|
aaronmk
|
rm_logs: _always
|
301 |
339
|
aaronmk
|
$(RM) $(logs)
|
302 |
|
|
|
303 |
783
|
aaronmk
|
##### Verification of import
|
304 |
264
|
aaronmk
|
|
305 |
1194
|
aaronmk
|
verify: $(addprefix verify-,$(tables)) _always ;
|
306 |
|
|
|
307 |
1199
|
aaronmk
|
verify-%: verify/%.ref verify/%.out _always
|
308 |
1184
|
aaronmk
|
-$(diffVerbose) $(+_)
|
309 |
1088
|
aaronmk
|
# don't abort on verification errors, which are expected during development
|
310 |
1194
|
aaronmk
|
# default:
|
311 |
1199
|
aaronmk
|
verify-%: verify/%.out _always
|
312 |
1200
|
aaronmk
|
$(if $(shell test -e $< && echo t),cat $<)
|
313 |
1199
|
aaronmk
|
# don't run if verify/%.out's default do-nothing action was used
|
314 |
1200
|
aaronmk
|
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
|
315 |
368
|
aaronmk
|
|
316 |
1620
|
aaronmk
|
verify = $(if $(reverify),"time" $(psqlAsBien) --set=datasource="'$(datasrc)'"\
|
317 |
|
|
--no-align --field-separator=$$'\t' --pset=footer=off --pset=null=NULL <$< >$@)
|
318 |
514
|
aaronmk
|
|
319 |
1199
|
aaronmk
|
verify/%.out: $(mappings)/verify.%.sql _always
|
320 |
1192
|
aaronmk
|
$(verify)
|
321 |
1194
|
aaronmk
|
# default:
|
322 |
1199
|
aaronmk
|
verify/%.out: _always ;
|
323 |
369
|
aaronmk
|
|
324 |
1199
|
aaronmk
|
all += $(wildcard verify/*.out)
|
325 |
1192
|
aaronmk
|
|
326 |
1378
|
aaronmk
|
ifneq ($(dbExport),)
|
327 |
1082
|
aaronmk
|
%.ref: %.ref.sql
|
328 |
|
|
$(dbAsBien) $(db) <$< >$@
|
329 |
|
|
endif
|
330 |
|
|
|
331 |
1667
|
aaronmk
|
##### Editing import
|
332 |
|
|
|
333 |
|
|
import/rotate: _always
|
334 |
1668
|
aaronmk
|
echo "UPDATE party SET organizationname = organizationname||'.$(date)'\
|
335 |
1854
|
aaronmk
|
WHERE organizationname = '$(datasrc)';"|$(psqlAsBien)
|
336 |
1667
|
aaronmk
|
|
337 |
|
|
import/rm: _always
|
338 |
1854
|
aaronmk
|
echo "DELETE FROM party WHERE organizationname = '$(datasrc)';"|\
|
339 |
|
|
$(psqlAsBien)
|
340 |
1667
|
aaronmk
|
|
341 |
783
|
aaronmk
|
##### Testing
|
342 |
368
|
aaronmk
|
|
343 |
1986
|
aaronmk
|
testRefOutput = $(subst .2-step,,$(subst .staging,,$(1)))
|
344 |
|
|
testRef = $(testRefOutput).ref
|
345 |
|
|
hasOwnRef = $(filter $@,$(call testRefOutput,$@))
|
346 |
|
|
# filter returns non-empty if they are equal
|
347 |
630
|
aaronmk
|
|
348 |
897
|
aaronmk
|
define runTest
|
349 |
776
|
aaronmk
|
@echo "Testing $(abspath $@)..."
|
350 |
991
|
aaronmk
|
>$@ env test=1 n=$(test_n) $(1)
|
351 |
1184
|
aaronmk
|
@(set -x; $(diff) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
|
352 |
780
|
aaronmk
|
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
|
353 |
777
|
aaronmk
|
$(if $(hasOwnRef),\
|
354 |
3179
|
aaronmk
|
{\
|
355 |
|
|
read -p $(emph)'Accept new test output? (y/n)'$(endEmph) REPLY;\
|
356 |
|
|
if test "$$REPLY" = y; then\
|
357 |
|
|
(set -x; $(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile);\
|
358 |
|
|
exit 0;\
|
359 |
|
|
fi;\
|
360 |
|
|
};,\
|
361 |
777
|
aaronmk
|
echo $(emph)"Note: The preceding failed test is compared to another test's\
|
362 |
|
|
output"$(endEmph);\
|
363 |
|
|
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
|
364 |
|
|
)\
|
365 |
652
|
aaronmk
|
exit $$e;}
|
366 |
628
|
aaronmk
|
endef
|
367 |
|
|
|
368 |
897
|
aaronmk
|
test2File = $(call runTest,$(map))
|
369 |
1986
|
aaronmk
|
test2Db = $(call runTest,$(map2db))
|
370 |
775
|
aaronmk
|
|
371 |
876
|
aaronmk
|
tests :=
|
372 |
|
|
|
373 |
1139
|
aaronmk
|
test/$(via).%.xml: maps/$(via).%.full.csv _always
|
374 |
775
|
aaronmk
|
$(test2File)
|
375 |
876
|
aaronmk
|
tests += test/$(via).%.xml
|
376 |
629
|
aaronmk
|
|
377 |
876
|
aaronmk
|
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
|
378 |
775
|
aaronmk
|
$(test2File)
|
379 |
876
|
aaronmk
|
tests += test/VegBIEN.%.xml
|
380 |
628
|
aaronmk
|
|
381 |
884
|
aaronmk
|
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
|
382 |
775
|
aaronmk
|
-$(test2File)
|
383 |
722
|
aaronmk
|
# Don't abort tester if only 2-step test fails, as it's often finicky
|
384 |
876
|
aaronmk
|
tests += test/VegBIEN.%.2-step.xml
|
385 |
630
|
aaronmk
|
|
386 |
876
|
aaronmk
|
test/import.%.out: maps/VegBIEN.%.csv _always
|
387 |
1986
|
aaronmk
|
$(test2Db)
|
388 |
876
|
aaronmk
|
tests += test/import.%.out
|
389 |
627
|
aaronmk
|
|
390 |
1987
|
aaronmk
|
testStaged2Db = $(foreach use_staged,1,$(test2Db))
|
391 |
|
|
# run with use_staged=1
|
392 |
1986
|
aaronmk
|
|
393 |
|
|
# Requires staging tables. To create them, run `make inputs/<datasrc>/install`.
|
394 |
|
|
test/import.%.staging.out: maps/VegBIEN.%.csv _always
|
395 |
1988
|
aaronmk
|
-$(if $(isCsv),$(testStaged2Db),cp -p $(@:.staging.out=.out) $@)
|
396 |
|
|
# Don't abort tester if only staging test fails, in case staging table missing
|
397 |
|
|
# Non-flat-file inputs fall back to mimicking a successful test
|
398 |
1986
|
aaronmk
|
tests += test/import.%.staging.out
|
399 |
|
|
|
400 |
876
|
aaronmk
|
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
|
401 |
724
|
aaronmk
|
|
402 |
1361
|
aaronmk
|
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
|
403 |
|
|
|
404 |
876
|
aaronmk
|
test: _always $(testOutputs) ;
|
405 |
|
|
|
406 |
1987
|
aaronmk
|
all += $(filter-out %.ref,$(wildcard test/*))
|
407 |
876
|
aaronmk
|
|
408 |
634
|
aaronmk
|
# Accepts a test output: make <test_output_path>-ok
|
409 |
626
|
aaronmk
|
%-ok: _always
|
410 |
630
|
aaronmk
|
$(CP) $* $(call testRef,$*)
|
411 |
502
|
aaronmk
|
|
412 |
3133
|
aaronmk
|
##### Documentation
|
413 |
|
|
|
414 |
3385
|
aaronmk
|
steps = $(selfMake) import test=1 by_col=1 verbosity=2 n=100\
|
415 |
|
|
2>&1|$(bin)/debug2redmine >$@
|
416 |
3188
|
aaronmk
|
|
417 |
3133
|
aaronmk
|
import/steps.by_col.sql: _always
|
418 |
3188
|
aaronmk
|
+$(steps)
|
419 |
3133
|
aaronmk
|
|
420 |
783
|
aaronmk
|
##### Input-type-specific
|
421 |
368
|
aaronmk
|
|
422 |
1917
|
aaronmk
|
# Each input type needs var $(mapEnv) and targets src/install, src/uninstall
|
423 |
624
|
aaronmk
|
|
424 |
1379
|
aaronmk
|
#### DB export
|
425 |
|
|
|
426 |
1378
|
aaronmk
|
ifneq ($(dbExport),)
|
427 |
256
|
aaronmk
|
|
428 |
1378
|
aaronmk
|
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbExport)))))
|
429 |
1196
|
aaronmk
|
db := $(datasrc)
|
430 |
622
|
aaronmk
|
|
431 |
1379
|
aaronmk
|
### Installation
|
432 |
256
|
aaronmk
|
|
433 |
1917
|
aaronmk
|
src/install: _always db ;
|
434 |
339
|
aaronmk
|
|
435 |
1917
|
aaronmk
|
src/uninstall: _always rm_db ;
|
436 |
368
|
aaronmk
|
|
437 |
1379
|
aaronmk
|
### DB-engine-specific
|
438 |
621
|
aaronmk
|
|
439 |
624
|
aaronmk
|
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
|
440 |
256
|
aaronmk
|
|
441 |
622
|
aaronmk
|
ifeq ($(dbEngineExt),my)
|
442 |
621
|
aaronmk
|
|
443 |
622
|
aaronmk
|
dbEngine := MySQL
|
444 |
|
|
|
445 |
621
|
aaronmk
|
bienPassword := $(shell cat $(root)/config/bien_password)
|
446 |
|
|
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
|
447 |
|
|
mysqlAsRoot := $(call mysqlAs,root)
|
448 |
|
|
dbAsBien := $(call mysqlAs,bien)
|
449 |
|
|
|
450 |
1097
|
aaronmk
|
dbExists = $(shell echo "SHOW DATABASES LIKE '$(db)';"|$(mysqlAsRoot))
|
451 |
|
|
|
452 |
|
|
define createDb
|
453 |
1098
|
aaronmk
|
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
|
454 |
1097
|
aaronmk
|
-$(mysqlAsRoot) --database=$(db) <$<
|
455 |
|
|
endef
|
456 |
|
|
# ignore errors in db import so that GRANT will still be run
|
457 |
|
|
|
458 |
1378
|
aaronmk
|
db: $(dbExport) _always
|
459 |
1097
|
aaronmk
|
$(if $(dbExists),,$(createDb))
|
460 |
368
|
aaronmk
|
echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
|
461 |
250
|
aaronmk
|
|
462 |
383
|
aaronmk
|
rm_db: _always
|
463 |
368
|
aaronmk
|
-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
|
464 |
|
|
echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
|
465 |
365
|
aaronmk
|
# ignore errors if grant not defined
|
466 |
250
|
aaronmk
|
|
467 |
1379
|
aaronmk
|
## Unrecognized DB engine
|
468 |
368
|
aaronmk
|
|
469 |
|
|
else
|
470 |
1378
|
aaronmk
|
$(error The DB filename $(dbExport) must be db.my.sql)
|
471 |
368
|
aaronmk
|
endif
|
472 |
|
|
|
473 |
1379
|
aaronmk
|
### Other input types
|
474 |
622
|
aaronmk
|
|
475 |
1379
|
aaronmk
|
else
|
476 |
624
|
aaronmk
|
|
477 |
1917
|
aaronmk
|
src/install: _always ;
|
478 |
|
|
src/uninstall: _always ;
|
479 |
1379
|
aaronmk
|
|
480 |
256
|
aaronmk
|
endif
|
481 |
1379
|
aaronmk
|
|
482 |
|
|
#### DB connection info
|
483 |
|
|
|
484 |
|
|
ifneq ($(dbEngine),)
|
485 |
|
|
# Must come after dbEngine is set
|
486 |
|
|
mapEnv := in_engine=$(dbEngine) in_database=$(db)
|
487 |
|
|
endif
|