Project

General

Profile

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