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 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 951 aaronmk
	$(if $(wildcard maps/),svn propset svn:ignore $$'.~*' maps)
62 766 aaronmk
	$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
63
64 783 aaronmk
##### Installation
65 766 aaronmk
66 418 aaronmk
reinstall: _always uninstall install ;
67 264 aaronmk
68 783 aaronmk
##### Maps
69 264 aaronmk
70 728 aaronmk
allViaMaps := $(filter-out maps/VegBIEN.%.csv,$(wildcard maps/*.csv))
71
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
72 783 aaronmk
73 775 aaronmk
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
74 783 aaronmk
75 749 aaronmk
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
76
viaMaps += $(filter-out $(viaMaps),$(wildcard maps/$(via).*.csv))
77 783 aaronmk
78 728 aaronmk
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
79
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
80
$(wildcard maps/VegBIEN.*.csv))
81 772 aaronmk
tables := $(directMaps:maps/VegBIEN.%.csv=%)
82 728 aaronmk
83 724 aaronmk
maps: $(directMaps) _always ;
84 368 aaronmk
85 728 aaronmk
.PRECIOUS: $(allViaMaps) $(directMaps)
86 368 aaronmk
87 728 aaronmk
all += $(autogenMaps)
88
89 775 aaronmk
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
90 415 aaronmk
	$(root)/bin/join_sort <$+ >$@
91 775 aaronmk
.PRECIOUS: maps/VegBIEN.%.csv $(coreMap)
92 264 aaronmk
93 783 aaronmk
##### Mapping
94 368 aaronmk
95 728 aaronmk
dbFile := $(firstword $(wildcard src/db.*.sql))
96 772 aaronmk
inputFiles := $(wildcard src/*.csv src/*.xml)
97 728 aaronmk
98 775 aaronmk
+maps = $(filter maps/% $(mappings)/%,$(+_))
99
<in = $(firstword $(filter-out $(+maps),$(+_)) $(wildcard $(exts:%=src/*.$*.%)))
100
map = $(if $(<in),<$(<in) ,$(if $(mapEnv),env $(mapEnv) ))$(root)/map $(+maps)
101 772 aaronmk
map2db = env out_database=vegbien $(map)
102 368 aaronmk
103 783 aaronmk
##### Import to VegBIEN
104 772 aaronmk
105
ifneq ($(dbFile)$(inputFiles),)
106
107 718 aaronmk
import: _always import-all verify ;
108 368 aaronmk
109 790 aaronmk
log = $(@:-all=)$(if $(n),.n=$(n),).$(date).log
110 893 aaronmk
import = -(set -x; "time" env commit=1 $(map2db)) $(if $(nolog),,\
111
$(if $(n),,>>$(log))) 2>&1$(if $(nolog),,$(if $(n),|tee -a $(log)))
112 636 aaronmk
# ignore import errors, which are often benign (e.g. invalid date format)
113 624 aaronmk
114 772 aaronmk
ifneq ($(inputFiles),)
115
import-all: $(addprefix import-,$(tables)) _always ;
116
else
117 724 aaronmk
import-all: $(directMaps) _always
118 718 aaronmk
	$(import)
119 772 aaronmk
endif
120 718 aaronmk
121
import-%: maps/VegBIEN.%.csv _always
122
	$(import)
123 772 aaronmk
# default:
124
import-%: _always ;
125 718 aaronmk
126 772 aaronmk
else
127
import: _always ;
128
endif
129
130 790 aaronmk
##### Log files from import
131
132 339 aaronmk
logs := $(wildcard *.log)
133
134 383 aaronmk
rm_logs: _always
135 339 aaronmk
	$(RM) $(logs)
136
137 783 aaronmk
##### Verification of import
138 264 aaronmk
139 624 aaronmk
ifneq ($(wildcard verify.ref*),)
140
141 514 aaronmk
verify: verify.ref verify.out _always
142 404 aaronmk
	$(DIFF) $(+_)
143 368 aaronmk
144 514 aaronmk
all += verify.out
145
146 383 aaronmk
%.out: %.sql _always
147 429 aaronmk
	$(out_cmd)
148 370 aaronmk
.PRECIOUS: %.out
149 772 aaronmk
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off\
150 429 aaronmk
--pset=null=NULL <$< >$@
151 369 aaronmk
152 624 aaronmk
else
153
verify: _always ;
154
endif
155
156 783 aaronmk
##### Testing
157 368 aaronmk
158 624 aaronmk
ifneq ($(wildcard test/),)
159
160 723 aaronmk
hasOwnRef = $(filter-out %.2-step.xml,$@)
161 630 aaronmk
testRef = $(1:.2-step.xml=.xml).ref
162
163 897 aaronmk
define runTest
164 776 aaronmk
@echo "Testing $(abspath $@)..."
165 775 aaronmk
>$@ env test=1 verbose=1 n=$(test_n) $(1)
166 653 aaronmk
@(set -x; $(DIFF) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
167 780 aaronmk
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
168 777 aaronmk
$(if $(hasOwnRef),\
169
echo $(emph)"To accept new test output:"$(endEmph);\
170
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
171
,\
172
echo $(emph)"Note: The preceding failed test is compared to another test's\
173
output"$(endEmph);\
174
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
175
)\
176 652 aaronmk
exit $$e;}
177 628 aaronmk
endef
178
179 897 aaronmk
test2File = $(call runTest,$(map))
180 775 aaronmk
181 876 aaronmk
tests :=
182
183
test/$(via).%.xml: maps/$(via).%.csv _always
184 775 aaronmk
	$(test2File)
185 876 aaronmk
tests += test/$(via).%.xml
186 629 aaronmk
187 876 aaronmk
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
188 775 aaronmk
	$(test2File)
189 876 aaronmk
tests += test/VegBIEN.%.xml
190 628 aaronmk
191 884 aaronmk
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
192 775 aaronmk
	-$(test2File)
193 722 aaronmk
# Don't abort tester if only 2-step test fails, as it's often finicky
194 876 aaronmk
tests += test/VegBIEN.%.2-step.xml
195 630 aaronmk
196 876 aaronmk
test/import.%.out: maps/VegBIEN.%.csv _always
197 897 aaronmk
	$(call runTest,$(map2db))
198 876 aaronmk
tests += test/import.%.out
199 627 aaronmk
200 724 aaronmk
.PRECIOUS: $(tests)
201
202 876 aaronmk
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
203 724 aaronmk
204 876 aaronmk
test: _always $(testOutputs) ;
205
206
all += $(testOutputs)
207
208 634 aaronmk
# Accepts a test output: make <test_output_path>-ok
209 626 aaronmk
%-ok: _always
210 630 aaronmk
	$(CP) $* $(call testRef,$*)
211 502 aaronmk
212 624 aaronmk
else
213
test: _always ;
214
endif
215
216 783 aaronmk
##### Input-type-specific
217 368 aaronmk
218 624 aaronmk
# Each input type needs var $(mapEnv) and targets install, uninstall
219
220 622 aaronmk
ifneq ($(dbFile),)
221 256 aaronmk
222 635 aaronmk
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
223
db := $(notdir $(realpath .))
224 622 aaronmk
225 621 aaronmk
%.ref: %.ref.sql
226
	$(inputDbAsBien) $(db) <$< >$@
227
.PRECIOUS: %.ref
228
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 622 aaronmk
db: $(dbFile) _always
249 635 aaronmk
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
250
	-$(mysqlAsRoot) --database=$(db) <$<
251 368 aaronmk
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
252 255 aaronmk
# ignore errors in db import so that GRANT will still be run
253 250 aaronmk
254 383 aaronmk
rm_db: _always
255 368 aaronmk
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
256
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
257 365 aaronmk
# ignore errors if grant not defined
258 250 aaronmk
259 783 aaronmk
### Unrecognized DB engine
260 368 aaronmk
261
else
262 635 aaronmk
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
263 368 aaronmk
endif
264
265 622 aaronmk
# Must come after dbEngine is set
266
mapEnv := in_engine=$(dbEngine) in_database=$(db)
267
268 783 aaronmk
#### Unrecognized input type
269 624 aaronmk
270 256 aaronmk
else
271 624 aaronmk
install: _always ;
272
uninstall: _always ;
273 256 aaronmk
endif