1 |
783
|
aaronmk
|
##### Configuration
|
2 |
|
|
|
3 |
1190
|
aaronmk
|
log ?= $(if $(test),,1)
|
4 |
1257
|
aaronmk
|
profile ?=
|
5 |
1386
|
aaronmk
|
exts ?= csv tsv txt xml
|
6 |
783
|
aaronmk
|
test_n ?= 2
|
7 |
|
|
tablesSort ?= plots organisms stems
|
8 |
|
|
|
9 |
|
|
##### Vars/functions
|
10 |
|
|
|
11 |
640
|
aaronmk
|
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
|
12 |
250
|
aaronmk
|
|
13 |
368
|
aaronmk
|
# Make
|
14 |
640
|
aaronmk
|
SHELL := /bin/bash
|
15 |
415
|
aaronmk
|
subMake = $(MAKE) $(@F) --directory=$(@D)
|
16 |
404
|
aaronmk
|
+_ = $(+:_%=)
|
17 |
368
|
aaronmk
|
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
|
18 |
|
|
|
19 |
790
|
aaronmk
|
# System
|
20 |
|
|
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
|
21 |
|
|
|
22 |
640
|
aaronmk
|
# Terminal
|
23 |
1184
|
aaronmk
|
termCols := $(shell tput cols)
|
24 |
640
|
aaronmk
|
esc := '['
|
25 |
|
|
reset := $(esc)'0m'
|
26 |
|
|
emph := $(esc)'7m '
|
27 |
|
|
endEmph := ' '$(reset)
|
28 |
|
|
|
29 |
368
|
aaronmk
|
# Commands
|
30 |
1245
|
aaronmk
|
MKDIR = mkdir -p
|
31 |
|
|
mkdir = $(MKDIR) $(@D)
|
32 |
395
|
aaronmk
|
CP = cp -p
|
33 |
1184
|
aaronmk
|
diff = diff --unified=2
|
34 |
|
|
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
|
35 |
|
|
--width=$(termCols),$(diff))
|
36 |
368
|
aaronmk
|
|
37 |
783
|
aaronmk
|
# Paths
|
38 |
1289
|
aaronmk
|
datasrc := $(notdir $(realpath .))
|
39 |
640
|
aaronmk
|
root := $(selfDir_uZPPqC)..
|
40 |
1289
|
aaronmk
|
bin := $(root)/bin
|
41 |
775
|
aaronmk
|
mappings := $(root)/mappings
|
42 |
1289
|
aaronmk
|
|
43 |
|
|
# Commands
|
44 |
|
|
join = $(bin)/join_union_sort
|
45 |
1081
|
aaronmk
|
psqlOpts := --set ON_ERROR_STOP=1 --quiet
|
46 |
1289
|
aaronmk
|
psqlAsBien := $(bin)/psql_vegbien $(psqlOpts)
|
47 |
353
|
aaronmk
|
|
48 |
783
|
aaronmk
|
##### General targets
|
49 |
250
|
aaronmk
|
|
50 |
418
|
aaronmk
|
all: _always maps ;
|
51 |
247
|
aaronmk
|
|
52 |
1260
|
aaronmk
|
.SUFFIXES: # turn off built-in suffix rules
|
53 |
|
|
.SECONDARY: # don't automatically delete intermediate files
|
54 |
1354
|
aaronmk
|
.DELETE_ON_ERROR: # delete target if recipe fails
|
55 |
247
|
aaronmk
|
|
56 |
383
|
aaronmk
|
_always:
|
57 |
|
|
.PHONY: _always
|
58 |
250
|
aaronmk
|
|
59 |
383
|
aaronmk
|
clean: _always
|
60 |
256
|
aaronmk
|
$(RM) $(all)
|
61 |
|
|
|
62 |
515
|
aaronmk
|
%.out: %.make _always
|
63 |
368
|
aaronmk
|
./$* >$@
|
64 |
|
|
|
65 |
783
|
aaronmk
|
##### SVN
|
66 |
250
|
aaronmk
|
|
67 |
766
|
aaronmk
|
svn_props: _always
|
68 |
1201
|
aaronmk
|
svn propset svn:ignore $$'*.log\n*.trace\nsrc*' .
|
69 |
951
|
aaronmk
|
$(if $(wildcard maps/),svn propset svn:ignore $$'.~*' maps)
|
70 |
1394
|
aaronmk
|
$(if $(wildcard src/),svn propset svn:ignore $$'*' src)
|
71 |
|
|
$(if $(wildcard test/),svn propset svn:ignore $$'*.out\n*.xml' test)
|
72 |
1199
|
aaronmk
|
$(if $(wildcard verify/),svn propset svn:ignore $$'*.out' verify)
|
73 |
766
|
aaronmk
|
|
74 |
783
|
aaronmk
|
##### Installation
|
75 |
766
|
aaronmk
|
|
76 |
418
|
aaronmk
|
reinstall: _always uninstall install ;
|
77 |
264
|
aaronmk
|
|
78 |
783
|
aaronmk
|
##### Maps
|
79 |
264
|
aaronmk
|
|
80 |
1289
|
aaronmk
|
srcMap := maps/src.%.csv
|
81 |
1139
|
aaronmk
|
fullViaMap := maps/%.full.csv
|
82 |
1289
|
aaronmk
|
directMap := maps/VegBIEN.%.csv
|
83 |
|
|
allViaMaps := $(filter-out $(srcMap) $(fullViaMap) $(directMap),\
|
84 |
1139
|
aaronmk
|
$(wildcard maps/*.csv))
|
85 |
728
|
aaronmk
|
via := $(firstword $(sort $(basename $(basename $(notdir $(allViaMaps))))))
|
86 |
783
|
aaronmk
|
|
87 |
775
|
aaronmk
|
coreMap := $(mappings)/$(via)-VegBIEN.%.csv
|
88 |
1139
|
aaronmk
|
selfMap := $(mappings)/$(via).self.%.csv
|
89 |
1289
|
aaronmk
|
noEmptyMap := $(mappings)/$(via)-VegBIEN.%.no_empty.csv
|
90 |
783
|
aaronmk
|
|
91 |
749
|
aaronmk
|
viaMaps := $(wildcard $(tablesSort:%=maps/$(via).%.csv))
|
92 |
1139
|
aaronmk
|
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard maps/$(via).*.csv))
|
93 |
783
|
aaronmk
|
|
94 |
728
|
aaronmk
|
autogenMaps := $(subst $(via).,VegBIEN.,$(viaMaps))
|
95 |
|
|
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
|
96 |
|
|
$(wildcard maps/VegBIEN.*.csv))
|
97 |
772
|
aaronmk
|
tables := $(directMaps:maps/VegBIEN.%.csv=%)
|
98 |
728
|
aaronmk
|
|
99 |
1289
|
aaronmk
|
srcMaps := $(filter-out maps/src.join.%.csv,$(wildcard maps/src.*.csv))
|
100 |
|
|
srcJoinMaps := $(srcMaps:maps/src.%.csv=maps/src.join.%.csv)
|
101 |
|
|
|
102 |
1273
|
aaronmk
|
# Must come before $(root)/% to override it
|
103 |
|
|
$(selfMap): _always
|
104 |
|
|
-+$(subMake)
|
105 |
|
|
# ignore errors if $(selfMap) does not exist
|
106 |
|
|
|
107 |
|
|
$(root)/%: _always
|
108 |
|
|
+$(subMake)
|
109 |
|
|
|
110 |
1139
|
aaronmk
|
maps :=
|
111 |
728
|
aaronmk
|
|
112 |
775
|
aaronmk
|
maps/VegBIEN.%.csv: maps/$(via).%.csv $(coreMap)
|
113 |
1289
|
aaronmk
|
$(join) <$+ >$@
|
114 |
1139
|
aaronmk
|
maps += $(autogenMaps)
|
115 |
|
|
|
116 |
1273
|
aaronmk
|
makeFullCsv = $(if $(shell test -e $(word 2,$+) && echo t),\
|
117 |
1289
|
aaronmk
|
env ignore=1 $(bin)/union <$+|$(bin)/sort_map >$@,$(CP) $< $@)
|
118 |
|
|
# can't use $(wildcard) because it won't recheck file after $(selfMap) is run
|
119 |
1273
|
aaronmk
|
|
120 |
1139
|
aaronmk
|
maps/$(via).%.full.csv: maps/$(via).%.csv $(selfMap)
|
121 |
1273
|
aaronmk
|
$(makeFullCsv)
|
122 |
1139
|
aaronmk
|
maps += $(patsubst maps/%.csv,maps/%.full.csv,$(viaMaps))
|
123 |
|
|
|
124 |
1289
|
aaronmk
|
maps/src.join.%.csv: maps/src.%.csv maps/$(via).%.full.csv $(noEmptyMap)
|
125 |
|
|
$(bin)/cols 0 0 <$<|$(bin)/join $(word 2,$+)|$(join) $(word 3,$+) >$@
|
126 |
|
|
maps += $(srcJoinMaps)
|
127 |
|
|
|
128 |
1139
|
aaronmk
|
maps: $(maps) _always ;
|
129 |
|
|
|
130 |
|
|
all += $(maps)
|
131 |
|
|
|
132 |
783
|
aaronmk
|
##### Mapping
|
133 |
368
|
aaronmk
|
|
134 |
1378
|
aaronmk
|
dbExport := $(firstword $(wildcard src/db.*.sql))
|
135 |
1385
|
aaronmk
|
inputFiles := $(wildcard $(exts:%=src/*.%))
|
136 |
728
|
aaronmk
|
|
137 |
775
|
aaronmk
|
+maps = $(filter maps/% $(mappings)/%,$(+_))
|
138 |
|
|
<in = $(firstword $(filter-out $(+maps),$(+_)) $(wildcard $(exts:%=src/*.$*.%)))
|
139 |
1386
|
aaronmk
|
inputs = $(strip $(wildcard $(<in).header) $(<in))
|
140 |
1382
|
aaronmk
|
map = $(if $(inputs),$(bin)/with_cat $(inputs) -- ,$(if $(mapEnv),env $(mapEnv)\
|
141 |
1386
|
aaronmk
|
,$(error No input file src/*.$*.{$(exts)})))$(root)/map $(+maps)
|
142 |
772
|
aaronmk
|
map2db = env out_database=vegbien $(map)
|
143 |
368
|
aaronmk
|
|
144 |
783
|
aaronmk
|
##### Import to VegBIEN
|
145 |
772
|
aaronmk
|
|
146 |
1378
|
aaronmk
|
ifneq ($(dbExport)$(inputFiles),)
|
147 |
772
|
aaronmk
|
|
148 |
1190
|
aaronmk
|
log_ = $(@:-all=)$(if $(n),.n=$(n),).$(date).log
|
149 |
|
|
trace = $(log_:.log=.trace)
|
150 |
1188
|
aaronmk
|
import = -(set -x; "time" env commit=1 verbose=1\
|
151 |
1190
|
aaronmk
|
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
|
152 |
|
|
$(if $(n),,>>$(log_))) 2>&1$(if $(log),$(if $(n),|tee -a $(log_)))
|
153 |
1088
|
aaronmk
|
# don't abort on import errors, which often relate to invalid input data
|
154 |
624
|
aaronmk
|
|
155 |
1193
|
aaronmk
|
import: $(addprefix import-,$(tables)) _always ;
|
156 |
718
|
aaronmk
|
|
157 |
|
|
import-%: maps/VegBIEN.%.csv _always
|
158 |
|
|
$(import)
|
159 |
772
|
aaronmk
|
# default:
|
160 |
|
|
import-%: _always ;
|
161 |
718
|
aaronmk
|
|
162 |
772
|
aaronmk
|
else
|
163 |
|
|
import: _always ;
|
164 |
|
|
endif
|
165 |
|
|
|
166 |
790
|
aaronmk
|
##### Log files from import
|
167 |
|
|
|
168 |
1188
|
aaronmk
|
logs := $(wildcard *.log *.trace)
|
169 |
339
|
aaronmk
|
|
170 |
383
|
aaronmk
|
rm_logs: _always
|
171 |
339
|
aaronmk
|
$(RM) $(logs)
|
172 |
|
|
|
173 |
783
|
aaronmk
|
##### Verification of import
|
174 |
264
|
aaronmk
|
|
175 |
1194
|
aaronmk
|
verify: $(addprefix verify-,$(tables)) _always ;
|
176 |
|
|
|
177 |
1199
|
aaronmk
|
verify-%: verify/%.ref verify/%.out _always
|
178 |
1184
|
aaronmk
|
-$(diffVerbose) $(+_)
|
179 |
1088
|
aaronmk
|
# don't abort on verification errors, which are expected during development
|
180 |
1194
|
aaronmk
|
# default:
|
181 |
1199
|
aaronmk
|
verify-%: verify/%.out _always
|
182 |
1200
|
aaronmk
|
$(if $(shell test -e $< && echo t),cat $<)
|
183 |
1199
|
aaronmk
|
# don't run if verify/%.out's default do-nothing action was used
|
184 |
1200
|
aaronmk
|
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
|
185 |
368
|
aaronmk
|
|
186 |
1245
|
aaronmk
|
define verify
|
187 |
|
|
$(mkdir)
|
188 |
1301
|
aaronmk
|
"time" $(psqlAsBien) --set=datasource="'$(datasrc)'" --no-align\
|
189 |
1245
|
aaronmk
|
--field-separator=$$'\t' --pset=footer=off --pset=null=NULL <$< >$@
|
190 |
|
|
endef
|
191 |
514
|
aaronmk
|
|
192 |
1199
|
aaronmk
|
verify/%.out: $(mappings)/verify.%.sql _always
|
193 |
1192
|
aaronmk
|
$(verify)
|
194 |
1194
|
aaronmk
|
# default:
|
195 |
1199
|
aaronmk
|
verify/%.out: _always ;
|
196 |
369
|
aaronmk
|
|
197 |
1199
|
aaronmk
|
all += $(wildcard verify/*.out)
|
198 |
1192
|
aaronmk
|
|
199 |
1378
|
aaronmk
|
ifneq ($(dbExport),)
|
200 |
1082
|
aaronmk
|
%.ref: %.ref.sql
|
201 |
|
|
$(dbAsBien) $(db) <$< >$@
|
202 |
|
|
endif
|
203 |
|
|
|
204 |
783
|
aaronmk
|
##### Testing
|
205 |
368
|
aaronmk
|
|
206 |
624
|
aaronmk
|
ifneq ($(wildcard test/),)
|
207 |
|
|
|
208 |
723
|
aaronmk
|
hasOwnRef = $(filter-out %.2-step.xml,$@)
|
209 |
630
|
aaronmk
|
testRef = $(1:.2-step.xml=.xml).ref
|
210 |
|
|
|
211 |
897
|
aaronmk
|
define runTest
|
212 |
776
|
aaronmk
|
@echo "Testing $(abspath $@)..."
|
213 |
991
|
aaronmk
|
>$@ env test=1 n=$(test_n) $(1)
|
214 |
1184
|
aaronmk
|
@(set -x; $(diff) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
|
215 |
780
|
aaronmk
|
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
|
216 |
777
|
aaronmk
|
$(if $(hasOwnRef),\
|
217 |
|
|
echo $(emph)"To accept new test output:"$(endEmph);\
|
218 |
|
|
echo "$(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile";\
|
219 |
|
|
,\
|
220 |
|
|
echo $(emph)"Note: The preceding failed test is compared to another test's\
|
221 |
|
|
output"$(endEmph);\
|
222 |
|
|
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
|
223 |
|
|
)\
|
224 |
652
|
aaronmk
|
exit $$e;}
|
225 |
628
|
aaronmk
|
endef
|
226 |
|
|
|
227 |
897
|
aaronmk
|
test2File = $(call runTest,$(map))
|
228 |
775
|
aaronmk
|
|
229 |
876
|
aaronmk
|
tests :=
|
230 |
|
|
|
231 |
1139
|
aaronmk
|
test/$(via).%.xml: maps/$(via).%.full.csv _always
|
232 |
775
|
aaronmk
|
$(test2File)
|
233 |
876
|
aaronmk
|
tests += test/$(via).%.xml
|
234 |
629
|
aaronmk
|
|
235 |
876
|
aaronmk
|
test/VegBIEN.%.xml: maps/VegBIEN.%.csv _always
|
236 |
775
|
aaronmk
|
$(test2File)
|
237 |
876
|
aaronmk
|
tests += test/VegBIEN.%.xml
|
238 |
628
|
aaronmk
|
|
239 |
884
|
aaronmk
|
test/VegBIEN.%.2-step.xml: test/$(via).%.xml $(coreMap) _always
|
240 |
775
|
aaronmk
|
-$(test2File)
|
241 |
722
|
aaronmk
|
# Don't abort tester if only 2-step test fails, as it's often finicky
|
242 |
876
|
aaronmk
|
tests += test/VegBIEN.%.2-step.xml
|
243 |
630
|
aaronmk
|
|
244 |
876
|
aaronmk
|
test/import.%.out: maps/VegBIEN.%.csv _always
|
245 |
897
|
aaronmk
|
$(call runTest,$(map2db))
|
246 |
876
|
aaronmk
|
tests += test/import.%.out
|
247 |
627
|
aaronmk
|
|
248 |
876
|
aaronmk
|
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
|
249 |
724
|
aaronmk
|
|
250 |
1361
|
aaronmk
|
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
|
251 |
|
|
|
252 |
876
|
aaronmk
|
test: _always $(testOutputs) ;
|
253 |
|
|
|
254 |
|
|
all += $(testOutputs)
|
255 |
|
|
|
256 |
634
|
aaronmk
|
# Accepts a test output: make <test_output_path>-ok
|
257 |
626
|
aaronmk
|
%-ok: _always
|
258 |
630
|
aaronmk
|
$(CP) $* $(call testRef,$*)
|
259 |
502
|
aaronmk
|
|
260 |
624
|
aaronmk
|
else
|
261 |
|
|
test: _always ;
|
262 |
|
|
endif
|
263 |
|
|
|
264 |
783
|
aaronmk
|
##### Input-type-specific
|
265 |
368
|
aaronmk
|
|
266 |
624
|
aaronmk
|
# Each input type needs var $(mapEnv) and targets install, uninstall
|
267 |
|
|
|
268 |
1379
|
aaronmk
|
#### DB export
|
269 |
|
|
|
270 |
1378
|
aaronmk
|
ifneq ($(dbExport),)
|
271 |
256
|
aaronmk
|
|
272 |
1378
|
aaronmk
|
dbEngineExt := $(subst .,,$(suffix $(basename $(notdir $(dbExport)))))
|
273 |
1196
|
aaronmk
|
db := $(datasrc)
|
274 |
622
|
aaronmk
|
|
275 |
1379
|
aaronmk
|
### Installation
|
276 |
256
|
aaronmk
|
|
277 |
621
|
aaronmk
|
install: _always db ;
|
278 |
339
|
aaronmk
|
|
279 |
621
|
aaronmk
|
uninstall: _always rm_db ;
|
280 |
368
|
aaronmk
|
|
281 |
1379
|
aaronmk
|
### DB-engine-specific
|
282 |
621
|
aaronmk
|
|
283 |
624
|
aaronmk
|
# Each DB engine needs vars $(dbEngine), $(dbAsBien) and targets db, rm_db
|
284 |
256
|
aaronmk
|
|
285 |
622
|
aaronmk
|
ifeq ($(dbEngineExt),my)
|
286 |
621
|
aaronmk
|
|
287 |
622
|
aaronmk
|
dbEngine := MySQL
|
288 |
|
|
|
289 |
621
|
aaronmk
|
bienPassword := $(shell cat $(root)/config/bien_password)
|
290 |
|
|
mysqlAs = mysql --user=$(1) --password='$(bienPassword)'
|
291 |
|
|
mysqlAsRoot := $(call mysqlAs,root)
|
292 |
|
|
dbAsBien := $(call mysqlAs,bien)
|
293 |
|
|
|
294 |
1097
|
aaronmk
|
dbExists = $(shell echo "SHOW DATABASES LIKE '$(db)';"|$(mysqlAsRoot))
|
295 |
|
|
|
296 |
|
|
define createDb
|
297 |
1098
|
aaronmk
|
echo "CREATE DATABASE $(db) DEFAULT CHARACTER SET latin1;"|$(mysqlAsRoot)
|
298 |
1097
|
aaronmk
|
-$(mysqlAsRoot) --database=$(db) <$<
|
299 |
|
|
endef
|
300 |
|
|
# ignore errors in db import so that GRANT will still be run
|
301 |
|
|
|
302 |
1378
|
aaronmk
|
db: $(dbExport) _always
|
303 |
1097
|
aaronmk
|
$(if $(dbExists),,$(createDb))
|
304 |
368
|
aaronmk
|
echo "GRANT SELECT ON $(db).* TO 'bien'@'localhost';"|$(mysqlAsRoot)
|
305 |
250
|
aaronmk
|
|
306 |
383
|
aaronmk
|
rm_db: _always
|
307 |
368
|
aaronmk
|
-echo "REVOKE ALL ON $(db).* FROM 'bien'@'localhost';"|$(mysqlAsRoot)
|
308 |
|
|
echo "DROP DATABASE IF EXISTS $(db);"|$(mysqlAsRoot)
|
309 |
365
|
aaronmk
|
# ignore errors if grant not defined
|
310 |
250
|
aaronmk
|
|
311 |
1379
|
aaronmk
|
## Unrecognized DB engine
|
312 |
368
|
aaronmk
|
|
313 |
|
|
else
|
314 |
1378
|
aaronmk
|
$(error The DB filename $(dbExport) must be db.my.sql)
|
315 |
368
|
aaronmk
|
endif
|
316 |
|
|
|
317 |
1379
|
aaronmk
|
### Other input types
|
318 |
622
|
aaronmk
|
|
319 |
1379
|
aaronmk
|
else
|
320 |
624
|
aaronmk
|
|
321 |
|
|
install: _always ;
|
322 |
|
|
uninstall: _always ;
|
323 |
1379
|
aaronmk
|
|
324 |
256
|
aaronmk
|
endif
|
325 |
1379
|
aaronmk
|
|
326 |
|
|
#### DB connection info
|
327 |
|
|
|
328 |
|
|
ifneq ($(dbEngine),)
|
329 |
|
|
# Must come after dbEngine is set
|
330 |
|
|
mapEnv := in_engine=$(dbEngine) in_database=$(db)
|
331 |
|
|
endif
|