Project

General

Profile

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