Project

General

Profile

1 783 aaronmk
##### Configuration
2
3 858 aaronmk
nolog ?=
4 783 aaronmk
exts ?= csv xml
5
test_n ?= 2
6
tablesSort ?= plots organisms stems
7
mainCoreMapTry ?= organisms specimens
8
9
##### Vars/functions
10
11 640 aaronmk
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
12 250 aaronmk
13 368 aaronmk
# Make
14 640 aaronmk
SHELL := /bin/bash
15 415 aaronmk
subMake = $(MAKE) $(@F) --directory=$(@D)
16 404 aaronmk
+_ = $(+:_%=)
17 368 aaronmk
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
18
19 790 aaronmk
# System
20
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
21
22 640 aaronmk
# Terminal
23
esc := '['
24
reset := $(esc)'0m'
25
emph := $(esc)'7m '
26
endEmph := ' '$(reset)
27
28 368 aaronmk
# Commands
29 395 aaronmk
CP = cp -p
30
DIFF = diff --unified=2
31 368 aaronmk
32 783 aaronmk
# Paths
33 640 aaronmk
root := $(selfDir_uZPPqC)..
34 775 aaronmk
mappings := $(root)/mappings
35 368 aaronmk
psqlAsBien := $(root)/bin/psql_vegbien
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 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
mainCoreMap := $(firstword $(wildcard $(mainCoreMapTry:%=$(coreMap))))
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 415 aaronmk
	$(root)/bin/join_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
map = $(if $(<in),<$(<in) ,$(if $(mapEnv),env $(mapEnv) ))$(root)/map $(+maps)
102 772 aaronmk
map2db = env out_database=vegbien $(map)
103 368 aaronmk
104 783 aaronmk
##### Import to VegBIEN
105 772 aaronmk
106
ifneq ($(dbFile)$(inputFiles),)
107
108 718 aaronmk
import: _always import-all verify ;
109 368 aaronmk
110 790 aaronmk
log = $(@:-all=)$(if $(n),.n=$(n),).$(date).log
111 772 aaronmk
import = -(set -x; "time" env commit=1 $(map2db)) \
112 858 aaronmk
$(if nolog,,$(if $(n),,>>$(log))) 2>&1$(if nolog,,$(if $(n),|tee -a $(log)))
113 636 aaronmk
# ignore import errors, which are often benign (e.g. invalid date format)
114 624 aaronmk
115 772 aaronmk
ifneq ($(inputFiles),)
116
import-all: $(addprefix import-,$(tables)) _always ;
117
else
118 724 aaronmk
import-all: $(directMaps) _always
119 718 aaronmk
	$(import)
120 772 aaronmk
endif
121 718 aaronmk
122
import-%: maps/VegBIEN.%.csv _always
123
	$(import)
124 772 aaronmk
# default:
125
import-%: _always ;
126 718 aaronmk
127 772 aaronmk
else
128
import: _always ;
129
endif
130
131 790 aaronmk
##### Log files from import
132
133 339 aaronmk
logs := $(wildcard *.log)
134
135 383 aaronmk
rm_logs: _always
136 339 aaronmk
	$(RM) $(logs)
137
138 783 aaronmk
##### Verification of import
139 264 aaronmk
140 624 aaronmk
ifneq ($(wildcard verify.ref*),)
141
142 514 aaronmk
verify: verify.ref verify.out _always
143 404 aaronmk
	$(DIFF) $(+_)
144 368 aaronmk
145 514 aaronmk
all += verify.out
146
147 383 aaronmk
%.out: %.sql _always
148 429 aaronmk
	$(out_cmd)
