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