-
Notifications
You must be signed in to change notification settings - Fork 1
/
cp_appro_to_loshrdf
executable file
·206 lines (176 loc) · 4.96 KB
/
cp_appro_to_loshrdf
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2022 Robin Vobruba <[email protected]>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
# See the output of "$0 -h" for details.
# Exit immediately on each error and unset variable;
# see: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -Eeuo pipefail
#set -Eeu
script_dir=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
script_name="$(basename "$0")"
APP_NAME="appro2loshrdf"
SRC_ROOT=target/okh_v2
DST_ROOT=target/losh-crawled/RDF
#DST_ROOT=target/losh-crawled/RDF/appropedia.org
SITE_APPROPEDIA="appropedia.org"
SITE_FIELD_READY="field-ready-projects.openknowhow.org"
SITE_GITLAB_COM="gitlab.com"
SITE_GITHUB="github.com"
SITES=(
"$SITE_APPROPEDIA"
"$SITE_FIELD_READY"
"$SITE_GITLAB_COM"
"$SITE_GITHUB"
)
clean=false
force=false
#gitlab_com____openflexure____openflexure-simple-illumination____-____raw____master____okh.toml
#gitlab_com____openflexure____openflexure-simple-illumination____-____raw____master____okh.ttl
#raw_githubusercontent_com____7B-Things____braille-signage-generator____main____okh.toml
#raw_githubusercontent_com____7B-Things____braille-signage-generator____main____okh.ttl
#field-ready-projects_openknowhow_org____manifests____okh-WA016.toml
#field-ready-projects_openknowhow_org____manifests____okh-WA016.ttl
function print_help() {
echo -e ""
echo -e "$APP_NAME - Copies appropedia.org fetcher content to the losh-rdf repo."
echo -e ""
echo -e "Usage:"
echo -e "\t$script_name [OPTION...]"
echo -e "Options:"
echo -e "\t-c, --clean"
echo -e "\t\tRemoves previously results of this conversion from the losh-rdf repo,"
echo -e "\t\tif the repo is clean and synced ot the remote, or if --force was supplied"
echo -e "\t-f, --force"
echo -e "\t\tDANGER: This may delete important data! See --clean"
echo -e "\t-h, --help"
echo -e "\t\tPrint this usage help and exit"
echo -e ""
}
function ensure_empty_dir() {
local dir="$1"
mkdir -p "$dir"
if ! rmdir "$dir" 2> /dev/null
then
>&2 echo "Dir '$dir' is not empty. Please empty it before instructing this script to move sutff there."
exit 1
fi
mkdir -p "$dir"
}
function file2site_and_name() {
file_name="$(basename --suffix ".ttl" "$1")"
case "$file_name" in
www_appropedia_org____generateOpenKnowHowManifest_php_title_*)
site="$SITE_APPROPEDIA"
name="${file_name#www_appropedia_org____generateOpenKnowHowManifest_php_title_}"
;;
field-ready-projects_openknowhow_org____manifests____okh-*)
site="$SITE_FIELD_READY"
name="${file_name#field-ready-projects_openknowhow_org____manifests____okh-}"
;;
gitlab_com____*)
site="$SITE_GITLAB_COM"
name="${file_name#gitlab_com____}"
name="${name%____-____raw____*}"
;;
raw_githubusercontent_com____*)
site="$SITE_GITHUB"
name="$(echo "$file_name" | sed -e 's|^raw_githubusercontent_com____\(.*\)____.\+____.\+$|\1|')"
;;
*)
>&2 echo "ERROR: Unknown source site for: '$file_name'"
exit 1
;;
esac
printf "%s '%s'" "$site" "$name"
}
function convert_to_loshrdf() {
for site in "${SITES[@]}"
do
dst_site="$DST_ROOT/$site"
if ! $force
then
printf "" #ensure_empty_dir "$dst_site"
fi
done
for src_ttl in "$SRC_ROOT"/*.ttl
do
site_and_name="$(file2site_and_name "$src_ttl")"
site="${site_and_name% *}"
name="${site_and_name#* \'}"
name="${name%\'}"
dst_site="$DST_ROOT/$site"
src_toml="${src_ttl%ttl}toml"
echo "Project ($site) '$name' ..."
#continue
dst_dir="$dst_site/$name"
mkdir -p "$dst_dir"
cp "$src_ttl" "$dst_dir/project.ttl"
cp "$src_toml" "$dst_dir/project.toml"
done
}
function clean_loshrdf() {
>&2 echo "ERROR: Cleaning is not implemented yet. Please empty the following directories manually, after making sure the current content is backed-up/synced to the git remote."
for site in "${SITES[@]}"
do
>&2 echo "$DST_ROOT/$site"
done
exit 1
if ! $force
then
# TODO Use git-check-repo, but it needs fixing first .. might not b eviable.. missing git-stash check still, and maybe more.
if ! $is_clean
then
>&2 echo "ERROR: You chose to play sounds, but the sound file '$SOUND_INCONSISTENCY' does not exist."
exit 1
fi
fi
for site in "${SITES[@]}"
do
dst_site="$DST_ROOT/$site"
ensure_empty_dir "$dst_site"
done
for src_ttl in "$SRC_ROOT"/*.ttl
do
site_and_name="$(file2site_and_name "$src_ttl")"
site="${site_and_name% *}"
name="${site_and_name#* \'}"
name="${name%\'}"
dst_site="$DST_ROOT/$site"
src_toml="${src_ttl%ttl}toml"
echo "Project ($site) '$name' ..."
continue
dst_dir="$dst_site/$name"
mkdir "$dst_dir"
cp "$src_ttl" "$dst_dir/project.ttl"
cp "$src_toml" "$dst_dir/project.toml"
done
}
# Process command line arguments
while [[ $# -gt 0 ]]
do
arg="$1"
shift # $2 -> $1, $3 -> $2, ...
case "$arg" in
-c|--clean)
clean=true
;;
-f|--force)
force=true
;;
-h|--help)
print_help
exit 0
;;
*) # non-/unknown option
>&2 echo "Unknown flag: '$arg'"
exit 1
;;
esac
done
if $clean
then
clean_loshrdf
else
convert_to_loshrdf
fi