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