Project

General

Profile

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