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