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