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 383 aaronmk
%.out: % _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 418 aaronmk
verify: _always $(addprefix verify-,$(tables)) ;
73 368 aaronmk
74 502 aaronmk
verify-%: verify/%.ref verify/%.out _always
75 404 aaronmk
	$(DIFF) $(+_)
76 368 aaronmk
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 502 aaronmk
all += $(wildcard verify/*.out)
84
85 368 aaronmk
#####
86
87 513 aaronmk
test: test.out _always
88 395 aaronmk
	$(DIFF) $(<:.out=.ref) $<
89 368 aaronmk
90 513 aaronmk
all += test.out
91 395 aaronmk
92 513 aaronmk
accept: _always
93
	$(CP) test.out test.ref
94 502 aaronmk
95 368 aaronmk
#####
96
97 256 aaronmk
ifdef db
98
99 264 aaronmk
mapEnv := in_engine=$(dbEngine) in_database=$(db)
100 256 aaronmk
101 368 aaronmk
####
102 256 aaronmk
103 368 aaronmk
ifeq ($(dbEngine),MySQL)
104 339 aaronmk
105 368 aaronmk
bienPassword := $(shell cat $(root)/config/bien_password)
106
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
107
mysqlAsRoot := $(call mysqlAs,root)
108
mysqlAsBien := $(call mysqlAs,bien)
109
110 502 aaronmk
import-%: maps/VegBIEN.%.csv _always
111 385 aaronmk
	-$(import_cmd)
112 383 aaronmk
import_cmd = (set -x; "time" env commit=1 $(map) $<) \
113 368 aaronmk
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
114 256 aaronmk
115 369 aaronmk
%.ref: %.ref.sql
116 371 aaronmk
	$(mysqlAsBien) $(db) <$< >$@
117 369 aaronmk
.PRECIOUS: %.ref
118 368 aaronmk
119
###
120
121 418 aaronmk
install: _always db ;
122 256 aaronmk
123 418 aaronmk
uninstall: _always rm_db ;
124 256 aaronmk
125 383 aaronmk
db: src.$(db).sql _always
126 368 aaronmk
	-$(mysqlAsRoot) <$<
127
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
128 255 aaronmk
# ignore errors in db import so that GRANT will still be run
129 250 aaronmk
130 383 aaronmk
rm_db: _always
131 368 aaronmk
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
132
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
133 365 aaronmk
# ignore errors if grant not defined
134 250 aaronmk
135 368 aaronmk
###
136
137
else
138
$(error dbEngine variable must be one of the following: MySQL, PostgreSQL)
139
endif
140
141
####
142
143 513 aaronmk
test.out: $(vegbienMaps) _always
144
	env test=1 n=$(test_n) $(map) $(+_) >$@ 2>&1 || { e=$$?; cat $@; exit $$e;}
145
.PRECIOUS: test.out
146 368 aaronmk
147 256 aaronmk
#####
148
149
else
150
$(error db variable must be set)
151
endif
152 368 aaronmk
153
#####
154
155
# Must come after mapEnv is set
156
map := env $(mapEnv) out_database=vegbien $(root)/map