Project

General

Profile

1 256 aaronmk
selfDir_3d1bc249 := $(dir $(lastword $(MAKEFILE_LIST)))
2 250 aaronmk
3 368 aaronmk
# Make
4 415 aaronmk
subMake = $(MAKE) $(@F) --directory=$(@D)
5 404 aaronmk
+_ = $(+:_%=)
6 368 aaronmk
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
7
8
# Commands
9 395 aaronmk
CP = cp -p
10
DIFF = diff --unified=2
11 368 aaronmk
12 415 aaronmk
tablesSort := plots organisms stems
13 502 aaronmk
vegxMaps := $(wildcard $(tablesSort:%=maps/VegX.%.csv))
14
vegxMaps += $(filter-out $(vegxMaps),$(wildcard maps/VegX.*.csv))
15
tables := $(vegxMaps:maps/VegX.%.csv=%)
16
vegbienMaps := $(subst VegX.,VegBIEN.,$(vegxMaps))
17 247 aaronmk
18 630 aaronmk
+maps = $(filter maps/%,$(+_))
19
+in = $(filter-out maps/%,$(+_))
20
21 368 aaronmk
root := $(selfDir_3d1bc249)..
22
psqlAsBien := $(root)/bin/psql_vegbien
23 353 aaronmk
24 401 aaronmk
# Config
25
test_n ?= 2
26
27 250 aaronmk
#####
28
29 418 aaronmk
all: _always maps ;
30 247 aaronmk
31
.SUFFIXES:
32
33 383 aaronmk
_always:
34
.PHONY: _always
35 250 aaronmk
36 383 aaronmk
clean: _always
37 256 aaronmk
	$(RM) $(all)
38
39 423 aaronmk
remake: _always clean all ;
40
41 515 aaronmk
%.out: %.make _always
42 368 aaronmk
	./$* >$@
43
.PRECIOUS: %.out
44
45 415 aaronmk
$(root)/%: _always
46
	+$(subMake)
47
48 250 aaronmk
#####
49
50 418 aaronmk
reinstall: _always uninstall install ;
51 264 aaronmk
52
#####
53
54 418 aaronmk
maps: $(vegbienMaps) _always ;
55 368 aaronmk
56
all += $(vegbienMaps)
57
58 502 aaronmk
maps/VegBIEN.%.csv: maps/VegX.%.csv $(root)/mappings/VegX-VegBIEN.%.csv
59 415 aaronmk
	$(root)/bin/join_sort <$+ >$@
60 502 aaronmk
.PRECIOUS: maps/VegBIEN.%.csv
61 264 aaronmk
62 368 aaronmk
#####
63
64 418 aaronmk
import: _always $(addprefix import-,$(tables)) verify ;
65 368 aaronmk
66
log = $*$(if $(n),.n=$(n),).log
67
68 624 aaronmk
import-%: maps/VegBIEN.%.csv _always
69
	-$(import_cmd)
70 636 aaronmk
# ignore import errors, which are often benign (e.g. invalid date format)
71 628 aaronmk
import_cmd = (set -x; "time" env commit=1 $(import) $<) \
72 624 aaronmk
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
73
74 339 aaronmk
logs := $(wildcard *.log)
75
76 383 aaronmk
rm_logs: _always
77 339 aaronmk
	$(RM) $(logs)
78
79 264 aaronmk
#####
80
81 624 aaronmk
ifneq ($(wildcard verify.ref*),)
82
83 514 aaronmk
verify: verify.ref verify.out _always
84 404 aaronmk
	$(DIFF) $(+_)
85 368 aaronmk
86 514 aaronmk
all += verify.out
87
88 383 aaronmk
%.out: %.sql _always
89 429 aaronmk
	$(out_cmd)
