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 339 aaronmk
logs := $(wildcard *.log)
66
67 383 aaronmk
rm_logs: _always
68 339 aaronmk
	$(RM) $(logs)
69
70 264 aaronmk
#####
71
72 514 aaronmk
verify: verify.ref verify.out _always
73 404 aaronmk
	$(DIFF) $(+_)
74 368 aaronmk
75 514 aaronmk
all += verify.out
76
77 383 aaronmk
%.out: %.sql _always
78 429 aaronmk
	$(out_cmd)
79 370 aaronmk
.PRECIOUS: %.out
80 429 aaronmk
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off \
81
--pset=null=NULL <$< >$@
82 369 aaronmk
83 368 aaronmk
#####
84
85 619 aaronmk
test: test/import.out _always
86 395 aaronmk
	$(DIFF) $(<:.out=.ref) $<
87 368 aaronmk
88 619 aaronmk
all += test/import.out
89 395 aaronmk
90 513 aaronmk
accept: _always
91 619 aaronmk
	$(CP) test/import.out test/import.ref
92 502 aaronmk
93 368 aaronmk
#####
94
95 622 aaronmk
dbFile := $(firstword $(wildcard src/*.sql))
96 256 aaronmk
97 622 aaronmk
ifneq ($(dbFile),)
98 256 aaronmk
99 622 aaronmk
dbFileBasename := $(basename $(notdir $(dbFile)))
100
dbEngineExt := $(subst .,,$(suffix $(dbFileBasename)))
101
db := $(basename $(dbFileBasename))
102
103 621 aaronmk
%.ref: %.ref.sql
104
	$(inputDbAsBien) $(db) <$< >$@
105
.PRECIOUS: %.ref
106
107 368 aaronmk
####
108 256 aaronmk
109 621 aaronmk
install: _always db ;
110 339 aaronmk
111 621 aaronmk
uninstall: _always rm_db ;
112 368 aaronmk
113 621 aaronmk
####
114
115 502 aaronmk
import-%: maps/VegBIEN.%.csv _always
116 385 aaronmk
	-$(import_cmd)
117 383 aaronmk
import_cmd = (set -x; "time" env commit=1 $(map) $<) \
118 368 aaronmk
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
119 256 aaronmk
120 621 aaronmk
####
121 368 aaronmk
122 621 aaronmk
test/import.out: $(vegbienMaps) _always
123
	env test=1 n=$(test_n) $(map) $(+_) >$@ 2>&1 || { e=$$?; cat $@; exit $$e;}
124
.PRECIOUS: test/import.out
125 368 aaronmk
126 621 aaronmk
####
127 256 aaronmk
128 621 aaronmk
# Each DB engine needs var $(dbAsBien) and targets db, rm_db
129 256 aaronmk
130 622 aaronmk
ifeq ($(dbEngineExt),my)
131 621 aaronmk
132 622 aaronmk
dbEngine := MySQL
133
134 621 aaronmk
bienPassword := $(shell cat $(root)/config/bien_password)
135
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
136
mysqlAsRoot := $(call mysqlAs,root)
137
dbAsBien := $(call mysqlAs,bien)
138
139 622 aaronmk
db: $(dbFile) _always
140 368 aaronmk
	-$(mysqlAsRoot) <$<
141
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
142 255 aaronmk
# ignore errors in db import so that GRANT will still be run
143 250 aaronmk
144 383 aaronmk
rm_db: _always
145 368 aaronmk
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
146
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
147 365 aaronmk
# ignore errors if grant not defined
148 250 aaronmk
149 621 aaronmk
####
150 368 aaronmk
151
else
152 622 aaronmk
$(error The DB engine extension "$(dbEngineExt)" for $(dbFile) must be one of\
153
the following: my, pg)
154 368 aaronmk
endif
155
156 256 aaronmk
#####
157
158 622 aaronmk
# Must come after dbEngine is set
159
mapEnv := in_engine=$(dbEngine) in_database=$(db)
160
161 256 aaronmk
else
162 622 aaronmk
$(error The src subdir must contain a file <db_name>.{my|pg}.sql)
163 256 aaronmk
endif
164 368 aaronmk
165
#####
166
167
# Must come after mapEnv is set
168
map := env $(mapEnv) out_database=vegbien $(root)/map