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
tables := $(vegxMaps:map.VegX.%.csv=%)
15 264 aaronmk
vegbienMaps := $(subst .VegX.,.VegBIEN.,$(vegxMaps))
16 247 aaronmk
17 368 aaronmk
root := $(selfDir_3d1bc249)..
18
psqlAsBien := $(root)/bin/psql_vegbien
19 353 aaronmk
20 401 aaronmk
# Config
21
test_n ?= 2
22
23 250 aaronmk
#####
24
25 418 aaronmk
all: _always maps ;
26 247 aaronmk
27
.SUFFIXES:
28
29 383 aaronmk
_always:
30
.PHONY: _always
31 250 aaronmk
32 383 aaronmk
clean: _always
33 256 aaronmk
	$(RM) $(all)
34
35 383 aaronmk
%.out: % _always
36 368 aaronmk
	./$* >$@
37
.PRECIOUS: %.out
38
39 408 aaronmk
all += $(wildcard *.out)
40
41 415 aaronmk
$(root)/%: _always
42
	+$(subMake)
43
44 250 aaronmk
#####
45
46 418 aaronmk
reinstall: _always uninstall install ;
47 264 aaronmk
48
#####
49
50 418 aaronmk
maps: $(vegbienMaps) _always ;
51 368 aaronmk
52
all += $(vegbienMaps)
53
54 415 aaronmk
map.VegBIEN.%.csv: map.VegX.%.csv $(root)/mappings/VegX-VegBIEN.%.csv
55
	$(root)/bin/join_sort <$+ >$@
56 264 aaronmk
.PRECIOUS: map.VegBIEN.%.csv
57
58 368 aaronmk
#####
59
60 418 aaronmk
import: _always $(addprefix import-,$(tables)) verify ;
61 368 aaronmk
62
log = $*$(if $(n),.n=$(n),).log
63
64 339 aaronmk
logs := $(wildcard *.log)
65
66 383 aaronmk
rm_logs: _always
67 339 aaronmk
	$(RM) $(logs)
68
69 264 aaronmk
#####
70
71 418 aaronmk
verify: _always $(addprefix verify-,$(tables)) ;
72 368 aaronmk
73 383 aaronmk
verify-%: verify.%.ref verify.%.out _always
74 404 aaronmk
	$(DIFF) $(+_)
75 368 aaronmk
76 383 aaronmk
%.out: %.sql _always
77 371 aaronmk
	$(psqlAsBien) --no-align --field-separator='	' --pset=footer=off <$< >$@
78 370 aaronmk
.PRECIOUS: %.out
79 369 aaronmk
80 368 aaronmk
#####
81
82 418 aaronmk
test: _always $(addprefix test-,$(tables)) ;
83 368 aaronmk
84 395 aaronmk
test-%: test.%.out _always
85
	$(DIFF) $(<:.out=.ref) $<
86 368 aaronmk
87 395 aaronmk
accept-%: _always
88
	$(CP) $* $(*:.out=.ref)
89
90 368 aaronmk
#####
91
92 256 aaronmk
ifdef db
93
94 264 aaronmk
mapEnv := in_engine=$(dbEngine) in_database=$(db)
95 256 aaronmk
96 368 aaronmk
####
97 256 aaronmk
98 368 aaronmk
ifeq ($(dbEngine),MySQL)
99 339 aaronmk
100 368 aaronmk
bienPassword := $(shell cat $(root)/config/bien_password)
101
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
102
mysqlAsRoot := $(call mysqlAs,root)
103
mysqlAsBien := $(call mysqlAs,bien)
104
105 383 aaronmk
import-%: map.VegBIEN.%.csv _always
106 385 aaronmk
	-$(import_cmd)
107 383 aaronmk
import_cmd = (set -x; "time" env commit=1 $(map) $<) \
108 368 aaronmk
$(if $(n),,>>$(log)) 2>&1$(if $(n),|tee -a $(log))
109 256 aaronmk
110 369 aaronmk
%.ref: %.ref.sql
111 371 aaronmk
	$(mysqlAsBien) $(db) <$< >$@
112 369 aaronmk
.PRECIOUS: %.ref
113 368 aaronmk
114
###
115
116 418 aaronmk
install: _always db ;
117 256 aaronmk
118 418 aaronmk
uninstall: _always rm_db ;
119 256 aaronmk
120 383 aaronmk
db: src.$(db).sql _always
121 368 aaronmk
	-$(mysqlAsRoot) <$<
122
	echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
123 255 aaronmk
# ignore errors in db import so that GRANT will still be run
124 250 aaronmk
125 383 aaronmk
rm_db: _always
126 368 aaronmk
	-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
127
	echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
128 365 aaronmk
# ignore errors if grant not defined
129 250 aaronmk
130 368 aaronmk
###
131
132
else
133
$(error dbEngine variable must be one of the following: MySQL, PostgreSQL)
134
endif
135
136
####
137
138 383 aaronmk
test.%.out: map.VegBIEN.%.csv _always
139 401 aaronmk
	env test=1 n=$(test_n) $(map) $< >$@ 2>&1 || { e=$$?; cat $@; exit $$e;}
140 368 aaronmk
.PRECIOUS: test.%.out
141
142 256 aaronmk
#####
143
144
else
145
$(error db variable must be set)
146
endif
147 368 aaronmk
148
#####
149
150
# Must come after mapEnv is set
151
map := env $(mapEnv) out_database=vegbien $(root)/map