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