-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli-common-fetch-dataset.sh
More file actions
61 lines (46 loc) · 1.44 KB
/
cli-common-fetch-dataset.sh
File metadata and controls
61 lines (46 loc) · 1.44 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
#!/usr/bin/env bash
set -euo pipefail
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
source $DIR/cli-common.sh
PYTHON_PATH=$REPO_PATH/tools/ckan-utils/.venv/bin/python3
DATASET="${1:?Usage: $0 <dataset>}"
PREFIX="${2:-}"
fetch_sh=($PYTHON_PATH $REPO_PATH/tools/ckan-utils/fetch_package_cli.py --package_id $DATASET)
if [ -n "$PREFIX" ]; then
fetch_sh+=(--resource_title "$PREFIX" --partial_match)
fi
echo "Running: ${fetch_sh[*]}"
# run it
"${fetch_sh[@]}"
if [ -n "$PREFIX" ]; then
latest_file_pattern="${PREFIX}*"
else
latest_file_pattern="*"
fi
if stat --version >/dev/null 2>&1; then
# Linux / GNU stat
stat_cmd="stat -c '%Y %n'"
else
# macOS / BSD stat
stat_cmd="stat -f '%m %N'"
fi
latest_file="$(
find $REPO_PATH/data/opentransportdata.swiss/$DATASET -type f -name "$latest_file_pattern" -exec bash -c "$stat_cmd \"\$1\"" _ {} \; \
| sort -nr | head -n1 | cut -d ' ' -f2-
)"
if [[ -z "$latest_file" || ! -f "$latest_file" ]]; then
echo "ERROR: no file found for package '$DATASET', prefix '$PREFIX'" >&2
exit 1
fi
file_dst_folder="$DIR/site/$DATASET"
mkdir -p $file_dst_folder
if [ -n "$PREFIX" ]; then
file_dst_extension="${latest_file##*.}"
file_dst_path="$file_dst_folder/${PREFIX}_LATEST.$file_dst_extension"
else
file_dst_file=$(basename "$latest_file")
file_dst_path="$file_dst_folder/$file_dst_file"
fi
cp $latest_file $file_dst_path
echo "copied $latest_file"
echo " to $file_dst_path"