Project

General

Profile

1 783 aaronmk
##### Configuration
2
3 1190 aaronmk
log ?= $(if $(test),,1)
4 1257 aaronmk
profile ?=
5 1620 aaronmk
reverify ?= 1
6 1386 aaronmk
exts ?= csv tsv txt xml
7 783 aaronmk
test_n ?= 2
8
tablesSort ?= plots organisms stems
9
10
##### Vars/functions
11
12 640 aaronmk
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
13 250 aaronmk
14 368 aaronmk
# Make
15 640 aaronmk
SHELL := /bin/bash
16 1509 aaronmk
selfMake = $(MAKE) --makefile=../input.Makefile
17 415 aaronmk
subMake = $(MAKE) $(@F) --directory=$(@D)
18 404 aaronmk
+_ = $(+:_%=)
19 368 aaronmk
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
20
21 790 aaronmk
# System
22
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
23
24 640 aaronmk
# Terminal
25 1184 aaronmk
termCols := $(shell tput cols)
26 640 aaronmk
esc := '['
27
reset := $(esc)'0m'
28
emph := $(esc)'7m '
29
endEmph := ' '$(reset)
30
31 368 aaronmk
# Commands
32 1245 aaronmk
MKDIR = mkdir -p
33
mkdir = $(MKDIR) $(@D)
34 395 aaronmk
CP = cp -p
35 1184 aaronmk
diff = diff --unified=2
36
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
37
--width=$(termCols),$(diff))
38 368 aaronmk
39 783 aaronmk
# Paths
40 1289 aaronmk
datasrc := $(notdir $(realpath .))
41 640 aaronmk
root := $(selfDir_uZPPqC)..
42 1289 aaronmk
bin := $(root)/bin
43 775 aaronmk
mappings := $(root)/mappings
44 1289 aaronmk
45
# Commands
46 1524 aaronmk
selfMap = $(bin)/cols 0 0
47 1081 aaronmk
psqlOpts := --set ON_ERROR_STOP=1 --quiet
48 1289 aaronmk
psqlAsBien := $(bin)/psql_vegbien $(psqlOpts)
49 353 aaronmk
50 1594 aaronmk
# SVN
51
addDir = $(if $(wildcard $(1)/),svn add --depth=empty $(1),svn mkdir $(1))
52
setSvnIgnore := svn propset svn:ignore
53
define addDirWithIgnore
54
$(addDir)
55
$(setSvnIgnore) $(2) $(1)
56
endef
57
58 783 aaronmk
##### General targets
59 250 aaronmk
60 418 aaronmk
all: _always maps ;
61 247 aaronmk
62 1260 aaronmk
.SUFFIXES: # turn off built-in suffix rules
63
.SECONDARY: # don't automatically delete intermediate files
64 1354 aaronmk
.DELETE_ON_ERROR: # delete target if recipe fails
65 247 aaronmk
66 383 aaronmk
_always:
67
.PHONY: _always
68 250 aaronmk
69 383 aaronmk
clean: _always
70 256 aaronmk
	$(RM) $(all)
71
72 1627 aaronmk
make_script = ./$< >$@
73 368 aaronmk
74 1604 aaronmk
%/: % _always ;
75
76 1627 aaronmk
# Must come before `%: %.make` to override it
77
src/%: src/%.make _always
78 1663 aaronmk
	(set -x; $(make_script)) 2>>$<.log
79 1627 aaronmk
.PRECIOUS: src/% # save partial outputs of aborted src make scripts
80
81
%: %.make _always
82
	$(make_script)
83
84 783 aaronmk
##### SVN
85 250 aaronmk
86 1594 aaronmk
add: _always
87
	$(call addDirWithIgnore,.,$$'')
88
	$(call addDirWithIgnore,src,$$'*')
89
	$(call addDirWithIgnore,maps,$$'.~*')
90
	$(call addDirWithIgnore,import,$$'*')
91
	$(call addDirWithIgnore,test,$$'*.out\n*.xml')
92
	$(call addDirWithIgnore,verify,$$'*.out')
