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