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