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