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