-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollection.mk
More file actions
169 lines (138 loc) · 6.16 KB
/
collection.mk
File metadata and controls
169 lines (138 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
.PHONY: \
collect\
collection\
commit-collection\
clobber-today
ifeq ($(COLLECTION_CONFIG_URL),)
COLLECTION_CONFIG_URL=$(CONFIG_URL)collection/$(COLLECTION_NAME)/
endif
ifeq ($(COLLECTION_DIR),)
COLLECTION_DIR=collection/
endif
ifeq ($(RESOURCE_DIR),)
RESOURCE_DIR=$(COLLECTION_DIR)resource/
endif
ifeq ($(DATASTORE_URL),)
DATASTORE_URL=https://files.planning.data.gov.uk/
endif
ifeq ($(REGENERATE_LOG_OVERRIDE),True)
REFILL_TODAYS_LOGS=false
else
REFILL_TODAYS_LOGS=true
endif
ifeq ($(INCREMENTAL_LOADING_OVERRIDE),)
INCREMENTAL_LOADING_OVERRIDE=false
endif
# data sources
SOURCE_CSV=$(COLLECTION_DIR)source.csv
ENDPOINT_CSV=$(COLLECTION_DIR)endpoint.csv
OLD_RESOURCE_CSV=$(COLLECTION_DIR)old-resource.csv
ifeq ($(COLLECTION_CONFIG_FILES),)
COLLECTION_CONFIG_FILES=\
$(SOURCE_CSV)\
$(ENDPOINT_CSV)\
$(OLD_RESOURCE_CSV)
endif
# collection log
LOG_DIR=$(COLLECTION_DIR)log/
LOG_FILES_TODAY:=$(LOG_DIR)$(shell date +%Y-%m-%d)/
# collection index
COLLECTION_INDEX=\
$(COLLECTION_DIR)/log.csv\
$(COLLECTION_DIR)/resource.csv
# collection URL
ifneq ($(COLLECTION),)
COLLECTION_URL=\
$(DATASTORE_URL)$(COLLECTION)-collection/collection
else
COLLECTION_URL=\
$(DATASTORE_URL)$(REPOSITORY)/collection
endif
init::
ifeq ($(COLLECTION_DATASET_BUCKET_NAME),)
$(eval LOG_STATUS_CODE := $(shell curl -I -o /dev/null -s -w "%{http_code}" '$(COLLECTION_URL)/log.csv'))
$(eval RESOURCE_STATUS_CODE = $(shell curl -I -o /dev/null -s -w "%{http_code}" '$(COLLECTION_URL)/resource.csv'))
@if [ $(LOG_STATUS_CODE) -ne 403 ] && [ $(RESOURCE_STATUS_CODE) -ne 403 ]; then \
echo 'Downloading log.csv and resource.csv from $(COLLECTION_URL)'; \
curl -qfsL '$(COLLECTION_URL)/log.csv' > $(COLLECTION_DIR)log.csv; \
curl -qfsL '$(COLLECTION_URL)/resource.csv' > $(COLLECTION_DIR)resource.csv; \
else \
echo 'Unable to locate log.csv and resource.csv' ;\
fi
else ifeq ($(REGENERATE_LOG_OVERRIDE),True)
echo 'Syncing log files to local';
aws s3 sync s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/$(LOG_DIR) $(LOG_DIR) --only-show-errors;
else
echo 'Downloading log.csv and resource.csv from s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/collection/'
@if aws s3 ls s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/collection/log.csv > /dev/null 2>&1; then \
aws s3 cp s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/collection/log.csv collection/log.csv; \
else \
echo "Could not download log.csv from S3"; \
fi
@if aws s3 ls s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/collection/resource.csv > /dev/null 2>&1; then \
aws s3 cp s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/collection/resource.csv collection/resource.csv; \
else \
echo "Could not download resource.csv from S3"; \
fi
endif
first-pass:: collect
second-pass:: collection
collect:: $(COLLECTION_CONFIG_FILES)
@mkdir -p $(RESOURCE_DIR)
digital-land ${DIGITAL_LAND_OPTS} collect $(ENDPOINT_CSV) --collection-dir $(COLLECTION_DIR) --refill-todays-logs $(REFILL_TODAYS_LOGS)
collection::
digital-land ${DIGITAL_LAND_OPTS} collection-save-csv --collection-dir $(COLLECTION_DIR) --refill-todays-logs $(REFILL_TODAYS_LOGS)
clobber-today::
rm -rf $(LOG_FILES_TODAY) $(COLLECTION_INDEX)
makerules::
curl -qfsL '$(MAKERULES_URL)collection.mk' > makerules/collection.mk
load-resources::
aws s3 sync s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/$(RESOURCE_DIR) $(RESOURCE_DIR) --no-progress
load-logs::
aws s3 sync s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/$(COLLECTION_DIR)log $(COLLECTION_DIR)log --no-progress
detect-new-resources::
aws s3 sync $(RESOURCE_DIR) s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/$(RESOURCE_DIR) --dryrun --size-only | { grep -oP 'resource/\K[a-f0-9]+' || true; } > new_resources.txt
save-resources::
ifeq ($(INCREMENTAL_LOADING_OVERRIDE),True)
aws s3 sync $(RESOURCE_DIR) s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/$(RESOURCE_DIR) --no-progress
else
aws s3 sync $(RESOURCE_DIR) s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/$(RESOURCE_DIR) --size-only --no-progress
endif
save-logs::
aws s3 sync $(COLLECTION_DIR)log s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/$(COLLECTION_DIR)log --no-progress
# if incremental loading is enabled than we still need to copy over the log and resource files
save-collection-log-resource::
aws s3 cp $(COLLECTION_DIR)log.csv s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/$(COLLECTION_DIR) --no-progress
aws s3 cp $(COLLECTION_DIR)resource.csv s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/$(COLLECTION_DIR) --no-progress
save-collection::
aws s3 cp $(COLLECTION_DIR)log.csv s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/$(COLLECTION_DIR) --no-progress
aws s3 cp $(COLLECTION_DIR)resource.csv s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/$(COLLECTION_DIR) --no-progress
aws s3 cp $(COLLECTION_DIR)source.csv s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/$(COLLECTION_DIR) --no-progress
aws s3 cp $(COLLECTION_DIR)endpoint.csv s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/$(COLLECTION_DIR) --no-progress
ifneq ($(wildcard $(COLLECTION_DIR)old-resource.csv),)
aws s3 cp $(COLLECTION_DIR)old-resource.csv s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/$(COLLECTION_DIR) --no-progress
endif
collection/resource/%:
@mkdir -p collection/resource/
ifeq ($(COLLECTION_DATASET_BUCKET_NAME),)
curl -qfsL '$(DATASTORE_URL)$(REPOSITORY)/$(RESOURCE_DIR)$(notdir $@)' > $@
else
aws s3 cp s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/$(RESOURCE_DIR)$(notdir $@) $@ --no-progress
endif
collection/$(COLLECTION)/resource/%:
@mkdir -p collection/$(COLLECTION)/resource
ifeq ($(COLLECTION_DATASET_BUCKET_NAME),)
curl -qfsL '$(DATASTORE_URL)$(REPOSITORY)/$(RESOURCE_DIR)$(notdir $@)' > $@
else
aws s3 cp s3://$(COLLECTION_DATASET_BUCKET_NAME)/$(REPOSITORY)/$(RESOURCE_DIR)$(notdir $@) $@ --no-progress
endif
collection/%.csv:
@mkdir -p $(COLLECTION_DIR)
ifeq ($(COLLECTION_DATASET_BUCKET_NAME),)
curl -qfsL '$(COLLECTION_CONFIG_URL)$(notdir $@)?version=$(shell date +%s)' > $@
else
aws s3 cp s3://$(COLLECTION_DATASET_BUCKET_NAME)/config/$(COLLECTION_DIR)$(COLLECTION_NAME)/$(notdir $@) $@ --no-progress
endif
config:: $(COLLECTION_CONFIG_FILES)
clean::
rm -f $(COLLECTION_CONFIG_FILES)