90 370 aaronmk
.PRECIOUS: %.out
91 429 aaronmk
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off \
92
--pset=null=NULL <$< >$@
93 369 aaronmk
94 624 aaronmk
else
95
verify: _always ;
96
endif
97
98 368 aaronmk
#####
99
100 624 aaronmk
ifneq ($(wildcard test/),)
101
102 630 aaronmk
tests := test/VegX.xml test/VegBIEN.xml test/VegBIEN.2-step.xml test/import.out
103 368 aaronmk
104 630 aaronmk
test: _always $(tests) ;
105 395 aaronmk
106 630 aaronmk
.PRECIOUS: $(tests)
107 629 aaronmk
108 630 aaronmk
all += $(tests)
109 628 aaronmk
110 630 aaronmk
testRef = $(1:.2-step.xml=.xml).ref
111
112 629 aaronmk
define test
113 630 aaronmk
env test=1 n=$(test_n) $(1) $(if $(+in),<$(+in)) $(+maps) $(if $(2),,>$@) 2>&1 \
114
$(if $(2),| grep -v -E '^$(2)$$' >$@) || { e=$$?; cat $@; exit $$e;}
115
$(DIFF) $(call testRef,$@) $@
116 628 aaronmk
endef
117
118 629 aaronmk
test2File = $(call test,$(map),Processed .* input rows)
119
test2Db = $(call test,$(import))
120
121
test/VegX.xml: $(vegxMaps) _always
122
	$(test2File)
123
124 628 aaronmk
test/VegBIEN.xml: $(vegbienMaps) _always
125 629 aaronmk
	$(test2File)
126 628 aaronmk
127 630 aaronmk
test/VegBIEN.2-step.xml: test/VegX.xml $(vegbienMaps) _always
128
	$(test2File)
129
130 627 aaronmk
test/import.out: $(vegbienMaps) _always
131 629 aaronmk
	$(test2Db)
132 627 aaronmk
133 634 aaronmk
# Accepts a test output: make <test_output_path>-ok
134 626 aaronmk
%-ok: _always
135 630 aaronmk
	$(CP) $* $(call testRef,$*)
136 502 aaronmk
137 624 aaronmk
else
138
test: _always ;
139
endif
140
141 368 aaronmk
#####
142
143 624 aaronmk
# Each input type needs var $(mapEnv) and targets install, uninstall
144
145 635 aaronmk
dbFile := $(firstword $(wildcard src/db.*.sql))
146 256 aaronmk
147 622 aaronmk
ifneq ($(dbFile),)
148 256 aaronmk
149 635 aaronmk
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbFile)))))
150
db := $(notdir $(realpath .))
151 622 aaronmk
152 621 aaronmk
%.ref: %.ref.sql
153
	$(inputDbAsBien) $(db) <$< >$@
154
.PRECIOUS: %.ref
155
156 368 aaronmk
####
157 256 aaronmk
158 621 aaronmk
install: _always db ;
159 339 aaronmk
160 621 aaronmk
uninstall: _always rm_db ;
161 368 aaronmk
162 621 aaronmk
####
163
164 624 aaronmk
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
165 256 aaronmk
166 622 aaronmk
ifeq ($(dbEngineExt),my)
167 621 aaronmk
168 622 aaronmk
dbEngine := MySQL
169
170 621 aaronmk
bienPassword := $(shell cat $(root)/config/bien_password)
171
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
172
mysqlAsRoot := $(call mysqlAs,root)
173
dbAsBien := $(call mysqlAs,bien)
174
175 622 aaronmk
db: $(dbFile) _always
176 635 aaronmk
	echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
177
	-$(mysqlAsRoot) --database=$(db) <$<
178 368 aaronmk
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
179 255 aaronmk
# ignore errors in db import so that GRANT will still be run
180 250 aaronmk
181 383 aaronmk
rm_db: _always
182 368 aaronmk
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
183
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
184 365 aaronmk
# ignore errors if grant not defined
185 250 aaronmk
186 624 aaronmk
###
187 368 aaronmk
188
else
189 635 aaronmk
$(error The DB filename $(dbFile) must have the form db.{my|pg}.sql)
190 368 aaronmk
endif
191
192 622 aaronmk
# Must come after dbEngine is set
193
mapEnv := in_engine=$(dbEngine) in_database=$(db)
194
195 624 aaronmk
####
196
197 256 aaronmk
else
198 624 aaronmk
mapEnv :=
199
install: _always ;
200
uninstall: _always ;
201 256 aaronmk
endif
202 368 aaronmk
203
#####
204
205
# Must come after mapEnv is set
206 628 aaronmk
map := $(if $(mapEnv),env $(mapEnv) ,)$(root)/map
207
import := env out_database=vegbien $(map)