149 370 aaronmk
.PRECIOUS: %.out
150 772 aaronmk
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off\
151 429 aaronmk
--pset=null=NULL <$< >$@
152 369 aaronmk
153 624 aaronmk
else
154
verify: _always ;
155
endif
156
157 783 aaronmk
##### Testing
158 368 aaronmk
159 624 aaronmk
ifneq ($(wildcard test/),)
160
161 723 aaronmk
hasOwnRef = $(filter-out %.2-step.xml,$@)
162 630 aaronmk
testRef = $(1:.2-step.xml=.xml).ref
163
164 629 aaronmk
define test
165 776 aaronmk
@echo "Testing $(abspath $@)..."
166 775 aaronmk
>$@ env test=1 verbose=1 n=$(test_n) $(1)
167 653 aaronmk
@(set -x; $(DIFF) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
168 780 aaronmk
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
169 777 aaronmk
$(if $(hasOwnRef),\
170
echo $(emph)"To accept new test output:"$(endEmph);\
171
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
172
,\
173
echo $(emph)"Note: The preceding failed test is compared to another test's\
174
output"$(endEmph);\
175
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
176
)\
177 652 aaronmk
exit $$e;}
178 628 aaronmk
endef
179
180 775 aaronmk
test2File = $(call test,$(map))
181
182 876 aaronmk
tests :=
183
184
test/$(via).%.xml: maps/$(via).%.csv _always
185 775 aaronmk
	$(test2File)
186 876 aaronmk
tests += test/$(via).%.xml
187 629 aaronmk
188 876 aaronmk
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
189 775 aaronmk
	$(test2File)
190 876 aaronmk
tests += test/VegBIEN.%.xml
191 628 aaronmk
192 876 aaronmk
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(mainCoreMap) _always
193 775 aaronmk
	-$(test2File)
194 722 aaronmk
# Don't abort tester if only 2-step test fails, as it's often finicky
195 876 aaronmk
tests += test/VegBIEN.%.2-step.xml
196 630 aaronmk
197 876 aaronmk
test/import.%.out: maps/VegBIEN.%.csv _always
198 718 aaronmk
	$(call test,$(map2db))
199 876 aaronmk
tests += test/import.%.out
200 627 aaronmk
201 724 aaronmk
.PRECIOUS: $(tests)
202
203 876 aaronmk
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
204 724 aaronmk
205 876 aaronmk
test: _always $(testOutputs) ;
206
207
all += $(testOutputs)
208
209 634 aaronmk
# Accepts a test output: make <test_output_path>-ok
210 626 aaronmk
%-ok: _always
211 630 aaronmk
	$(CP) $* $(call testRef,$*)
212 502 aaronmk
213 624 aaronmk
else
214
test: _always ;
215
endif
216
217 783 aaronmk
##### Input-type-specific
218 368 aaronmk
219 624 aaronmk
# Each input type needs var $(mapEnv) and targets install, uninstall
220
221 622 aaronmk
ifneq ($(dbFile),)
222 256 aaronmk
223 635 aaronmk
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
224
db := $(notdir $(realpath .))
225 622 aaronmk
226 621 aaronmk
%.ref: %.ref.sql
227
	$(inputDbAsBien) $(db) <$< >$@
228
.PRECIOUS: %.ref
229
230 783 aaronmk
#### Installation
231 256 aaronmk
232 621 aaronmk
install: _always db ;
233 339 aaronmk
234 621 aaronmk
uninstall: _always rm_db ;
235 368 aaronmk
236 783 aaronmk
#### DB-engine-specific
237 621 aaronmk
238 624 aaronmk
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
239 256 aaronmk
240 622 aaronmk
ifeq ($(dbEngineExt),my)
241 621 aaronmk
242 622 aaronmk
dbEngine := MySQL
243
244 621 aaronmk
bienPassword := $(shell cat $(root)/config/bien_password)
245
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
246
mysqlAsRoot := $(call mysqlAs,root)
247
dbAsBien := $(call mysqlAs,bien)
248
249 622 aaronmk
db: $(dbFile) _always
250 635 aaronmk
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
251
	-$(mysqlAsRoot) --database=$(db) <$<
252 368 aaronmk
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
253 255 aaronmk
# ignore errors in db import so that GRANT will still be run
254 250 aaronmk
255 383 aaronmk
rm_db: _always
256 368 aaronmk
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
257
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
258 365 aaronmk
# ignore errors if grant not defined
259 250 aaronmk
260 783 aaronmk
### Unrecognized DB engine
261 368 aaronmk
262
else
263 635 aaronmk
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
264 368 aaronmk
endif
265
266 622 aaronmk
# Must come after dbEngine is set
267
mapEnv := in_engine=$(dbEngine) in_database=$(db)
268
269 783 aaronmk
#### Unrecognized input type
270 624 aaronmk
271 256 aaronmk
else
272 624 aaronmk
install: _always ;
273
uninstall: _always ;
274 256 aaronmk
endif