1 |
783
|
aaronmk
|
##### Configuration
|
2 |
|
|
|
3 |
1190
|
aaronmk
|
log ?= $(if $(test),,1)
|
4 |
1257
|
aaronmk
|
profile ?=
|
5 |
783
|
aaronmk
|
exts ?= csv xml
|
6 |
|
|
test_n ?= 2
|
7 |
|
|
tablesSort ?= plots organisms stems
|
8 |
|
|
|
9 |
|
|
##### Vars/functions
|
10 |
|
|
|
11 |
640
|
aaronmk
|
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
|
12 |
250
|
aaronmk
|
|
13 |
368
|
aaronmk
|
# Make
|
14 |
640
|
aaronmk
|
SHELL := /bin/bash
|
15 |
415
|
aaronmk
|
subMake = $(MAKE) $(@F) --directory=$(@D)
|
16 |
404
|
aaronmk
|
+_ = $(+:_%=)
|
17 |
368
|
aaronmk
|
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
|
18 |
|
|
|
19 |
790
|
aaronmk
|
# System
|
20 |
|
|
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
|
21 |
|
|
|
22 |
640
|
aaronmk
|
# Terminal
|
23 |
1184
|
aaronmk
|
termCols := $(shell tput cols)
|
24 |
640
|
aaronmk
|
esc := '['
|
25 |
|
|
reset := $(esc)'0m'
|
26 |
|
|
emph := $(esc)'7m '
|
27 |
|
|
endEmph := ' '$(reset)
|
28 |
|
|
|
29 |
368
|
aaronmk
|
# Commands
|
30 |
1245
|
aaronmk
|
MKDIR = mkdir -p
|
31 |
|
|
mkdir = $(MKDIR) $(@D)
|
32 |
395
|
aaronmk
|
CP = cp -p
|
33 |
1184
|
aaronmk
|
diff = diff --unified=2
|
34 |
|
|
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
|
35 |
|
|
--width=$(termCols),$(diff))
|
36 |
368
|
aaronmk
|
|
37 |
783
|
aaronmk
|
# Paths
|
38 |
640
|
aaronmk
|
root := $(selfDir_uZPPqC)..
|
39 |
775
|
aaronmk
|
mappings := $(root)/mappings
|
40 |
1196
|
aaronmk
|
datasrc := $(notdir $(realpath .))
|
41 |
1081
|
aaronmk
|
psqlOpts := --set ON_ERROR_STOP=1 --quiet
|
42 |
|
|
psqlAsBien := $(root)/bin/psql_vegbien $(psqlOpts)
|
43 |
353
|
aaronmk
|
|
44 |
783
|
aaronmk
|
##### General targets
|
45 |
250
|
aaronmk
|
|
46 |
418
|
aaronmk
|
all: _always maps ;
|
47 |
247
|
aaronmk
|
|
48 |
1260
|
aaronmk
|
.SUFFIXES: # turn off built-in suffix rules
|
49 |
|
|
.SECONDARY: # don't automatically delete intermediate files
|
50 |
247
|
aaronmk
|
|
51 |
383
|
aaronmk
|
_always:
|
52 |
|
|
.PHONY: _always
|
53 |
250
|
aaronmk
|
|
54 |
383
|
aaronmk
|
clean: _always
|
55 |
256
|
aaronmk
|
$(RM) $(all)
|
56 |
|
|
|
57 |
423
|
aaronmk
|
remake: _always clean all ;
|
58 |
|
|
|
59 |
515
|
aaronmk
|
%.out: %.make _always
|
60 |
368
|
aaronmk
|
./$* >$@
|
61 |
|
|
|
62 |
415
|
aaronmk
|
$(root)/%: _always
|
63 |
|
|
+$(subMake)
|
64 |
|
|
|
65 |
783
|
aaronmk
|
##### SVN
|
66 |
250
|
aaronmk
|
|
67 |
766
|
aaronmk
|
svn_props: _always
|
68 |
1201
|
aaronmk
|
svn propset svn:ignore $$'*.log\n*.trace\nsrc*' .
|
69 |
951
|
aaronmk
|
$(if $(wildcard maps/),svn propset svn:ignore $$'.~*' maps)
|
70 |
1199
|
aaronmk
|
$(if $(wildcard verify/),svn propset svn:ignore $$'*.out' verify)
|
71 |
766
|
aaronmk
|
$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
|
72 |
|
|
|
73 |
783
|
aaronmk
|
##### Installation
|
74 |
766
|
aaronmk
|
|
75 |
418
|
aaronmk
|
reinstall: _always uninstall install ;
|
76 |
264
|
aaronmk
|
|
77 |
783
|
aaronmk
|
##### Maps
|
78 |
264
|
aaronmk
|
|
79 |
1139
|
aaronmk
|
fullViaMap := maps/%.full.csv
|
80 |
|
|
allViaMaps := $(filter-out maps/VegBIEN.%.csv $(fullViaMap),\
|
81 |
|
|
$(wildcard maps/*.csv))
|
82 |
728
|
aaronmk
|
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
|
83 |
783
|
aaronmk
|
|
84 |
775
|
aaronmk
|
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
|
85 |
1139
|
aaronmk
|
selfMap := $(mappings)/$(via).self.%.csv
|
86 |
783
|
aaronmk
|
|
87 |
749
|
aaronmk
|
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
|
88 |
1139
|
aaronmk
|
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard maps/$(via).*.csv))
|
89 |
783
|
aaronmk
|
|
90 |
728
|
aaronmk
|
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
|
91 |
|
|
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
|
92 |
|
|
$(wildcard maps/VegBIEN.*.csv))
|
93 |
772
|
aaronmk
|
tables := $(directMaps:maps/VegBIEN.%.csv=%)
|
94 |
728
|
aaronmk
|
|
95 |
1139
|
aaronmk
|
maps :=
|
96 |
728
|
aaronmk
|
|
97 |
775
|
aaronmk
|
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
|
98 |
1126
|
aaronmk
|
$(root)/bin/join_union_sort <$+ >$@
|
99 |
264
|
aaronmk
|
|
100 |
1139
|
aaronmk
|
maps += $(autogenMaps)
|
101 |
|
|
|
102 |
|
|
maps/$(via).%.full.csv: maps/$(via).%.csv $(selfMap)
|
103 |
|
|
env ignore=1 $(root)/bin/union <$+|$(root)/bin/sort_map >$@
|
104 |
|
|
maps/$(via).%.full.csv: maps/$(via).%.csv # fallback if no self map
|
105 |
1194
|
aaronmk
|
$(CP) $< $@
|
106 |
1139
|
aaronmk
|
|
107 |
|
|
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
|
108 |
|
|
|
109 |
|
|
maps: $(maps) _always ;
|
110 |
|
|
|
111 |
|
|
all += $(maps)
|
112 |
|
|
|
113 |
783
|
aaronmk
|
##### Mapping
|
114 |
368
|
aaronmk
|
|
115 |
728
|
aaronmk
|
dbFile := $(firstword $(wildcard src/db.*.sql))
|
116 |
772
|
aaronmk
|
inputFiles := $(wildcard src/*.csv src/*.xml)
|
117 |
728
|
aaronmk
|
|
118 |
775
|
aaronmk
|
+maps = $(filter maps/% $(mappings)/%,$(+_))
|
119 |
|
|
<in = $(firstword $(filter-out $(+maps),$(+_)) $(wildcard $(exts:%=src/*.$*.%)))
|
120 |
958
|
aaronmk
|
map = $(if $(<in),<$(<in) ,$(if $(mapEnv),env $(mapEnv) ,$(error\
|
121 |
1139
|
aaronmk
|
No input file src/*.$*.{$(exts)} )))$(root)/map $(+maps)
|
122 |
772
|
aaronmk
|
map2db = env out_database=vegbien $(map)
|
123 |
368
|
aaronmk
|
|
124 |
783
|
aaronmk
|
##### Import to VegBIEN
|
125 |
772
|
aaronmk
|
|
126 |
|
|
ifneq ($(dbFile)$(inputFiles),)
|
127 |
|
|
|
128 |
1190
|
aaronmk
|
log_ = $(@:-all=)$(if $(n),.n=$(n),).$(date).log
|
129 |
|
|
trace = $(log_:.log=.trace)
|
130 |
1188
|
aaronmk
|
import = -(set -x; "time" env commit=1 verbose=1\
|
131 |
1190
|
aaronmk
|
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
|
132 |
|
|
$(if $(n),,>>$(log_))) 2>&1$(if $(log),$(if $(n),|tee -a $(log_)))
|
133 |
1088
|
aaronmk
|
# don't abort on import errors, which often relate to invalid input data
|
134 |
624
|
aaronmk
|
|
135 |
1193
|
aaronmk
|
import: $(addprefix import-,$(tables)) _always ;
|
136 |
718
|
aaronmk
|
|
137 |
|
|
import-%: maps/VegBIEN.%.csv _always
|
138 |
|
|
$(import)
|
139 |
772
|
aaronmk
|
# default:
|
140 |
|
|
import-%: _always ;
|
141 |
718
|
aaronmk
|
|
142 |
772
|
aaronmk
|
else
|
143 |
|
|
import: _always ;
|
144 |
|
|
endif
|
145 |
|
|
|
146 |
790
|
aaronmk
|
##### Log files from import
|
147 |
|
|
|
148 |
1188
|
aaronmk
|
logs := $(wildcard *.log *.trace)
|
149 |
339
|
aaronmk
|
|
150 |
383
|
aaronmk
|
rm_logs: _always
|
151 |
339
|
aaronmk
|
$(RM) $(logs)
|
152 |
|
|
|
153 |
783
|
aaronmk
|
##### Verification of import
|
154 |
264
|
aaronmk
|
|
155 |
1194
|
aaronmk
|
verify: $(addprefix verify-,$(tables)) _always ;
|
156 |
|
|
|
157 |
1199
|
aaronmk
|
verify-%: verify/%.ref verify/%.out _always
|
158 |
1184
|
aaronmk
|
-$(diffVerbose) $(+_)
|
159 |
1088
|
aaronmk
|
# don't abort on verification errors, which are expected during development
|
160 |
1194
|
aaronmk
|
# default:
|
161 |
1199
|
aaronmk
|
verify-%: verify/%.out _always
|
162 |
1200
|
aaronmk
|
$(if $(shell test -e $< && echo t),cat $<)
|
163 |
1199
|
aaronmk
|
# don't run if verify/%.out's default do-nothing action was used
|
164 |
1200
|
aaronmk
|
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
|
165 |
368
|
aaronmk
|
|
166 |
1245
|
aaronmk
|
define verify
|
167 |
|
|
$(mkdir)
|
168 |
|
|
$(psqlAsBien) --set=datasource="'$(datasrc)'" --no-align\
|
169 |
|
|
--field-separator=$$'\t' --pset=footer=off --pset=null=NULL <$< >$@
|
170 |
|
|
endef
|
171 |
514
|
aaronmk
|
|
172 |
1199
|
aaronmk
|
verify/%.out: $(mappings)/verify.%.sql _always
|
173 |
1192
|
aaronmk
|
$(verify)
|
174 |
1194
|
aaronmk
|
# default:
|
175 |
1199
|
aaronmk
|
verify/%.out: _always ;
|
176 |
369
|
aaronmk
|
|
177 |
1199
|
aaronmk
|
all += $(wildcard verify/*.out)
|
178 |
1192
|
aaronmk
|
|
179 |
1082
|
aaronmk
|
ifneq ($(dbFile),)
|
180 |
|
|
%.ref: %.ref.sql
|
181 |
|
|
$(dbAsBien) $(db) <$< >$@
|
182 |
|
|
endif
|
183 |
|
|
|
184 |
783
|
aaronmk
|
##### Testing
|
185 |
368
|
aaronmk
|
|
186 |
624
|
aaronmk
|
ifneq ($(wildcard test/),)
|
187 |
|
|
|
188 |
723
|
aaronmk
|
hasOwnRef = $(filter-out %.2-step.xml,$@)
|
189 |
630
|
aaronmk
|
testRef = $(1:.2-step.xml=.xml).ref
|
190 |
|
|
|
191 |
897
|
aaronmk
|
define runTest
|
192 |
776
|
aaronmk
|
@echo "Testing $(abspath $@)..."
|
193 |
991
|
aaronmk
|
>$@ env test=1 n=$(test_n) $(1)
|
194 |
1184
|
aaronmk
|
@(set -x; $(diff) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
|
195 |
780
|
aaronmk
|
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
|
196 |
777
|
aaronmk
|
$(if $(hasOwnRef),\
|
197 |
|
|
echo $(emph)"To accept new test output:"$(endEmph);\
|
198 |
|
|
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
|
199 |
|
|
,\
|
200 |
|
|
echo $(emph)"Note: The preceding failed test is compared to another test's\
|
201 |
|
|
output"$(endEmph);\
|
202 |
|
|
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
|
203 |
|
|
)\
|
204 |
652
|
aaronmk
|
exit $$e;}
|
205 |
628
|
aaronmk
|
endef
|
206 |
|
|
|
207 |
897
|
aaronmk
|
test2File = $(call runTest,$(map))
|
208 |
775
|
aaronmk
|
|
209 |
876
|
aaronmk
|
tests :=
|
210 |
|
|
|
211 |
1139
|
aaronmk
|
test/$(via).%.xml: maps/$(via).%.full.csv _always
|
212 |
775
|
aaronmk
|
$(test2File)
|
213 |
876
|
aaronmk
|
tests += test/$(via).%.xml
|
214 |
629
|
aaronmk
|
|
215 |
876
|
aaronmk
|
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
|
216 |
775
|
aaronmk
|
$(test2File)
|
217 |
876
|
aaronmk
|
tests += test/VegBIEN.%.xml
|
218 |
628
|
aaronmk
|
|
219 |
884
|
aaronmk
|
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
|
220 |
775
|
aaronmk
|
-$(test2File)
|
221 |
722
|
aaronmk
|
# Don't abort tester if only 2-step test fails, as it's often finicky
|
222 |
876
|
aaronmk
|
tests += test/VegBIEN.%.2-step.xml
|
223 |
630
|
aaronmk
|
|
224 |
876
|
aaronmk
|
test/import.%.out: maps/VegBIEN.%.csv _always
|
225 |
897
|
aaronmk
|
$(call runTest,$(map2db))
|
226 |
876
|
aaronmk
|
tests += test/import.%.out
|
227 |
627
|
aaronmk
|
|
228 |
876
|
aaronmk
|
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
|
229 |
724
|
aaronmk
|
|
230 |
876
|
aaronmk
|
test: _always $(testOutputs) ;
|
231 |
|
|
|
232 |
|
|
all += $(testOutputs)
|
233 |
|
|
|
234 |
634
|
aaronmk
|
# Accepts a test output: make <test_output_path>-ok
|
235 |
626
|
aaronmk
|
%-ok: _always
|
236 |
630
|
aaronmk
|
$(CP) $* $(call testRef,$*)
|
237 |
502
|
aaronmk
|
|
238 |
624
|
aaronmk
|
else
|
239 |
|
|
test: _always ;
|
240 |
|
|
endif
|
241 |
|
|
|
242 |
783
|
aaronmk
|
##### Input-type-specific
|
243 |
368
|
aaronmk
|
|
244 |
624
|
aaronmk
|
# Each input type needs var $(mapEnv) and targets install, uninstall
|
245 |
|
|
|
246 |
622
|
aaronmk
|
ifneq ($(dbFile),)
|
247 |
256
|
aaronmk
|
|
248 |
635
|
aaronmk
|
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
|
249 |
1196
|
aaronmk
|
db := $(datasrc)
|
250 |
622
|
aaronmk
|
|
251 |
783
|
aaronmk
|
#### Installation
|
252 |
256
|
aaronmk
|
|
253 |
621
|
aaronmk
|
install: _always db ;
|
254 |
339
|
aaronmk
|
|
255 |
621
|
aaronmk
|
uninstall: _always rm_db ;
|
256 |
368
|
aaronmk
|
|
257 |
783
|
aaronmk
|
#### DB-engine-specific
|
258 |
621
|
aaronmk
|
|
259 |
624
|
aaronmk
|
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
|
260 |
256
|
aaronmk
|
|
261 |
622
|
aaronmk
|
ifeq ($(dbEngineExt),my)
|
262 |
621
|
aaronmk
|
|
263 |
622
|
aaronmk
|
dbEngine := MySQL
|
264 |
|
|
|
265 |
621
|
aaronmk
|
bienPassword := $(shell cat $(root)/config/bien_password)
|
266 |
|
|
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
|
267 |
|
|
mysqlAsRoot := $(call mysqlAs,root)
|
268 |
|
|
dbAsBien := $(call mysqlAs,bien)
|
269 |
|
|
|
270 |
1097
|
aaronmk
|
dbExists = $(shell echo "SHOW DATABASES LIKE '$(db)';"|$(mysqlAsRoot))
|
271 |
|
|
|
272 |
|
|
define createDb
|
273 |
1098
|
aaronmk
|
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
|
274 |
1097
|
aaronmk
|
-$(mysqlAsRoot) --database=$(db) <$<
|
275 |
|
|
endef
|
276 |
|
|
# ignore errors in db import so that GRANT will still be run
|
277 |
|
|
|
278 |
622
|
aaronmk
|
db: $(dbFile) _always
|
279 |
1097
|
aaronmk
|
$(if $(dbExists),,$(createDb))
|
280 |
368
|
aaronmk
|
echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
|
281 |
250
|
aaronmk
|
|
282 |
383
|
aaronmk
|
rm_db: _always
|
283 |
368
|
aaronmk
|
-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
|
284 |
|
|
echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
|
285 |
365
|
aaronmk
|
# ignore errors if grant not defined
|
286 |
250
|
aaronmk
|
|
287 |
783
|
aaronmk
|
### Unrecognized DB engine
|
288 |
368
|
aaronmk
|
|
289 |
|
|
else
|
290 |
993
|
aaronmk
|
$(error The DB filename $(dbFile) must be db.my.sql)
|
291 |
368
|
aaronmk
|
endif
|
292 |
|
|
|
293 |
622
|
aaronmk
|
# Must come after dbEngine is set
|
294 |
|
|
mapEnv := in_engine=$(dbEngine) in_database=$(db)
|
295 |
|
|
|
296 |
783
|
aaronmk
|
#### Unrecognized input type
|
297 |
624
|
aaronmk
|
|
298 |
256
|
aaronmk
|
else
|
299 |
624
|
aaronmk
|
install: _always ;
|
300 |
|
|
uninstall: _always ;
|
301 |
256
|
aaronmk
|
endif
|