93 766 aaronmk
94 783 aaronmk
##### Installation
95 766 aaronmk
96 418 aaronmk
reinstall: _always uninstall install ;
97 264 aaronmk
98 783 aaronmk
##### Maps
99 264 aaronmk
100 1289 aaronmk
srcMap := maps/src.%.csv
101 1139 aaronmk
fullViaMap := maps/%.full.csv
102 1289 aaronmk
directMap := maps/VegBIEN.%.csv
103
allViaMaps := $(filter-out $(srcMap) $(fullViaMap) $(directMap),\
104 1139 aaronmk
$(wildcard maps/*.csv))
105 728 aaronmk
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
106 783 aaronmk
107 775 aaronmk
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
108 1524 aaronmk
coreSelfMap := $(mappings)/$(via).self.%.csv
109 1289 aaronmk
noEmptyMap := $(mappings)/$(via)-VegBIEN.%.no_empty.csv
110 783 aaronmk
111 749 aaronmk
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
112 1139 aaronmk
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard maps/$(via).*.csv))
113 783 aaronmk
114 728 aaronmk
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
115
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
116
$(wildcard maps/VegBIEN.*.csv))
117 772 aaronmk
tables := $(directMaps:maps/VegBIEN.%.csv=%)
118 728 aaronmk
119 1289 aaronmk
srcMaps := $(filter-out maps/src.join.%.csv,$(wildcard maps/src.*.csv))
120
121 1273 aaronmk
# Must come before $(root)/% to override it
122 1524 aaronmk
$(coreSelfMap): _always
123 1273 aaronmk
	-+$(subMake)
124 1524 aaronmk
# ignore errors if $(coreSelfMap) does not exist
125 1273 aaronmk
126 1509 aaronmk
# Via maps cleanup
127
ifneq ($(filter maps/.%.last_cleanup,$(MAKECMDGOALS)),)
128 1524 aaronmk
maps/.$(via).%.csv.last_cleanup: maps/$(via).%.csv $(coreSelfMap)
129 1509 aaronmk
	$(bin)/in_place $< $(bin)/subtract $(word 2,$+) 0 1
130
	touch $@
131
# default:
132
maps/.$(via).%.csv.last_cleanup: ;
133
else
134
$(viaMaps): _always
135
	$(selfMake) $(@:maps/%=maps/.%.last_cleanup)
136
endif
137 1273 aaronmk
138 1139 aaronmk
maps :=
139 728 aaronmk
140 1526 aaronmk
joinSrcMap = $(if $(wildcard maps/src.$*.csv),$(bin)/in_place $@\
141
$(bin)/intersect maps/src.$*.csv 0)
142
143 1273 aaronmk
makeFullCsv = $(if $(shell test -e $(word 2,$+) && echo t),\
144 1289 aaronmk
env ignore=1 $(bin)/union <$+|$(bin)/sort_map >$@,$(CP) $< $@)
145 1524 aaronmk
# can't use $(wildcard) because it won't recheck file after $(coreSelfMap) runs
146 1273 aaronmk
147 1524 aaronmk
maps/$(via).%.full.csv: maps/$(via).%.csv $(coreSelfMap)
148 1273 aaronmk
	$(makeFullCsv)
149 1526 aaronmk
	$(joinSrcMap)
150 1139 aaronmk
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
151
152 1530 aaronmk
maps/VegBIEN.%.csv: maps/$(via).%.full.csv $(coreMap)
153
	$(bin)/join <$+|$(bin)/sort_map >$@
154
maps += $(autogenMaps)
155
156 1139 aaronmk
maps: $(maps) _always ;
157
158
all += $(maps)
159
160 1509 aaronmk
##### External dependencies
161
162
$(root)/%: _always
163
	+$(subMake)
164 1628 aaronmk
.PRECIOUS: $(root)/% # let ext. dir's Makefile decide whether to delete on error
165 1509 aaronmk
166 783 aaronmk
##### Mapping
167 368 aaronmk
168 1378 aaronmk
dbExport := $(firstword $(wildcard src/db.*.sql))
169 1385 aaronmk
inputFiles := $(wildcard $(exts:%=src/*.%))
170 728 aaronmk
171 775 aaronmk
+maps = $(filter maps/% $(mappings)/%,$(+_))
172 1451 aaronmk
<in = $(firstword $(filter-out $(+maps),$(+_)))
173 1659 aaronmk
srcs = $(shell $(bin)/sort_filenames $(wildcard $(exts:%=src/*.$*.%)))
174 1703 aaronmk
isXml = $(filter %.xml,$(firstword $(srcs)))
175 1451 aaronmk
map = $(if $(<in),<$(<in),\
176 1703 aaronmk
$(if $(srcs),$(bin)/with_cat$(if $(isXml),,_csv) $(srcs) --,\
177 1451 aaronmk
$(if $(mapEnv),env $(mapEnv),\
178
$(error No input file src/*.$*.{$(exts)}))))\
179
$(root)/map $(+maps)
180 772 aaronmk
map2db = env out_database=vegbien $(map)
181 368 aaronmk
182 783 aaronmk
##### Import to VegBIEN
183 772 aaronmk
184 1378 aaronmk
ifneq ($(dbExport)$(inputFiles),)
185 772 aaronmk
186 1594 aaronmk
log_ = import/$*$(if $(n),.n=$(n),).$(date).log
187 1190 aaronmk
trace = $(log_:.log=.trace)
188 1575 aaronmk
import = -(set -x; "time" env commit=1\
189 1190 aaronmk
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
190
$(if $(n),,>>$(log_))) 2>&1$(if $(log),$(if $(n),|tee -a $(log_)))
191 1088 aaronmk
# don't abort on import errors, which often relate to invalid input data
192 624 aaronmk
193 1193 aaronmk
import: $(addprefix import-,$(tables)) _always ;
194 718 aaronmk
195
import-%: maps/VegBIEN.%.csv _always
196
	$(import)
197 772 aaronmk
# default:
198
import-%: _always ;
199 718 aaronmk
200 772 aaronmk
else
201
import: _always ;
202
endif
203
204 790 aaronmk
##### Log files from import
205
206 1594 aaronmk
logs := $(wildcard import/*.log import/*.trace)
207 339 aaronmk
208 383 aaronmk
rm_logs: _always
209 339 aaronmk
	$(RM) $(logs)
210
211 783 aaronmk
##### Verification of import
212 264 aaronmk
213 1194 aaronmk
verify: $(addprefix verify-,$(tables)) _always ;
214
215 1199 aaronmk
verify-%: verify/%.ref verify/%.out _always
216 1184 aaronmk
	-$(diffVerbose) $(+_)
217 1088 aaronmk
# don't abort on verification errors, which are expected during development
218 1194 aaronmk
# default:
219 1199 aaronmk
verify-%: verify/%.out _always
220 1200 aaronmk
	$(if $(shell test -e $< && echo t),cat $<)
221 1199 aaronmk
# don't run if verify/%.out's default do-nothing action was used
222 1200 aaronmk
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
223 368 aaronmk
224 1620 aaronmk
verify = $(if $(reverify),"time" $(psqlAsBien) --set=datasource="'$(datasrc)'"\
225
--no-align --field-separator=$$'\t' --pset=footer=off --pset=null=NULL <$< >$@)
226 514 aaronmk
227 1199 aaronmk
verify/%.out: $(mappings)/verify.%.sql _always
228 1192 aaronmk
	$(verify)
229 1194 aaronmk
# default:
230 1199 aaronmk
verify/%.out: _always ;
231 369 aaronmk
232 1199 aaronmk
all += $(wildcard verify/*.out)
233 1192 aaronmk
234 1378 aaronmk
ifneq ($(dbExport),)
235 1082 aaronmk
%.ref: %.ref.sql
236
	$(dbAsBien) $(db) <$< >$@
237
endif
238
239 1667 aaronmk
##### Editing import
240
241
import/rotate: _always
242 1668 aaronmk
	echo "UPDATE party SET organizationname = organizationname||'.$(date)'\
243 1667 aaronmk
WHERE organizationname = '$(db)';"|$(psqlAsBien)
244
245
import/rm: _always
246
	echo "DELETE FROM party WHERE organizationname = '$(db)';"|$(psqlAsBien)
247
248 783 aaronmk
##### Testing
249 368 aaronmk
250 723 aaronmk
hasOwnRef = $(filter-out %.2-step.xml,$@)
251 630 aaronmk
testRef = $(1:.2-step.xml=.xml).ref
252
253 897 aaronmk
define runTest
254 776 aaronmk
@echo "Testing $(abspath $@)..."
255 991 aaronmk
>$@ env test=1 n=$(test_n) $(1)
256 1184 aaronmk
@(set -x; $(diff) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
257 780 aaronmk
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
258 777 aaronmk
$(if $(hasOwnRef),\
259
echo $(emph)"To accept new test output:"$(endEmph);\
260
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
261
,\
262
echo $(emph)"Note: The preceding failed test is compared to another test's\
263
output"$(endEmph);\
264
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
265
)\
266 652 aaronmk
exit $$e;}
267 628 aaronmk
endef
268
269 897 aaronmk
test2File = $(call runTest,$(map))
270 775 aaronmk
271 876 aaronmk
tests :=
272
273 1139 aaronmk
test/$(via).%.xml: maps/$(via).%.full.csv _always
274 775 aaronmk
	$(test2File)
275 876 aaronmk
tests += test/$(via).%.xml
276 629 aaronmk
277 876 aaronmk
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
278 775 aaronmk
	$(test2File)
279 876 aaronmk
tests += test/VegBIEN.%.xml
280 628 aaronmk
281 884 aaronmk
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
282 775 aaronmk
	-$(test2File)
283 722 aaronmk
# Don't abort tester if only 2-step test fails, as it's often finicky
284 876 aaronmk
tests += test/VegBIEN.%.2-step.xml
285 630 aaronmk
286 876 aaronmk
test/import.%.out: maps/VegBIEN.%.csv _always
287 897 aaronmk
	$(call runTest,$(map2db))
288 876 aaronmk
tests += test/import.%.out
289 627 aaronmk
290 876 aaronmk
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
291 724 aaronmk
292 1361 aaronmk
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
293
294 876 aaronmk
test: _always $(testOutputs) ;
295
296
all += $(testOutputs)
297
298 634 aaronmk
# Accepts a test output: make <test_output_path>-ok
299 626 aaronmk
%-ok: _always
300 630 aaronmk
	$(CP) $* $(call testRef,$*)
301 502 aaronmk
302 783 aaronmk
##### Input-type-specific
303 368 aaronmk
304 624 aaronmk
# Each input type needs var $(mapEnv) and targets install, uninstall
305
306 1379 aaronmk
#### DB export
307
308 1378 aaronmk
ifneq ($(dbExport),)
309 256 aaronmk
310 1378 aaronmk
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbExport)))))
311 1196 aaronmk
db := $(datasrc)
312 622 aaronmk
313 1379 aaronmk
### Installation
314 256 aaronmk
315 621 aaronmk
install: _always db ;
316 339 aaronmk
317 621 aaronmk
uninstall: _always rm_db ;
318 368 aaronmk
319 1379 aaronmk
### DB-engine-specific
320 621 aaronmk
321 624 aaronmk
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
322 256 aaronmk
323 622 aaronmk
ifeq ($(dbEngineExt),my)
324 621 aaronmk
325 622 aaronmk
dbEngine := MySQL
326
327 621 aaronmk
bienPassword := $(shell cat $(root)/config/bien_password)
328
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
329
mysqlAsRoot := $(call mysqlAs,root)
330
dbAsBien := $(call mysqlAs,bien)
331
332 1097 aaronmk
dbExists = $(shell echo "SHOW DATABASES LIKE '$(db)';"|$(mysqlAsRoot))
333
334
define createDb
335 1098 aaronmk
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
336 1097 aaronmk
-$(mysqlAsRoot) --database=$(db) <$<
337
endef
338
# ignore errors in db import so that GRANT will still be run
339
340 1378 aaronmk
db: $(dbExport) _always
341 1097 aaronmk
	$(if $(dbExists),,$(createDb))
342 368 aaronmk
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
343 250 aaronmk
344 383 aaronmk
rm_db: _always
345 368 aaronmk
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
346
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
347 365 aaronmk
# ignore errors if grant not defined
348 250 aaronmk
349 1379 aaronmk
## Unrecognized DB engine
350 368 aaronmk
351
else
352 1378 aaronmk
$(error The DB filename $(dbExport) must be db.my.sql)
353 368 aaronmk
endif
354
355 1379 aaronmk
### Other input types
356 622 aaronmk
357 1379 aaronmk
else
358 624 aaronmk
359
install: _always ;
360
uninstall: _always ;
361 1379 aaronmk
362 256 aaronmk
endif
363 1379 aaronmk
364
#### DB connection info
365
366
ifneq ($(dbEngine),)
367
# Must come after dbEngine is set
368
mapEnv := in_engine=$(dbEngine) in_database=$(db)
369
endif