Project

General

Profile

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
# OS
22
os := $(shell uname)
23
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
24

    
25
# Formatting
26
SED = sed -$(if $(filter Darwin,$(os)),E,r)
27

    
28
# System
29
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
30

    
31
# Terminal
32
termCols := $(shell tput cols)
33
esc := '['
34
reset := $(esc)'0m'
35
emph := $(esc)'7m '
36
endEmph := ' '$(reset)
37

    
38
# Commands
39
MKDIR = mkdir -p
40
mkdir = $(MKDIR) $(@D)
41
CP = cp -p
42
diff = diff --unified=2
43
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
44
--width=$(termCols),$(diff))
45

    
46
# Paths
47
datasrc := $(notdir $(realpath .))
48
root := $(selfDir_uZPPqC)..
49
bin := $(root)/bin
50
mappings := $(root)/mappings
51

    
52
# Commands
53
selfMap = $(bin)/cols 0 0
54
psqlOpts := --set ON_ERROR_STOP=1 --quiet
55
psqlAsBien := $(bin)/psql_vegbien $(psqlOpts)
56

    
57
# SVN
58
addDir = $(if $(wildcard $(1)/),svn add --depth=empty $(1),svn mkdir $(1))
59
setSvnIgnore = svn propset svn:ignore $(2) $(1)
60
define addDirWithIgnore
61
$(addDir)
62
$(setSvnIgnore)
63
endef
64

    
65
##### General targets
66

    
67
all: _always maps ;
68

    
69
.SUFFIXES: # turn off built-in suffix rules
70
.SECONDARY: # don't automatically delete intermediate files
71
.DELETE_ON_ERROR: # delete target if recipe fails
72

    
73
_always:
74
.PHONY: _always
75

    
76
clean: _always
77
	$(RM) $(all)
78

    
79
remake: _always clean
80
	+$(selfMake)
81
# re-run make so that cache of existing files is reset
82

    
83
make_script = ./$< >$@
84

    
85
%/: % _always ;
86

    
87
# Must come before `%: %.make` to override it
88
src/%: src/%.make _always
89
	(set -x; $(make_script)) 2>>$<.log
90
.PRECIOUS: src/% # save partial outputs of aborted src make scripts
91

    
92
%: %.make _always
93
	$(make_script)
94

    
95
##### SVN
96

    
97
add: _always
98
	$(call setSvnIgnore,.,$$'')
99
	$(call addDirWithIgnore,src,$$'*')
100
	$(call addDirWithIgnore,maps,$$'.~*')
101
	$(call addDirWithIgnore,import,$$'*')
102
	$(call addDirWithIgnore,test,$$'*.out\n*.xml')
103
	$(call addDirWithIgnore,verify,$$'*.out')
