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