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