Project

General

Profile

1 3761 aaronmk
selfDir_uZPPqC := $(dir $(lastword $(MAKEFILE_LIST)))
2
root := $(selfDir_uZPPqC)..
3
include $(root)/lib/common.Makefile
4
5
6 783 aaronmk
##### Configuration
7
8 1921 aaronmk
# Command line
9 1190 aaronmk
log ?= $(if $(test),,1)
10 1257 aaronmk
profile ?=
11 1983 aaronmk
quiet ?=
12 1620 aaronmk
reverify ?= 1
13 1990 aaronmk
use_staged ?= $(by_col)
14 1921 aaronmk
15
# Makefile
16 1386 aaronmk
exts ?= csv tsv txt xml
17 783 aaronmk
test_n ?= 2
18
19
##### Vars/functions
20
21 1821 aaronmk
# Paths
22
datasrc := $(notdir $(realpath .))
23
bin := $(root)/bin
24
mappings := $(root)/mappings
25
26 368 aaronmk
# Make
27 640 aaronmk
SHELL := /bin/bash
28 1509 aaronmk
selfMake = $(MAKE) --makefile=../input.Makefile
29 1821 aaronmk
subMake = $(MAKE) $(@:$(root)/%=%) --directory=$(root)
30 404 aaronmk
+_ = $(+:_%=)
31 368 aaronmk
addBeforeExt = $(basename $(2))$(1)$(suffix $(2))
32
33 1742 aaronmk
# OS
34
os := $(shell uname)
35
forOs = $(patsubst %,%-$(filter Linux Darwin,$(os)),$(1))
36
37
# Formatting
38
SED = sed -$(if $(filter Darwin,$(os)),E,r)
39
40 790 aaronmk
# System
41
date = $(shell date +"%Y-%m-%d-%H-%M-%S")
42
43 640 aaronmk
# Terminal
44 1184 aaronmk
termCols := $(shell tput cols)
45 640 aaronmk
esc := '['
46
reset := $(esc)'0m'
47
emph := $(esc)'7m '
48
endEmph := ' '$(reset)
49
50 368 aaronmk
# Commands
51 1245 aaronmk
MKDIR = mkdir -p
52
mkdir = $(MKDIR) $(@D)
53 395 aaronmk
CP = cp -p
54 1184 aaronmk
diff = diff --unified=2
55 3721 aaronmk
diffIgnoreSpace = $(diff) --ignore-space-change
56 1184 aaronmk
diffVerbose = $(if $(verbose),diff --side-by-side --left-column\
57
--width=$(termCols),$(diff))
58 368 aaronmk
59 1918 aaronmk
# BIEN commands
60 4137 aaronmk
sortFilenames = $(shell $(bin)/sort_filenames $(1))
61 1524 aaronmk
selfMap = $(bin)/cols 0 0
62 1081 aaronmk
psqlOpts := --set ON_ERROR_STOP=1 --quiet
63 1289 aaronmk
psqlAsBien := $(bin)/psql_vegbien $(psqlOpts)
64 353 aaronmk
65 1594 aaronmk
# SVN
66
addDir = $(if $(wildcard $(1)/),svn add --depth=empty $(1),svn mkdir $(1))
67 1771 aaronmk
setSvnIgnore = svn propset svn:ignore $(2) $(1)
68 1594 aaronmk
define addDirWithIgnore
69
$(addDir)
70 1771 aaronmk
$(setSvnIgnore)
71 1594 aaronmk
endef
72
73 783 aaronmk
##### General targets
74 250 aaronmk
75 418 aaronmk
all: _always maps ;
76 247 aaronmk
77 383 aaronmk
clean: _always
78 256 aaronmk
	$(RM) $(all)
79
80 1742 aaronmk
remake: _always clean
81
	+$(selfMake)
82
# re-run make so that cache of existing files is reset
83
84 1627 aaronmk
make_script = ./$< >$@
85 368 aaronmk
86 1604 aaronmk
%/: % _always ;
87
88 1627 aaronmk
# Must come before `%: %.make` to override it
89
src/%: src/%.make _always
90 3317 aaronmk
	(set -x; $(make_script)) 2>>$<.log.sql
91 1627 aaronmk
.PRECIOUS: src/% # save partial outputs of aborted src make scripts
92
93
%: %.make _always
94
	$(make_script)
