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
import_cmd = (set -x; "time" env commit=1 $(map) $<) \
68
$(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 619 aaronmk
test: test/import.out _always
99 395 aaronmk
	$(DIFF) $(<:.out=.ref) $<
100 368 aaronmk
101 619 aaronmk
all += test/import.out
102 395 aaronmk
103 513 aaronmk
accept: _always
104 619 aaronmk
	$(CP) test/import.out test/import.ref
105 502 aaronmk
106 624 aaronmk
else
107
test: _always ;
108
endif
109
110 368 aaronmk
#####
111
112 624 aaronmk
# Each input type needs var $(mapEnv) and targets install, uninstall
113
114 622 aaronmk
dbFile := $(firstword $(wildcard src/*.sql))
115 256 aaronmk
116 622 aaronmk
ifneq ($(dbFile),)
117 256 aaronmk
118 622 aaronmk
dbFileBasename := $(basename $(notdir $(dbFile)))
119
dbEngineExt := $(subst .,,$(suffix $(dbFileBasename)))
120
db := $(basename $(dbFileBasename))
121
122 621 aaronmk
%.ref: %.ref.sql
123
	$(inputDbAsBien) $(db) <$< >$@
124
.PRECIOUS: %.ref
125
126 368 aaronmk
####
127 256 aaronmk
128 621 aaronmk
install: _always db ;
129 339 aaronmk
130 621 aaronmk
uninstall: _always rm_db ;
131 368 aaronmk
132 621 aaronmk
####
133
134
test/import.out: $(vegbienMaps) _always
135
	env test=1 n=$(test_n) $(map) $(+_) >$@ 2>&1 || { e=$$?; cat $@; exit $$e;}
136
.PRECIOUS: test/import.out
137 368 aaronmk
138 621 aaronmk
####
139 256 aaronmk
140 624 aaronmk
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
141 256 aaronmk
142 622 aaronmk
ifeq ($(dbEngineExt),my)
143 621 aaronmk
144 622 aaronmk
dbEngine := MySQL
145
146 621 aaronmk
bienPassword := $(shell cat $(root)/config/bien_password)
147
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
148
mysqlAsRoot := $(call mysqlAs,root)
149
dbAsBien := $(call mysqlAs,bien)
150
151 622 aaronmk
db: $(dbFile) _always
152 368 aaronmk
	-$(mysqlAsRoot) <$<
153
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
154 255 aaronmk
# ignore errors in db import so that GRANT will still be run
155 250 aaronmk
156 383 aaronmk
rm_db: _always
157 368 aaronmk
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
158
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
159 365 aaronmk
# ignore errors if grant not defined
160 250 aaronmk
161 624 aaronmk
###
162 368 aaronmk
163
else
164 624 aaronmk
$(error The DB filename $(dbFile) must have the form <db_name>.{my|pg}.sql)
165 368 aaronmk
endif
166
167 622 aaronmk
# Must come after dbEngine is set
168
mapEnv := in_engine=$(dbEngine) in_database=$(db)
169
170 624 aaronmk
####
171
172 256 aaronmk
else
173 624 aaronmk
mapEnv :=
174
install: _always ;
175
uninstall: _always ;
176 256 aaronmk
endif
177 368 aaronmk
178
#####
179
180
# Must come after mapEnv is set
181
map := env $(mapEnv) out_database=vegbien $(root)/map