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