Project

General

Profile

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