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