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
##### Sources
110

    
111
srcs = $(shell $(bin)/sort_filenames $(wildcard $(exts:%=src/*.$*.%)))
112
isXml = $(filter %.xml,$(firstword $(srcs)))
113
catSrcs = cat$(if $(isXml),,_csv) $(srcs)
114

    
115
##### Maps
116

    
117
srcMap := maps/src.%.csv
118
fullViaMap := maps/%.full.csv
119
directMap := maps/VegBIEN.%.csv
120
allViaMaps := $(filter-out $(srcMap) $(fullViaMap) $(directMap),\
121
$(wildcard maps/*.csv))
122
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
123

    
124
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
125
coreSelfMap := $(mappings)/$(via).self.%.csv
126
noEmptyMap := $(mappings)/$(via)-VegBIEN.%.no_empty.csv
127

    
128
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
129
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard maps/$(via).*.csv))
130

    
131
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
132
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
133
$(wildcard maps/VegBIEN.*.csv))
134
tables := $(directMaps:maps/VegBIEN.%.csv=%)
135

    
136
srcMaps := $(wildcard maps/src.*.csv)
137

    
138
# Must come before $(root)/% to override it
139
$(coreSelfMap): _always
140
	-+$(subMake)
141
# ignore errors if $(coreSelfMap) does not exist
142

    
143
# Via maps cleanup
144
ifneq ($(filter maps/.%.last_cleanup,$(MAKECMDGOALS)),)
145
maps/.$(via).%.csv.last_cleanup: maps/$(via).%.csv $(coreSelfMap)
146
	$(bin)/in_place $< $(bin)/subtract $(word 2,$+) 0 1
147
	touch $@
148
# default:
149
maps/.$(via).%.csv.last_cleanup: ;
150
else
151
$(viaMaps): _always
152
	$(selfMake) $(@:maps/%=maps/.%.last_cleanup)
153
endif
154

    
155
maps :=
156

    
157
joinSrcMap = $(if $(wildcard maps/src.$*.csv),$(bin)/in_place $@\
158
$(bin)/intersect maps/src.$*.csv 0)
159

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

    
164
maps/$(via).%.full.csv: maps/$(via).%.csv $(coreSelfMap)
165
	$(makeFullCsv)
166
	$(joinSrcMap)
167
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
168

    
169
maps/VegBIEN.%.csv: maps/$(via).%.full.csv $(coreMap)
170
	$(bin)/join <$+|$(bin)/sort_map >$@
171
maps += $(autogenMaps)
172

    
173
maps: $(maps) _always ;
174

    
175
all += $(maps)
176

    
177
##### Maps validation
178

    
179
missing_mappings: _always missing_join_mappings missing_input_mappings ;
180

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

    
187
##### External dependencies
188

    
189
$(root)/%: _always
190
	+$(subMake)
191
.PRECIOUS: $(root)/% # let ext. dir's Makefile decide whether to delete on error
192

    
193
##### Mapping
194

    
195
dbExport := $(firstword $(wildcard src/db.*.sql))
196
inputFiles := $(wildcard $(exts:%=src/*.%))
197

    
198
+maps = $(filter maps/% $(mappings)/%,$(+_))
199
<in = $(firstword $(filter-out $(+maps),$(+_)))
200
map = $(if $(<in),<$(<in),\
201
$(if $(srcs),$(bin)/with_$(catSrcs) --,\
202
$(if $(mapEnv),env $(mapEnv),\
203
$(error No input file src/*.$*.{$(exts)}))))\
204
$(root)/map $(+maps)
205
map2db = env out_database=vegbien $(map)
206

    
207
##### Import to VegBIEN
208

    
209
ifneq ($(dbExport)$(inputFiles),)
210

    
211
log_ = import/$*$(if $(n),.n=$(n),).$(date).log
212
trace = $(log_:.log=.trace)
213
import = -(set -x; "time" env commit=1\
214
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
215
$(if $(n),,>>$(log_))) 2>&1$(if $(log),$(if $(n),|tee -a $(log_)))
216
# don't abort on import errors, which often relate to invalid input data
217

    
218
import: $(addprefix import-,$(tables)) _always ;
219

    
220
import-%: maps/VegBIEN.%.csv _always
221
	$(import)
222
# default:
223
import-%: _always ;
224

    
225
else
226
import: _always ;
227
endif
228

    
229
##### Log files from import
230

    
231
logs := $(wildcard import/*.log import/*.trace)
232

    
233
rm_logs: _always
234
	$(RM) $(logs)
235

    
236
##### Verification of import
237

    
238
verify: $(addprefix verify-,$(tables)) _always ;
239

    
240
verify-%: verify/%.ref verify/%.out _always
241
	-$(diffVerbose) $(+_)
242
# don't abort on verification errors, which are expected during development
243
# default:
244
verify-%: verify/%.out _always
245
	$(if $(shell test -e $< && echo t),cat $<)
246
# don't run if verify/%.out's default do-nothing action was used
247
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
248

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

    
252
verify/%.out: $(mappings)/verify.%.sql _always
253
	$(verify)
254
# default:
255
verify/%.out: _always ;
256

    
257
all += $(wildcard verify/*.out)
258

    
259
ifneq ($(dbExport),)
260
%.ref: %.ref.sql
261
	$(dbAsBien) $(db) <$< >$@
262
endif
263

    
264
##### Editing import
265

    
266
import/rotate: _always
267
	echo "UPDATE party SET organizationname = organizationname||'.$(date)'\
268
WHERE organizationname = '$(db)';"|$(psqlAsBien)
269

    
270
import/rm: _always
271
	echo "DELETE FROM party WHERE organizationname = '$(db)';"|$(psqlAsBien)
272

    
273
##### Testing
274

    
275
hasOwnRef = $(filter-out %.2-step.xml,$@)
276
testRef = $(1:.2-step.xml=.xml).ref
277

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

    
294
test2File = $(call runTest,$(map))
295

    
296
tests :=
297

    
298
test/$(via).%.xml: maps/$(via).%.full.csv _always
299
	$(test2File)
300
tests += test/$(via).%.xml
301

    
302
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
303
	$(test2File)
304
tests += test/VegBIEN.%.xml
305

    
306
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
307
	-$(test2File)
308
# Don't abort tester if only 2-step test fails, as it's often finicky
309
tests += test/VegBIEN.%.2-step.xml
310

    
311
test/import.%.out: maps/VegBIEN.%.csv _always
312
	$(call runTest,$(map2db))
313
tests += test/import.%.out
314

    
315
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
316

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

    
319
test: _always $(testOutputs) ;
320

    
321
all += $(testOutputs)
322

    
323
# Accepts a test output: make <test_output_path>-ok
324
%-ok: _always
325
	$(CP) $* $(call testRef,$*)
326

    
327
##### Input-type-specific
328

    
329
# Each input type needs var $(mapEnv) and targets install, uninstall
330

    
331
#### DB export
332

    
333
ifneq ($(dbExport),)
334

    
335
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbExport)))))
336
db := $(datasrc)
337

    
338
### Installation
339

    
340
install: _always db ;
341

    
342
uninstall: _always rm_db ;
343

    
344
### DB-engine-specific
345

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

    
348
ifeq ($(dbEngineExt),my)
349

    
350
dbEngine := MySQL
351

    
352
bienPassword := $(shell cat $(root)/config/bien_password)
353
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
354
mysqlAsRoot := $(call mysqlAs,root)
355
dbAsBien := $(call mysqlAs,bien)
356

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

    
359
define createDb
360
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
361
-$(mysqlAsRoot) --database=$(db) <$<
362
endef
363
# ignore errors in db import so that GRANT will still be run
364

    
365
db: $(dbExport) _always
366
	$(if $(dbExists),,$(createDb))
367
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
368

    
369
rm_db: _always
370
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
371
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
372
# ignore errors if grant not defined
373

    
374
## Unrecognized DB engine
375

    
376
else
377
$(error The DB filename $(dbExport) must be db.my.sql)
378
endif
379

    
380
### Other input types
381

    
382
else
383

    
384
install: _always ;
385
uninstall: _always ;
386

    
387
endif
388

    
389
#### DB connection info
390

    
391
ifneq ($(dbEngine),)
392
# Must come after dbEngine is set
393
mapEnv := in_engine=$(dbEngine) in_database=$(db)
394
endif
(2-2/2)