104

    
105
##### Installation
106

    
107
reinstall: _always uninstall install ;
108

    
109
##### Existing maps discovery
110

    
111
srcMap := maps/src.%.csv
112
fullViaMap := maps/%.full.csv
113
directMap := maps/VegBIEN.%.csv
114
allViaMaps := $(filter-out $(srcMap) $(fullViaMap) $(directMap),\
115
$(wildcard maps/*.csv))
116
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
117

    
118
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
119
coreSelfMap := $(mappings)/$(via).self.%.csv
120
noEmptyMap := $(mappings)/$(via)-VegBIEN.%.no_empty.csv
121

    
122
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
123
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard maps/$(via).*.csv))
124

    
125
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
126
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
127
$(wildcard maps/VegBIEN.*.csv))
128
tables := $(directMaps:maps/VegBIEN.%.csv=%)
129

    
130
##### Sources
131

    
132
srcs = $(shell $(bin)/sort_filenames $(wildcard $(exts:%=src/*.$*.%)))
133
isXml = $(filter %.xml,$(firstword $(srcs)))
134
catSrcs = $(bin)/cat$(if $(isXml),,_csv) $(srcs)
135
withCatSrcs = $(catSrcs:$(bin)/=$(bin)/with_) --
136

    
137
cat: $(addprefix cat-,$(tables)) _always ;
138

    
139
cat-%: _always
140
	$(catSrcs)
141

    
142
##### Maps building
143

    
144
srcMaps := $(wildcard maps/src.*.csv)
145

    
146
# Must come before $(root)/% to override it
147
$(coreSelfMap): _always
148
	-+$(subMake)
149
# ignore errors if $(coreSelfMap) does not exist
150

    
151
# Via maps cleanup
152
ifneq ($(filter maps/.%.last_cleanup,$(MAKECMDGOALS)),)
153
maps/.$(via).%.csv.last_cleanup: maps/$(via).%.csv $(coreSelfMap)
154
	$(bin)/in_place $< $(bin)/subtract $(word 2,$+) 0 1
155
	touch $@
156
# default:
157
maps/.$(via).%.csv.last_cleanup: ;
158
else
159
$(viaMaps): _always
160
	$(selfMake) $(@:maps/%=maps/.%.last_cleanup)
161
endif
162

    
163
maps :=
164

    
165
joinSrcMap = $(if $(wildcard maps/src.$*.csv),$(bin)/in_place $@\
166
$(bin)/intersect maps/src.$*.csv 0)
167

    
168
makeFullCsv = $(if $(shell test -e $(word 2,$+) && echo t),\
169
env ignore=1 $(bin)/union <$+|$(bin)/sort_map >$@,$(CP) $< $@)
170
# can't use $(wildcard) because it won't recheck file after $(coreSelfMap) runs
171

    
172
maps/$(via).%.full.csv: maps/$(via).%.csv $(coreSelfMap)
173
	$(makeFullCsv)
174
	$(joinSrcMap)
175
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
176

    
177
maps/VegBIEN.%.csv: maps/$(via).%.full.csv $(coreMap)
178
	$(bin)/join <$+|$(bin)/sort_map >$@
179
maps += $(autogenMaps)
180

    
181
maps: $(maps) _always ;
182

    
183
all += $(maps)
184

    
185
##### Maps validation
186

    
187
missing_mappings: _always missing_join_mappings missing_input_mappings ;
188

    
189
missing_%_mappings: _always # stem is one of join|input
190
	@echo $(emph)"Missing $* mappings:"$(endEmph)
191
	@+$(selfMake) remake 2>&1\
192
|$(SED) -n 's/^.*No $* mapping for ([0-9A-Za-z_-]+).*$$/\1/p'\
193
$(if $(filter join,$*),|$(bin)/ucase_first 0)|sort|uniq
194

    
195
##### External dependencies
196

    
197
$(root)/%: _always
198
	+$(subMake)
199
.PRECIOUS: $(root)/% # let ext. dir's Makefile decide whether to delete on error
200

    
201
##### Mapping
202

    
203
dbExport := $(firstword $(wildcard src/db.*.sql))
204
inputFiles := $(wildcard $(exts:%=src/*.%))
205

    
206
+maps = $(filter maps/% $(mappings)/%,$(+_))
207
<in = $(firstword $(filter-out $(+maps),$(+_)))
208
map = $(if $(<in),<$(<in),\
209
$(if $(srcs),$(withCatSrcs),\
210
$(if $(mapEnv),env $(mapEnv),\
211
$(error No input file src/*.$*.{$(exts)}))))\
212
$(root)/map $(+maps)
213
map2db = env out_database=vegbien $(map)
214

    
215
##### Import to VegBIEN
216

    
217
ifneq ($(dbExport)$(inputFiles),)
218

    
219
log_ = import/$*$(if $(n),.n=$(n),).$(date).log
220
trace = $(log_:.log=.trace)
221
import = -(set -x; "time" env commit=1\
222
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
223
$(if $(n),,>>$(log_))) 2>&1$(if $(log),$(if $(n),|tee -a $(log_)))
224
# don't abort on import errors, which often relate to invalid input data
225

    
226
import: $(addprefix import-,$(tables)) _always ;
227

    
228
import-%: maps/VegBIEN.%.csv _always
229
	$(import)
230
# default:
231
import-%: _always ;
232

    
233
else
234
import: _always ;
235
endif
236

    
237
##### Log files from import
238

    
239
logs := $(wildcard import/*.log import/*.trace)
240

    
241
rm_logs: _always
242
	$(RM) $(logs)
243

    
244
##### Verification of import
245

    
246
verify: $(addprefix verify-,$(tables)) _always ;
247

    
248
verify-%: verify/%.ref verify/%.out _always
249
	-$(diffVerbose) $(+_)
250
# don't abort on verification errors, which are expected during development
251
# default:
252
verify-%: verify/%.out _always
253
	$(if $(shell test -e $< && echo t),cat $<)
254
# don't run if verify/%.out's default do-nothing action was used
255
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
256

    
257
verify = $(if $(reverify),"time" $(psqlAsBien) --set=datasource="'$(datasrc)'"\
258
--no-align --field-separator=$$'\t' --pset=footer=off --pset=null=NULL <$< >$@)
259

    
260
verify/%.out: $(mappings)/verify.%.sql _always
261
	$(verify)
262
# default:
263
verify/%.out: _always ;
264

    
265
all += $(wildcard verify/*.out)
266

    
267
ifneq ($(dbExport),)
268
%.ref: %.ref.sql
269
	$(dbAsBien) $(db) <$< >$@
270
endif
271

    
272
##### Editing import
273

    
274
import/rotate: _always
275
	echo "UPDATE party SET organizationname = organizationname||'.$(date)'\
276
WHERE organizationname = '$(db)';"|$(psqlAsBien)
277

    
278
import/rm: _always
279
	echo "DELETE FROM party WHERE organizationname = '$(db)';"|$(psqlAsBien)
280

    
281
##### Testing
282

    
283
hasOwnRef = $(filter-out %.2-step.xml,$@)
284
testRef = $(1:.2-step.xml=.xml).ref
285

    
286
define runTest
287
@echo "Testing $(abspath $@)..."
288
>$@ env test=1 n=$(test_n) $(1)
289
@(set -x; $(diff) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
290
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
291
$(if $(hasOwnRef),\
292
echo $(emph)"To accept new test output:"$(endEmph);\
293
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
294
,\
295
echo $(emph)"Note: The preceding failed test is compared to another test's\
296
output"$(endEmph);\
297
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
298
)\
299
exit $$e;}
300
endef
301

    
302
test2File = $(call runTest,$(map))
303

    
304
tests :=
305

    
306
test/$(via).%.xml: maps/$(via).%.full.csv _always
307
	$(test2File)
308
tests += test/$(via).%.xml
309

    
310
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
311
	$(test2File)
312
tests += test/VegBIEN.%.xml
313

    
314
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
315
	-$(test2File)
316
# Don't abort tester if only 2-step test fails, as it's often finicky
317
tests += test/VegBIEN.%.2-step.xml
318

    
319
test/import.%.out: maps/VegBIEN.%.csv _always
320
	$(call runTest,$(map2db))
321
tests += test/import.%.out
322

    
323
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
324

    
325
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
326

    
327
test: _always $(testOutputs) ;
328

    
329
all += $(testOutputs)
330

    
331
# Accepts a test output: make <test_output_path>-ok
332
%-ok: _always
333
	$(CP) $* $(call testRef,$*)
334

    
335
##### Input-type-specific
336

    
337
# Each input type needs var $(mapEnv) and targets install, uninstall
338

    
339
#### DB export
340

    
341
ifneq ($(dbExport),)
342

    
343
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbExport)))))
344
db := $(datasrc)
345

    
346
### Installation
347

    
348
install: _always db ;
349

    
350
uninstall: _always rm_db ;
351

    
352
### DB-engine-specific
353

    
354
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
355

    
356
ifeq ($(dbEngineExt),my)
357

    
358
dbEngine := MySQL
359

    
360
bienPassword := $(shell cat $(root)/config/bien_password)
361
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
362
mysqlAsRoot := $(call mysqlAs,root)
363
dbAsBien := $(call mysqlAs,bien)
364

    
365
dbExists = $(shell echo "SHOW DATABASES LIKE '$(db)';"|$(mysqlAsRoot))
366

    
367
define createDb
368
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
369
-$(mysqlAsRoot) --database=$(db) <$<
370
endef
371
# ignore errors in db import so that GRANT will still be run
372

    
373
db: $(dbExport) _always
374
	$(if $(dbExists),,$(createDb))
375
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
376

    
377
rm_db: _always
378
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
379
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
380
# ignore errors if grant not defined
381

    
382
## Unrecognized DB engine
383

    
384
else
385
$(error The DB filename $(dbExport) must be db.my.sql)
386
endif
387

    
388
### Other input types
389

    
390
else
391

    
392
install: _always ;
393
uninstall: _always ;
394

    
395
endif
396

    
397
#### DB connection info
398

    
399
ifneq ($(dbEngine),)
400
# Must come after dbEngine is set
401
mapEnv := in_engine=$(dbEngine) in_database=$(db)
402
endif
(2-2/2)