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
vegxMaps := $(wildcard $(tablesSort:%=map.VegX.%.csv))
14 440 aaronmk
vegxMaps += $(filter-out $(vegxMaps),$(wildcard map.VegX.*.csv))
15 415 aaronmk
tables := $(vegxMaps:map.VegX.%.csv=%)
16 264 aaronmk
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 408 aaronmk
all += $(wildcard *.out)
43
44 415 aaronmk
$(root)/%: _always
45
	+$(subMake)
46
47 250 aaronmk
#####
48
49 418 aaronmk
reinstall: _always uninstall install ;
50 264 aaronmk
51
#####
52
53 418 aaronmk
maps: $(vegbienMaps) _always ;
54 368 aaronmk
55
all += $(vegbienMaps)
56
57 415 aaronmk
map.VegBIEN.%.csv: map.VegX.%.csv $(root)/mappings/VegX-VegBIEN.%.csv
58
	$(root)/bin/join_sort <$+ >$@
59 264 aaronmk
.PRECIOUS: map.VegBIEN.%.csv
60
61 368 aaronmk
#####
62
63 418 aaronmk
import: _always $(addprefix import-,$(tables)) verify ;
64 368 aaronmk
65
log = $*$(if $(n),.n=$(n),).log
66
67 339 aaronmk
logs := $(wildcard *.log)
68
69 383 aaronmk
rm_logs: _always
70 339 aaronmk
	$(RM) $(logs)
71
72 264 aaronmk
#####
73
74 418 aaronmk
verify: _always $(addprefix verify-,$(tables)) ;
75 368 aaronmk
76 383 aaronmk
verify-%: verify.%.ref verify.%.out _always
77 404 aaronmk
	$(DIFF) $(+_)
78 368 aaronmk
79 383 aaronmk
%.out: %.sql _always
80 429 aaronmk
	$(out_cmd)
81 370 aaronmk
.PRECIOUS: %.out
82 429 aaronmk
out_cmd = $(psqlAsBien) --no-align --field-separator='	' --pset=footer=off \
83
--pset=null=NULL <$< >$@
84 369 aaronmk
85 368 aaronmk
#####
86
87 418 aaronmk
test: _always $(addprefix test-,$(tables)) ;
88 368 aaronmk
89 395 aaronmk
test-%: test.%.out _always
90
	$(DIFF) $(<:.out=.ref) $<
91 368 aaronmk
92 395 aaronmk
accept-%: _always
93
	$(CP) $* $(*:.out=.ref)
94
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 383 aaronmk
import-%: map.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 383 aaronmk
test.%.out: map.VegBIEN.%.csv _always
144 401 aaronmk
	env test=1 n=$(test_n) $(map) $< >$@ 2>&1 || { e=$$?; cat $@; exit $$e;}
145 368 aaronmk
.PRECIOUS: test.%.out
146
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