95
96 783 aaronmk
##### SVN
97 250 aaronmk
98 1594 aaronmk
add: _always
99 4124 aaronmk
	$(call addDirWithIgnore,src,'*')
100
	$(call addDirWithIgnore,src/specimens,'*')
101
	$(call addDirWithIgnore,src/specimens/logs,$$'*.log.sql\n*.trace')
102
	$(call addDirWithIgnore,verify,'*.out')
103 766 aaronmk
104 1969 aaronmk
##### Existing maps discovery
105 1955 aaronmk
106 4181 aaronmk
sortFile := src/import_order.txt
107
108
ifneq ($(wildcard $(sortFile)),)
109
tables := $(shell cat $(sortFile))# $(shell) replaces "\n" with " "
110
else
111 4137 aaronmk
allSubdirs := $(call wildcard/,src/*/)
112
tables := $(call sortFilenames,$(filter-out _%,$(allSubdirs:src/%/=%)))
113 4181 aaronmk
endif
114 4137 aaronmk
115 4118 aaronmk
srcMaps := $(wildcard src/*/src.csv)
116 1955 aaronmk
117 4118 aaronmk
srcMap := src/%/src.csv
118
viaMap := src/%/map.csv
119
fullViaMap := src/%/map.full.csv
120
directMap := src/%/VegBIEN.csv
121
anyMap := $(srcMap) $(viaMap) $(fullViaMap) $(directMap)
122 3574 aaronmk
123 4120 aaronmk
anyTest = src/$*/test.%
124 4122 aaronmk
srcsOnly = $(filter-out $(anyMap) $(anyTest) src/%/logs,$(1))
125 4120 aaronmk
126 4095 aaronmk
via := Veg+
127 783 aaronmk
128 4098 aaronmk
coreMap := $(mappings)/$(via)-VegBIEN.csv
129
coreSelfMap := $(mappings)/$(via).self.csv
130 783 aaronmk
131 4137 aaronmk
viaMaps := $(wildcard $(tables:%=src/%/map.csv))
132 4118 aaronmk
viaMaps += $(filter-out $(viaMaps),$(srcMaps:src/%/src.csv=src/%/map.csv))
133
viaMaps += $(filter-out $(viaMaps) $(fullViaMap),$(wildcard src/*/map.csv))
134 783 aaronmk
135 4117 aaronmk
autogenMaps := $(subst map.,VegBIEN.,$(viaMaps))
136 728 aaronmk
directMaps := $(autogenMaps) $(filter-out $(autogenMaps),\
137 4118 aaronmk
$(wildcard src/*/VegBIEN.csv))
138 728 aaronmk
139 1969 aaronmk
##### Sources
140
141 4137 aaronmk
srcs = $(call sortFilenames,$(call srcsOnly,$(wildcard src/$*/*)))
142 1969 aaronmk
isCsv = $(if $(srcs),$(if $(filter %.xml,$(srcs)),,1))
143
    # true if $(srcs) non-empty and contains no *.xml
144
catSrcs = $(bin)/cat$(if $(isCsv),_csv) $(srcs)
145
withCatSrcs = $(catSrcs:$(bin)/%=$(bin)/with_%) --
146
147 1986 aaronmk
# Usage: `make {--silent|-s} inputs/<datasrc>/cat` (don't echo make commands)
148 1969 aaronmk
cat: $(addprefix cat-,$(tables)) _always ;
149
150
cat-%: _always
151
	$(catSrcs)
152
153 4121 aaronmk
##### Staging tables installation
154 1921 aaronmk
155 4121 aaronmk
install: _always schema $(addprefix install-,$(tables)) ;
156 1921 aaronmk
157 4121 aaronmk
uninstall: _always rm_schema ;
158
# rm_schema will also drop all staging tables
159 1921 aaronmk
160
reinstall: _always uninstall install ;
161
162 4121 aaronmk
schema: _always
163 1921 aaronmk
	-echo 'CREATE SCHEMA "$(datasrc)";'|$(psqlAsBien)
164
# ignore errors if schema exists
165
166 4121 aaronmk
rm_schema: _always
167 1921 aaronmk
	echo 'DROP SCHEMA IF EXISTS "$(datasrc)" CASCADE;'|$(psqlAsBien)
168
169 4121 aaronmk
install-%: _always
170 1955 aaronmk
	$(if $(isCsv),$(import_install_))
171 3476 aaronmk
import_install_ = (prefix=; . $(bin)/vegbien_dest; "time" nice -n +5\
172
env schema=$(datasrc) table=$* $(bin)/csv2db $(catSrcs)\
173 4122 aaronmk
$(if $(log),$(if $(quiet),&>,2>&1|tee )src/$*/logs/install.log.sql))
174 1921 aaronmk
175 1777 aaronmk
##### Maps building
176
177 1789 aaronmk
# Maps to (try to) build are added to one of these
178 1779 aaronmk
maps :=
179 1789 aaronmk
createOnlyMaps :=
180 1779 aaronmk
181 3569 aaronmk
srcRoot = $(mappings)/roots/$*.sh
182
mkSrcMap = $(catSrcs)|(. $(srcRoot); env datasrc=$(datasrc) $(bin)/src_map >$@)
183 1779 aaronmk
184 3567 aaronmk
# Src maps cleanup
185 4134 aaronmk
ifneq ($(filter src/%/.src.csv.last_cleanup,$(MAKECMDGOALS)),)
186 4118 aaronmk
src/%/.src.csv.last_cleanup: src/%/src.csv
187 3814 aaronmk
	$(bin)/in_place $< $(bin)/cols +
188 3567 aaronmk
	touch $@
189
else
190 1779 aaronmk
# Autogen src maps with known table names
191 4118 aaronmk
src/%/src.csv: _always
192 3567 aaronmk
	$(if $(wildcard $@),,$(if $(isCsv),$(mkSrcMap)))
193 4131 aaronmk
	+$(if $(isCsv),$(selfMake) $(@:src/%/src.csv=src/%/.src.csv.last_cleanup))
194 3567 aaronmk
# only build if doesn't exist
195 1779 aaronmk
# only build if CSV srcs exist for that table name
196 3567 aaronmk
endif
197
198 4137 aaronmk
createOnlyMaps += $(tables:%=src/%/src.csv)
199 1779 aaronmk
200 1273 aaronmk
# Must come before $(root)/% to override it
201 1524 aaronmk
$(coreSelfMap): _always
202 1273 aaronmk
	-+$(subMake)
203 1524 aaronmk
# ignore errors if $(coreSelfMap) does not exist
204 1273 aaronmk
205 3572 aaronmk
# Via maps cleanup
206 4118 aaronmk
ifneq ($(filter src/%/.map.csv.last_cleanup,$(MAKECMDGOALS)),)
207
src/%/.map.csv.last_cleanup: src/%/map.csv $(coreMap)
208 3572 aaronmk
	$(bin)/in_place $< env ignore=1 $(bin)/subtract $(word 2,$+) 0 1 2
209
	touch $@
210
# Include comment column so commented mappings are never removed
211
else
212 4118 aaronmk
src/%/map.csv: src/%/src.csv _always
213 3574 aaronmk
	$(if $(wildcard $@),,$(if $(wildcard $<),$(CP) $< $@))
214 4118 aaronmk
	+$(selfMake) $(@:src/%/map.csv=src/%/.map.csv.last_cleanup)
215 3572 aaronmk
endif
216
217 4135 aaronmk
srcMap* = src/$*/src.csv
218
joinSrcMap = $(if $(hasSrc),$(bin)/in_place $@ $(bin)/intersect $(srcMap*) 0)
219 4133 aaronmk
hasSrc = $(shell test -s $(word 3,$+) && echo t)
220
221
makeFullCsv = env ignore=1 $(bin)/union <$(wordlist 1,2,$+)|$(bin)/sort_map >$@
222
223 4135 aaronmk
src/%/map.full.csv: src/%/map.csv $(coreSelfMap)
224 4133 aaronmk
	$(makeFullCsv)
225
	$(joinSrcMap)
226
maps += $(patsubst src/%/map.csv,src/%/map.full.csv,$(viaMaps))
227
228 4118 aaronmk
src/%/VegBIEN.csv: src/%/map.full.csv $(coreMap)
229 1530 aaronmk
	$(bin)/join <$+|$(bin)/sort_map >$@
230
maps += $(autogenMaps)
231
232 1789 aaronmk
maps: $(createOnlyMaps) $(maps) _always ;
233 1139 aaronmk
234
all += $(maps)
235
236 1742 aaronmk
##### Maps validation
237
238 3764 aaronmk
missingMappingsCmd = +$(selfMake) remake 2>&1\
239 3821 aaronmk
|$(SED) -n 's/^.*No $(*2Space) mapping for (.*)$$/\1/p'|$(SED) 's/\/_.*//'\
240 3764 aaronmk
$(if $(filter non-empty_join,$*),|$(bin)/ucase_first 0)|sort|uniq
241 1742 aaronmk
242 3764 aaronmk
include $(root)/lib/mappings.Makefile
243 1742 aaronmk
244 1509 aaronmk
##### External dependencies
245
246
$(root)/%: _always
247
	+$(subMake)
248 1628 aaronmk
.PRECIOUS: $(root)/% # let ext. dir's Makefile decide whether to delete on error
249 1509 aaronmk
250 783 aaronmk
##### Mapping
251 368 aaronmk
252 4118 aaronmk
+maps = $(filter src/%/map.csv src/%/VegBIEN.csv $(mappings)/%,$(+_))
253 4106 aaronmk
map = $(if $(srcs),$(withCatSrcs) $(root)/map $(+maps),\
254
$(shell echo Warning: No input file src/$*/*.{$(exts)} >&2)false)
255
# need false to run some command, when prefixed by `env ...` below
256 3387 aaronmk
map2db = $(if $(use_staged),env in_database=vegbien in_schema=$(datasrc)\
257
in_table=$*) env out_database=vegbien $(map)
258 368 aaronmk
259 783 aaronmk
##### Import to VegBIEN
260 772 aaronmk
261 1853 aaronmk
profileTest = $(if $(profile),$(if $(test),1))
262
profileOnly = -env profile_to=/dev/fd/3 $(map2db) 3>&1 1>&2|\
263
$(bin)/profile_stats /dev/fd/0
264
265 4122 aaronmk
log_ = src/$*/logs/$(if $(n),n=$(n).,)$(date).log.sql
266 3317 aaronmk
trace = $(log_:.log.sql=.trace)
267 1853 aaronmk
import = -$(if $(profileTest),$(profileOnly),(set -x; "time" env commit=1\
268 1190 aaronmk
$(if $(profile),profile_to=$(trace)) $(map2db)) $(if $(log),\
269 3616 aaronmk
$(if $(n),,&>$(log_)))$(if $(log),$(if $(n), 2>&1|tee -a $(log_))))
270 1088 aaronmk
# don't abort on import errors, which often relate to invalid input data
271 624 aaronmk
272 1193 aaronmk
import: $(addprefix import-,$(tables)) _always ;
273 718 aaronmk
274 4118 aaronmk
import-%: src/%/VegBIEN.csv _always
275 718 aaronmk
	$(import)
276 772 aaronmk
# default:
277
import-%: _always ;
278 718 aaronmk
279 790 aaronmk
##### Log files from import
280
281 4122 aaronmk
logs := $(wildcard src/*/logs/*.log.sql src/*/logs/*.trace)
282 339 aaronmk
283 383 aaronmk
rm_logs: _always
284 339 aaronmk
	$(RM) $(logs)
285
286 783 aaronmk
##### Verification of import
287 264 aaronmk
288 1194 aaronmk
verify: $(addprefix verify-,$(tables)) _always ;
289
290 1199 aaronmk
verify-%: verify/%.ref verify/%.out _always
291 1184 aaronmk
	-$(diffVerbose) $(+_)
292 1088 aaronmk
# don't abort on verification errors, which are expected during development
293 1194 aaronmk
# default:
294 1199 aaronmk
verify-%: verify/%.out _always
295 1200 aaronmk
	$(if $(shell test -e $< && echo t),cat $<)
296 1199 aaronmk
# don't run if verify/%.out's default do-nothing action was used
297 1200 aaronmk
# can't use $(wildcard) because it won't recheck file after verify/%.out is run
298 368 aaronmk
299 1620 aaronmk
verify = $(if $(reverify),"time" $(psqlAsBien) --set=datasource="'$(datasrc)'"\
300
--no-align --field-separator=$$'\t' --pset=footer=off --pset=null=NULL <$< >$@)
301 514 aaronmk
302 1199 aaronmk
verify/%.out: $(mappings)/verify.%.sql _always
303 1192 aaronmk
	$(verify)
304 1194 aaronmk
# default:
305 1199 aaronmk
verify/%.out: _always ;
306 369 aaronmk
307 1199 aaronmk
all += $(wildcard verify/*.out)
308 1192 aaronmk
309 1667 aaronmk
##### Editing import
310
311 4121 aaronmk
rotate: _always
312 1668 aaronmk
	echo "UPDATE party SET organizationname = organizationname||'.$(date)'\
313 1854 aaronmk
WHERE organizationname = '$(datasrc)';"|$(psqlAsBien)
314 1667 aaronmk
315 4121 aaronmk
rm: _always
316 1854 aaronmk
	echo "DELETE FROM party WHERE organizationname = '$(datasrc)';"|\
317
$(psqlAsBien)
318 1667 aaronmk
319 783 aaronmk
##### Testing
320 368 aaronmk
321 1986 aaronmk
testRefOutput = $(subst .2-step,,$(subst .staging,,$(1)))
322
testRef = $(testRefOutput).ref
323
hasOwnRef = $(filter $@,$(call testRefOutput,$@))
324
# filter returns non-empty if they are equal
325 630 aaronmk
326 897 aaronmk
define runTest
327 776 aaronmk
@echo "Testing $(abspath $@)..."
328 991 aaronmk
>$@ env test=1 n=$(test_n) $(1)
329 3721 aaronmk
@(set -x; $(diffIgnoreSpace) $(call testRef,$@) $@) 2>&1 || { e=$$?;\
330 780 aaronmk
$(if $(wildcard $(call testRef,$@)),,cat $@;)\
331 777 aaronmk
$(if $(hasOwnRef),\
332 3179 aaronmk
{\
333
read -p $(emph)'Accept new test output? (y/n)'$(endEmph) REPLY;\
334
if test "$$REPLY" = y; then\
335
(set -x; $(MAKE) $@-ok --directory=$(realpath .) --makefile=../input.Makefile);\
336
exit 0;\
337
fi;\
338
};,\
339 777 aaronmk
echo $(emph)"Note: The preceding failed test is compared to another test's\
340
output"$(endEmph);\
341
echo $(emph)"When it fails, this always indicates a bug"$(endEmph);\
342
)\
343 652 aaronmk
exit $$e;}
344 628 aaronmk
endef
345
346 1986 aaronmk
test2Db = $(call runTest,$(map2db))
347 775 aaronmk
348 876 aaronmk
tests :=
349
350 4120 aaronmk
src/%/test.xml: src/%/VegBIEN.csv _always
351 1986 aaronmk
	$(test2Db)
352 4120 aaronmk
tests += src/%/test.xml
353 627 aaronmk
354 1987 aaronmk
testStaged2Db = $(foreach use_staged,1,$(test2Db))
355
    # run with use_staged=1
356 1986 aaronmk
357
# Requires staging tables. To create them, run `make inputs/<datasrc>/install`.
358 4120 aaronmk
src/%/test.staging.xml: src/%/VegBIEN.csv _always
359 3642 aaronmk
	-$(if $(isCsv),$(testStaged2Db),cp -p $(@:.staging.xml=.xml) $@)
360 1988 aaronmk
# Don't abort tester if only staging test fails, in case staging table missing
361
# Non-flat-file inputs fall back to mimicking a successful test
362 4120 aaronmk
tests += src/%/test.staging.xml
363 1986 aaronmk
364 876 aaronmk
testOutputs := $(foreach test,$(tests),$(tables:%=$(test)))
365 724 aaronmk
366 1361 aaronmk
.PRECIOUS: $(testOutputs) # save outputs of failed tests so they can be accepted
367
368 876 aaronmk
test: _always $(testOutputs) ;
369
370 4120 aaronmk
all += $(wildcard src/%/test*.xml)
371 876 aaronmk
372 634 aaronmk
# Accepts a test output: make <test_output_path>-ok
373 626 aaronmk
%-ok: _always
374 630 aaronmk
	$(CP) $* $(call testRef,$*)
375 502 aaronmk
376 4120 aaronmk
accept-all: _always
377 3579 aaronmk
	+yes|$(selfMake) test
378
379 3133 aaronmk
##### Documentation
380
381 4122 aaronmk
steps = $(selfMake) -s import-$* test=1 by_col=1 verbosity=2 n=100\
382 3385 aaronmk
2>&1|$(bin)/debug2redmine >$@
383 3188 aaronmk
384 4122 aaronmk
src/%/logs/steps.by_col.log.sql: _always
385 3188 aaronmk
	+$(steps)