Project

General

Profile

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