forked from openedx/openedx-atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atlas
executable file
·616 lines (521 loc) · 18.2 KB
/
atlas
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#!/bin/bash
# getoptions generates code that fails this check, so we need to disable it
# shellcheck disable=SC2004
# Atlas uses semantic versioning (https://semver.org/). To cut a new release, checkout the README:
# - https://github.com/openedx/openedx-atlas#releasing-a-new-version
_ATLAS_VERSION="unreleased" # release.yml GitHub Action will fill in the tag version
# @getoptions
parser_definition() {
_ATLAS_USAGE_HELP="Atlas is a CLI tool that has essentially one command: \`atlas pull\`
Configuration file:
Atlas defaults to using a configuration file named \`atlas.yml\` placed
in the root directory. Configuration file:
pull:
repository: <organization-name>/<repository-name>
branch: <branch-name>
directory: <repo-directory-name>:<local-dir-name> ...
filter: <pattern> ...
Atlas can also use a configuration file in a different path using the \`--config\` flag
after \`atlas\`: \`atlas pull --config config.yml\`.
Atlas can also be used without a configuration file by using the flags below after
\`atlas pull\`.
Positional arguments DIRECTORY MAPPINGS ...
One or more directory map pair separated by a colon (:) e.g. FROM_DIR:TO_DIR.
The first directory (FROM_DIR) represents a directory in the git repository.
The second directory (TO_DIR) represents a local directory to copy files to.
At least one directory pair is required:
$ atlas pull frontend-app-learning/messages:learning-app frontend-lib-test/messages:test-lib
This syntax is inspired by the \`docker --volume from_dir:to_dir\` mounting syntax.
Options:
\`-r\` or \`--repository\`:
slug of the GitHub repository to pull from. Defaults 'openedx/openedx-translations'.
\`-b\` or \`--branch\`:
Branch to pull from. Defaults to 'main'
\`-f\` or \`--filter\`:
A comma-separated (or space-separated) list of patterns match files and sub-directories.
This is mainly useful to filter specific languages to download.
The same filter is applied to all DIRECTORY MAPPINGS arguments.
\`--filter=fr_CA,ar,es_419\` will match both directories named 'es_419' and
files named 'es_419.json' among others
Example:
$ cd frontend-app-learning/src/i18n/messages
$ atlas pull --filter=fr_CA,ar,es_419 \\
translations/frontend-app-learning/src/i18n/messages:frontend-app-learning \\
translations/frontend-component-header/src/i18n/messages:frontend-component-header
Will result in the following tree:
├── frontend-app-learning
│ ├── ar.json
│ ├── es_419.json
│ └── fr_CA.json
└── frontend-component-header
├── ar.json
├── es_419.json
└── fr_CA.json
"
setup REST help:usage -- "${_ATLAS_USAGE_HELP}" ''
msg -- '' 'Commands:'
cmd pull -- "pull"
disp :usage -h --help
disp _ATLAS_VERSION --version
}
parser_definition_pull() {
setup PULL_REST help:usage_pull -- \
"Usage: atlas pull [options...] [directory mappings...]"
msg -- 'Options:'
param CONFIG --config -- "path to alternative atlas.yaml configuration file"
param BRANCH -b --branch -- "A branch of translation files"
param REPOSITORY -r --repository -- "The repository containing translation files"
param FILTER -f --filter -- "List of patterns to select which files and sub-directories to checkout."
flag VERBOSE -v --verbose -- "verbose output to terminal"
flag SILENT -s --silent -- "no output to terminal"
disp :usage_pull -h --help
}
# @end
# @gengetoptions parser -i parser_definition parse
# Generated by getoptions (BEGIN)
# URL: https://github.com/ko1nksm/getoptions (v3.3.0)
REST=''
parse() {
OPTIND=$(($#+1))
while OPTARG= && [ $# -gt 0 ]; do
case $1 in
--?*=*) OPTARG=$1; shift
eval 'set -- "${OPTARG%%\=*}" "${OPTARG#*\=}"' ${1+'"$@"'}
;;
--no-*|--without-*) unset OPTARG ;;
-[h]?*) OPTARG=$1; shift
eval 'set -- "${OPTARG%"${OPTARG#??}"}" -"${OPTARG#??}"' ${1+'"$@"'}
OPTARG= ;;
esac
case $1 in
'-h'|'--help')
usage
exit 0 ;;
'--version')
echo "${_ATLAS_VERSION}"
exit 0 ;;
--)
while [ $# -gt 0 ]; do
REST="${REST} \"\${$(($OPTIND-$#))}\""
shift
done
break ;;
[-]?*) set "unknown" "$1"; break ;;
*)
case $1 in 'pull') ;;
*) set "notcmd" "$1"; break
esac
while [ $# -gt 0 ]; do
REST="${REST} \"\${$(($OPTIND-$#))}\""
shift
done
break ;;
esac
shift
done
[ $# -eq 0 ] && { OPTIND=1; unset OPTARG; return 0; }
case $1 in
unknown) set "Unrecognized option: $2" "$@" ;;
noarg) set "Does not allow an argument: $2" "$@" ;;
required) set "Requires an argument: $2" "$@" ;;
pattern:*) set "Does not match the pattern (${1#*:}): $2" "$@" ;;
notcmd) set "Not a command: $2" "$@" ;;
*) set "Validation error ($1): $2" "$@"
esac
echo "$1" >&2
exit 1
}
usage() {
cat<<'GETOPTIONSHERE'
Atlas is a CLI tool that has essentially one command: `atlas pull`
Configuration file:
Atlas defaults to using a configuration file named `atlas.yml` placed
in the root directory. Configuration file:
pull:
repository: <organization-name>/<repository-name>
branch: <branch-name>
directory: <repo-directory-name>:<local-dir-name> ...
filter: <pattern> ...
Atlas can also use a configuration file in a different path using the `--config` flag
after `atlas`: `atlas pull --config config.yml`.
Atlas can also be used without a configuration file by using the flags below after
`atlas pull`.
Positional arguments DIRECTORY MAPPINGS ...
One or more directory map pair separated by a colon (:) e.g. FROM_DIR:TO_DIR.
The first directory (FROM_DIR) represents a directory in the git repository.
The second directory (TO_DIR) represents a local directory to copy files to.
At least one directory pair is required:
$ atlas pull frontend-app-learning/messages:learning-app frontend-lib-test/messages:test-lib
This syntax is inspired by the `docker --volume from_dir:to_dir` mounting syntax.
Options:
`-r` or `--repository`:
slug of the GitHub repository to pull from. Defaults 'openedx/openedx-translations'.
`-b` or `--branch`:
Branch to pull from. Defaults to 'main'
`-f` or `--filter`:
A comma-separated (or space-separated) list of patterns match files and sub-directories.
This is mainly useful to filter specific languages to download.
The same filter is applied to all DIRECTORY MAPPINGS arguments.
`--filter=fr_CA,ar,es_419` will match both directories named 'es_419' and
files named 'es_419.json' among others
Example:
$ cd frontend-app-learning/src/i18n/messages
$ atlas pull --filter=fr_CA,ar,es_419 \
translations/frontend-app-learning/src/i18n/messages:frontend-app-learning \
translations/frontend-component-header/src/i18n/messages:frontend-component-header
Will result in the following tree:
├── frontend-app-learning
│ ├── ar.json
│ ├── es_419.json
│ └── fr_CA.json
└── frontend-component-header
├── ar.json
├── es_419.json
└── fr_CA.json
Commands:
pull pull
-h, --help
--version
GETOPTIONSHERE
}
# Generated by getoptions (END)
# @end
# @gengetoptions parser -i parser_definition_pull parse_pull
# Generated by getoptions (BEGIN)
# URL: https://github.com/ko1nksm/getoptions (v3.3.0)
CONFIG=''
BRANCH=''
REPOSITORY=''
FILTER=''
VERBOSE=''
SILENT=''
PULL_REST=''
parse_pull() {
OPTIND=$(($#+1))
while OPTARG= && [ $# -gt 0 ]; do
case $1 in
--?*=*) OPTARG=$1; shift
eval 'set -- "${OPTARG%%\=*}" "${OPTARG#*\=}"' ${1+'"$@"'}
;;
--no-*|--without-*) unset OPTARG ;;
-[brf]?*) OPTARG=$1; shift
eval 'set -- "${OPTARG%"${OPTARG#??}"}" "${OPTARG#??}"' ${1+'"$@"'}
;;
-[vsh]?*) OPTARG=$1; shift
eval 'set -- "${OPTARG%"${OPTARG#??}"}" -"${OPTARG#??}"' ${1+'"$@"'}
OPTARG= ;;
esac
case $1 in
'--config')
[ $# -le 1 ] && set "required" "$1" && break
OPTARG=$2
CONFIG="$OPTARG"
shift ;;
'-b'|'--branch')
[ $# -le 1 ] && set "required" "$1" && break
OPTARG=$2
BRANCH="$OPTARG"
shift ;;
'-r'|'--repository')
[ $# -le 1 ] && set "required" "$1" && break
OPTARG=$2
REPOSITORY="$OPTARG"
shift ;;
'-f'|'--filter')
[ $# -le 1 ] && set "required" "$1" && break
OPTARG=$2
FILTER="$OPTARG"
shift ;;
'-v'|'--verbose')
[ "${OPTARG:-}" ] && OPTARG=${OPTARG#*\=} && set "noarg" "$1" && break
eval '[ ${OPTARG+x} ] &&:' && OPTARG='1' || OPTARG=''
VERBOSE="$OPTARG"
;;
'-s'|'--silent')
[ "${OPTARG:-}" ] && OPTARG=${OPTARG#*\=} && set "noarg" "$1" && break
eval '[ ${OPTARG+x} ] &&:' && OPTARG='1' || OPTARG=''
SILENT="$OPTARG"
;;
'-h'|'--help')
usage_pull
exit 0 ;;
--)
shift
while [ $# -gt 0 ]; do
PULL_REST="${PULL_REST} \"\${$(($OPTIND-$#))}\""
shift
done
break ;;
[-]?*) set "unknown" "$1"; break ;;
*)
PULL_REST="${PULL_REST} \"\${$(($OPTIND-$#))}\""
esac
shift
done
[ $# -eq 0 ] && { OPTIND=1; unset OPTARG; return 0; }
case $1 in
unknown) set "Unrecognized option: $2" "$@" ;;
noarg) set "Does not allow an argument: $2" "$@" ;;
required) set "Requires an argument: $2" "$@" ;;
pattern:*) set "Does not match the pattern (${1#*:}): $2" "$@" ;;
notcmd) set "Not a command: $2" "$@" ;;
*) set "Validation error ($1): $2" "$@"
esac
echo "$1" >&2
exit 1
}
usage_pull() {
cat<<'GETOPTIONSHERE'
Usage: atlas pull [options...] [directory mappings...]
Options:
--config CONFIG path to alternative atlas.yaml configuration file
-b, --branch BRANCH A branch of translation files
-r, --repository REPOSITORY The repository containing translation files
-f, --filter FILTER List of patterns to select which files and sub-directories to checkout.
-v, --verbose verbose output to terminal
-s, --silent no output to terminal
-h, --help
GETOPTIONSHERE
}
# Generated by getoptions (END)
# @end
parse_yaml() {
# adapted from https://gist.github.com/pkuczynski/8665367
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*'
local fs
fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" "$1" |
awk -F"$fs" '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'"$prefix"'",vn, $2, $3);
}
}'
}
set_pull_params() {
# Set vars from config yaml
if [ "$CONFIG" ];
then
if [ -f "$CONFIG" ]; then
eval "$(parse_yaml "$CONFIG")"
else
echo "$CONFIG does not exist."
exit
fi
else
if [ -f atlas.yml ]; then
eval "$(parse_yaml atlas.yml)"
else
pull_directory=""
pull_repository="openedx/openedx-translations"
pull_branch="main"
pull_filter=""
fi
fi
# Override vars based on args
if [ $# -gt 0 ];
then
# Due to sh/dash compatibility, arrays are not supported, therefore directories with spaces aren't supported.
pull_directory="$*"
fi
if [ "$REPOSITORY" ];
then
pull_repository=$REPOSITORY
fi
if [ "$BRANCH" ];
then
pull_branch=$BRANCH
fi
if [ "$FILTER" ];
then
pull_filter="$FILTER"
fi
pull_filter="$(echo "$pull_filter" | tr ',' ' ')" # Accept comma and/or space separated directories list
}
contains_substring() {
# Checks if $1 string contains $2.
printf '%s' "$1" | grep --fixed-strings --silent -e "$2"
return $?
}
directory_exists() {
# Checks if directory ($1) exists
# This is mainly used to be mocked in tests
test -d "$1"
return $?
}
display_pull_params() {
# Output configured vars to user
echo "Pulling translation files"
echo " - directory: ${pull_directory:-Not Specified}"
echo " - repository: ${pull_repository:-Not Specified}"
echo " - branch: ${pull_branch:-Not Specified}"
echo " - filter: ${pull_filter:-Not Specified}"
}
check_git_version() {
MINIMUM_GIT_VERSION="2.25.0" # The `git sparse-checkout` subcommand was introduced in 2.25.0
GIT_VERSION=$(git --version | grep 'git version' | sed -e 's/git version //g')
# Check if GIT_VERSION is a valid version number
if ! echo "$GIT_VERSION" | grep -Eq '^[0-9]+(\.[0-9]+){1,3}$';
then
echo "Unable to determine git version. Please ensure git is installed and available on your PATH."
return 1
fi
if [ "$(printf '%s\n' "$MINIMUM_GIT_VERSION" "$GIT_VERSION" | sort --version-sort | head -n1)" != "$MINIMUM_GIT_VERSION" ];
then
echo "Git version ${GIT_VERSION} is not supported. Please upgrade to ${MINIMUM_GIT_VERSION} or higher."
return 1
fi
}
git_sparse_checkout_set() {
# Call `git sparse-checkout set --no-cone` with the list of include/exclude files as arguments
# as a replacement of `sparse-checkout add` that isn't available in git 2.25.0
xargs git sparse-checkout set
}
pull_translations() {
check_git_version || return 1
if [ -z "$pull_directory" ];
then
echo "Missing positional argument:" >&2
echo " At least one DIRECTORY map is required as a positional argument:" >&2
echo " $ atlas pull DIRECTORY_FROM1:DIRECTORY_TO1 DIRECTORY_FROM2:DIRECTORY_TO2 ..." >&2
return 1
fi
if [ -z "$SILENT" ];
then
echo "Creating a temporary Git repository to pull translations into \"./translations_TEMP\"..."
fi
# use git in quiet mode when verbose isn't set
if [ "$VERBOSE" ];
then
quiet=""
else
quiet="--quiet"
fi
# use git in quiet mode when verbose isn't set
if [ "$pull_branch" ];
then
pull_branch_argument="--branch=${pull_branch}"
else
pull_branch_argument=""
fi
# Creating a shallow clone of the repo without without pulling the files yet
# shellcheck disable=SC2086
git clone $quiet $pull_branch_argument --filter=blob:none --no-checkout --depth=1 \
"https://github.com/${pull_repository}.git" translations_TEMP || exit
cd translations_TEMP || exit
# finished "Creating a temporary Git repository to pull translations into <temp dir>..." step
if [ -z "$SILENT" ];
then
echo "Done."
echo "Setting git sparse-checkout rules..."
fi
# Set git sparse-checkout list of include/exclude files
# Tells sparse checkout to ignore all files, except when otherwise noted
# See https://www.git-scm.com/docs/git-sparse-checkout for detailed implementation
git_sparse_checkout_rules="'--no-cone' '!*'"
for directory_from_to in $pull_directory;
do
directory_from=$(echo "${directory_from_to}" | cut -f1 -d ':')
directory_to=$(echo "${directory_from_to}" | cut -f2 -d ':')
if [ "$pull_filter" ];
then
for one_filter in $pull_filter;
do
# Include directories that matches the language pattern e.g. `/ar/`
git_sparse_checkout_rules="${git_sparse_checkout_rules} '${directory_from}/**/${one_filter}/**'"
# Include files that matches the language pattern e.g. `ar.*`
git_sparse_checkout_rules="${git_sparse_checkout_rules} '${directory_from}/**/${one_filter}.*'"
done
else
# Include all files within provided DIRECTORY if no filter is specified.
git_sparse_checkout_rules="${git_sparse_checkout_rules} '${directory_from}/**'"
fi
done
echo "$git_sparse_checkout_rules" | git_sparse_checkout_set
# finished "Creating a temporary Git repository to pull translations into <temp dir>..." step
if [ -z "$SILENT" ];
then
echo "Done."
echo "Pulling translation files from the repository..."
fi
# Retrieve translation files from the repo
git checkout HEAD
# Remove .git directory
rm -rf .git
# Leave the temp dir
cd ..
# finished "Pulling translations into <temp dir>..." step
# Copy translation files out of the temp dir
for directory_from_to in $pull_directory;
do
directory_from="$(echo "${directory_from_to}" | cut -f1 -d ':')"
directory_from="./translations_TEMP/${directory_from}"
# If no colon is provided then directory_to=. (aka working directory)
if contains_substring "$directory_from_to" ":";
then
directory_to="$(echo "${directory_from_to}" | cut -f2 -d ':')"
else
directory_to=.
fi
mkdir -p "$directory_to"
if [ -z "$SILENT" ];
then
echo "Done."
echo "Copying translations from \"${directory_from}\" to \"./${directory_to}\"..."
fi
if directory_exists "$directory_from";
then
cp -r "${directory_from}"/* "${directory_to}"/ || exit 1
else
if [ -z "$SILENT" ];
then
echo "Skipped copying \"${directory_from}\" because it was not found in the repository."
fi
fi
done
# finished "Copying translations from <temp dir> to <dest dir>..." step
if [ -z "$SILENT" ];
then
echo "Done."
echo "Removing temporary directory..."
fi
# Remove the temp dir
rm -rf translations_TEMP
# finshed "Removing temporary directory..." step
if [ -z "$SILENT" ];
then
echo "Done."
# finished pulling translations!
echo ""
echo "Translations pulled successfully!"
fi
}
# used for shellspec mocking
test || __() { :; }
parse "$@"
eval "set -- $REST"
if [ $# -gt 0 ]; then
cmd=$1
shift
case $cmd in
pull)
parse_pull "$@"
eval "set -- $PULL_REST"
set_pull_params "$@" # Make the arguments available to `set_pull_params`
if [ -z "$SILENT" ];
then
display_pull_params
fi
# allow mocking pull_translations
__ begin_pull_translations_mock __
pull_translations || exit 1
__ end_pull_translations_mock __
;;
--) # no subcommand, arguments only
esac
fi