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