-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrinkutoin
executable file
·997 lines (953 loc) · 30.6 KB
/
rinkutoin
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
#!/usr/bin/env bash
#
# @author hxAri (hxari)
# @create 07-01-2025 03:47
# @github https://github.com/hxAri/Rinkutoin
#
# Rinkutoin is an Open-Source project for doing various things related to LinkedIn, e.g Custom Voyager Graphql, Extract Profile Info, Media Downloader, etc.
#
# Rinkutoin Copyright (c) 2025 - hxAri <[email protected]>
# Rinkutoin Licence under GNU General Public Licence v3
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Subshell status code
# Just for container last subshell exit code.
SUBSHELLSTATUS=
# Current filename.
__name__="$0"
# Target basename.
pathname=$(basename $__name__)
# Change current working directory.
cd $(dirname $__name__)
# Iterate down a (possible) chain of symlinks.
while [ -L "$pathname" ]
do
pathname=$(readlink $pathname)
cd $(dirname $pathname)
pathname=$(basename $pathname)
done
# Clear the terminal screens.
clear
# Get current application basepath.
basepath=$(pwd)
# For compatibilty system.
if [[ ! $(command -v puts) ]]; then
# Puts is a pretty command line output (just for my os only)
function puts() {
echo -e "\x1b[0m$@"
}
fi
# Check if default virtual environment variable (does not set or blank).
if [[ ! -n $VIRTUAL ]]; then
export VIRTUAL=/home/virtual/bin/activate
fi
function main() {
# Program action.
local action=""
if [[ -n "$1" ]]; then
action=$(echo "$1" | grep -oP "\[{2}\K([^\]]+)" || echo "")
if [[ "[[$action]]" != "$1" ]]; then
unset action
fi
fi
# Program argument values.
local arguments=()
local i=1
for argument in ${@}; do
if [[ $i -ne 1 ]] || [[ -z "$action" ]]; then
arguments+=( "$argument" )
fi
i=$((i+1))
done
# Default filee configurations.
local config=${basepath}/.config
# Default application configurations.
declare -A configs=(
[branch]=
[checked]=false
[clean]=true
[compress]=tar
[environment]=development
[include]=${includes[@]}
[library]=
[packages]=
[program]=example
[repository]=
[root]=false
[servers]=
[version]=1.0.0
[virtual]=${VIRTUAL}
[virtualize]=true
)
# Default includes files and directories on backup.
local includes=(
".git"
".gitignore"
".github"
"LICENSE"
"pyproject.toml"
"README.md"
"requirements.txt"
"resources"
"setup.py"
"src"
)
# Replace all spaces into comma as separator.
# And please don't use spaces for file or directory name!
configs[include]="${configs[include]// /,}"
# Handle activate virtual environment.
function activate() {
local command=
local create=
local filename=
local install=
local mname=
local mversion=
local moperator=
local requirement="${basepath}/requirements.txt"
local requirements=()
if [[ ${configs[virtualize]} == false ]]; then
return 0
elif [[ -z ${configs[virtual]} ]]; then
puts " <<- i > activate: venv environment activation: e.g /path/to/venv/bin/activate"
while [[ ! -f $filename ]]; do
stdin "filename" "<<- r > virtual:"
if [[ "$filename" = "" ]]; then
continue
elif [[ ! -f "$filename" ]]; then
target=$(dirname "${filename}")
if [[ "$target" == "." ]]; then
target=$(pwd)
fi
while [[ $create == "" ]]; do
stdin "create" "<<- r > activate: create the virtual environment here \"${target}\" with name \"${filename}\" [Y/n]"
if [[ $create == "" ]]; then
create=y
fi
create="${create,,}"
if [[ "$create" == "y" ]]; then
puts " <<- i > activate: creating virtual environment on \"${target}\""
python3 -m venv ${target}
if [[ $? -eq 0 ]]; then
configs[virtual]=$filename
puts " <<- i > activate: success created virtual environment on \"${target}\""
fi
create=
break
elif [[ "$create" == "n" ]]; then
create=
break
else
create=
fi
done
else
configs[virtual]=$filename
break
fi
done
stdin "install" "<<- r > activate: requirement: install all requirement modules [Y/n]"
install="${install,,}"
if [[ "$install" == "y" ]]; then
pip install -r "$requirement"
if [[ $? -eq 1 ]]; then
puts " <<- e > activate: requirement: failed install requirement python modules"
return 1
fi
configs[checked]=true
install=
break
elif [[ "$install" == "n" ]]; then
puts " <<- e > activate: requirement: aborted"
return 1
else
install=
fi
config --write
elif [[ ! -f ${configs[virtual]} ]]; then
puts " <<- i > activate: ${configs[virtual]}: no such file or directory"
configs[virtual]=
activate
return $?
fi
source ${configs[virtual]}
if [[ $? -ne 0 ]]; then
puts " <<- ! > activate: something wrong when activate virtual environment"
return 1
fi
if [[ ${configs[checked]} != true ]] && [[ -f $requirement ]]; then
readarray -t requirements < "$requirement"
for module in ${requirements[@]}; do
mname=$(echo "$module" | sed -E "s/(^[a-zA-Z0-9_-]+).*/\1/")
mversion=$(echo "$module" | sed -E "s/^[a-zA-Z0-9_-]+(>=|<=|==|>|<|=)([0-9\.]+)/\2/")
moperator=$(echo "$module" | sed -E "s/^[a-zA-Z0-9_-]+(>=|<=|==|>|<|=)([0-9\.]+)/\1/")
if [[ $(pip list | grep "$mname" | grep "$mversion") == "" ]]; then
puts " <<- i > dependency: module: ${module}: python module is not installed"
stdin "install" "<<- r > dependency: module: ${module}: install this module [Y/n]"
install="${install,,}"
if [[ "$install" == "y" ]]; then
command="pip install ${module}"
puts " <<- i > dependency: module: ${module}: ${command}"
puts " <<- i > dependency: module: ${module}: pip: installing python module"
subshell "$command" "<<- i > dependency: module: ${module}: pip: {stdout}"
if [[ $SUBSHELLSTATUS -eq 1 ]]; then
puts " <<- e > dependency: module: ${module}: pip: failed install python module"
return 1
fi
unset IFS
install=
break
elif [[ "$install" == "n" ]]; then
puts " <<- e > dependency: module: aborted"
return 1
else
install=
fi
fi
done
fi
}
# Return archive filename.
function archive() {
local compression=${configs[compress]}
if [[ -n $1 ]]; then
compression=$1
fi
local extension="tar.gz"
if [[ -n $2 ]]; then
extension=$2
fi
if [[ "$compression" =~ ^(tar|zip)$ ]]; then
if [[ "$compression" == "zip" ]]; then
extension="zip"
fi
echo "${configs[program],,}-${configs[version]}.${extension}"
fi
}
# Handle create program archive.
# execute: backing [arguments]
function backing() {
local archive=$(archive $@)
local compression=${configs[compress]}
if [[ -n $1 ]]; then
compression=$1
fi
if [[ -z $archive ]]; then
puts " <<- e > backing: ${compression}: unsupported compression"
return 1
fi
local filenames=()
local arguments=()
if [[ "$compression" == "tar" ]]; then
arguments+=( "-cvf" )
fi
IFS="," read -r -a filenames <<< "${configs[include]}"
unset IFS
for index in ${!filenames[@]}; do
local filename=${filenames[$index]}
if [[ ! -d $filename ]] && [[ ! -f $filename ]]; then
unset filenames[$index]
fi
done
local index=1
local length=${#filenames[@]}
local spaces=""
local command="$compression ${arguments[@]}"
for (( i=0; i<${#command}; i++ )); do
spaces+=" "
done
local command="$compression ${arguments[@]} $archive ${filenames[@]}"
puts " <<- i > backing: creating archive with \"${compression}\" compression"
puts " <<- i > backing: $compression ${arguments[@]} ${basepath}/$archive \\"
for filename in ${filenames[@]}; do
if [[ $index -lt $length ]]; then
puts " <<- i > backing: ${spaces} ${basepath}/${filename} \\"
else
puts " <<- i > backing: ${spaces} ${basepath}/${filename}"
fi
index=$((index+1))
done
subshell "$command" "<<- i > backing: ${compression}: {stdout}"
if [[ $SUBSHELLSTATUS -eq 0 ]]; then
puts " <<- i > backing: ${compression}: archive: filename: ${archive}"
puts " <<- i > backing: ${compression}: archive: fullname: ${basepath}/${archive}"
return 0
fi
puts " <<- i > backing: ${compression}: something wrong when creating archive file"
return 1
}
# Handle git auto commit for added or modifiend files.
function commits() {
if [[ ! -d ${basepath}/.git ]]; then
puts " <<- e > commits: .git: no such directory"
return 1
fi
local branch=${configs[branch]}
if [[ -z $branch ]]; then
puts " <<- i > commits: branch: name not defined in config"
stdin "branch" "<<- r > commits: branch:"
fi
while [[ ! $(git ls-remote origin "$branch") ]]; do
puts " <<- e > commits: branch: ${branch}: no such origin branch"
branch=
stdin "branch" "<<- r > commits: branch:"
done
if [[ "$branch" != "$(git branch --show-current)" ]]; then
subshell "git checkout $branch" "<<- i > commits: git: checkout: {stdout}"
fi
local filenames=()
local options=()
if [[ $(git config --get commit.gpgSign) ]]; then
options+=( "-S" )
fi
options+=( "-m" )
local statuses=()
puts " <<- e > commits: scanning files"
readarray -t statuses < <(git status --short)
for stdout in "${statuses[@]}"; do
local append=
local filename="${stdout:3}"
local messages=
local status="${stdout:0:2}"
status=${status/ /}
stdin "append" "<<- r > commits: ${filename}: ${status}: add this file [Y/n]" "N"
if [[ "${append^^}" == "Y" ]]; then
if [[ "${status/ /}" == "M" ]]; then
git diff "$filename"
fi
puts " <<- i > commits: git: add \"$filename\""
git add "$filename"
stdin "messages" "<<- r > commits: ${filename}: messages:"
puts " <<- i > commits: git: commit ${options[@]} \"${messages//\"/\\\"}\""
git commit ${options[@]} "$messages"
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
puts " <<- i > commits: git: something wrong when create commit"
return 1
fi
filenames+=( "$filename" )
fi
done
if [[ ${#filenames[@]} -ne 0 ]]; then
puts " <<- i > commits: git: push: origin \"$branch\""
git push origin "$branch"
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
puts " <<- e > commits: git: push: error when push into remote repository"
return 1
fi
fi
}
# Handle read and write application configuration e.g read and write configs.
# execute: config --read|write
function config() {
local action=$1
local keyset=
local keysets=()
for keyset in ${!configs[@]}; do
keysets+=( $keyset )
done
local contents=()
local values=
if [[ "$action" == "--read" ]]; then
while IFS="" read -r line; do
if [[ -z "$line" ]] || [[ ${line:0:1} == "#" ]]; then
continue
fi
keyset=$(echo -e "$line" | cut -d "=" -f 1)
values=$(echo -e "$line" | cut -d "=" -f 2)
if [[ -n "$keyset" ]] && [[ ${keysets[@]} =~ $keyset ]]; then
if [[ $keyset == virtual ]]; then
if [[ -z "$values" ]]; then
puts " <<- ! > config: virtual environment activation required"
return 1
elif [[ ! -f "$values" ]]; then
puts " <<- ! > config: $values: no such virtual environment"
return 1
fi
fi
configs[$keyset]=$values
else
puts " <<- ! > config: $keyset: unknown configuration variable"
return 1
fi
done < $config
unset IFS
elif [[ "$action" == "--write" ]]; then
echo -e "\c" > "$config"
for keyset in ${!configs[@]}; do
echo -e "${keyset}=${configs[$keyset]}" >> "$config"
if [[ $? -eq 0 ]]; then
puts " <<- ! > config: error: when write configuration"
fi
done
elif [[ -z $1 ]]; then
puts " <<- ! > config: require option --read|write"
return 1
else
puts " <<- ! > config: $action: unknown option"
return 1
fi
}
# Handle check and validate dependency requirements.
function dependency() {
# Handle linux check and install packet in linux distro.
# execute: manager --available [package]
# manager --check [package]
# manager --install [package]
function manager() {
local action=$1
local distro=
local manager=
local package=$2
if [[ -f /etc/os-release ]]; then
distro=$(grep -E "^ID=" /etc/os-release | cut -d "=" -f2 | tr -d "\"")
elif [[ -f /etc/redhat-release ]]; then
distro="rhel"
elif [[ -f /etc/lsb-release ]]; then
distro=$(grep DISTRIB_ID /etc/lsb-release | cut -d "=" -f2 | tr "[:upper:]" "[:lower:]" )
elif [[ -f /etc/debian_version ]]; then
distro="debian"
elif [[ -f /etc/alpine-release ]]; then
distro="alpine"
elif [[ -f /etc/SuSE-release ]]; then
distro="suse"
else
if command -v hostnamectl &> /dev/null; then
distro=$(hostnamectl | grep "Operating System" | cut -d: -f2 | awk "{print tolower(\$1)}")
elif command -v uname &> /dev/null; then
distro=$(uname -s | awk "{print tolower(\$1)}")
else
puts " <<- e > manager: unknown linux distribution"
exit 1
fi
fi
case "$distro" in
ubuntu|debian) manager="apt" ;;
fedora|centos|rhel|rocky|alma) manager="dnf" ;;
opensuse|suse) manager="zypper" ;;
arch|manjaro) manager="pacman" ;;
alpine) manager="apk" ;;
void) manager="xbps" ;;
nixos) manager="nix-env" ;;
gentoo) manager="emerge" ;;
slackware) manager="slackpkg" ;;
*)
if [[ $(command -v apt &> /dev/null) ]]; then
manager="apt"
elif [[ $(command -v dnf &> /dev/null) ]]; then
manager="dnf"
elif [[ $(command -v pacman &> /dev/null) ]]; then
manager="pacman"
elif [[ $(command -v apk &> /dev/null) ]]; then
manager="apk"
elif [[ $(command -v zypper &> /dev/null) ]]; then
manager="zypper"
elif [[ $(command -v xbps &> /dev/null) ]]; then
manager="xbps"
elif [[ $(command -v nix-env &> /dev/null) ]]; then
manager="nix-env"
elif [[ $(command -v emerge &> /dev/null) ]]; then
manager="emerge"
elif [[ $(command -v slackpkg &> /dev/null) ]]; then
manager="slackpkg"
else
puts " <<- e > manager: unknown linux default manager packet"
exit 1
fi
;;
esac
if [[ "$action" == "--available" ]]; then
case "$manager" in
apt) apt-cache search "^$package$" | grep -qw "$package" ;;
dnf|yum) yum list available "$package" &> /dev/null ;;
pacman) pacman -Ss "^$package$" &> /dev/null ;;
apk) apk search "^$package$" &> /dev/null ;;
zypper) zypper se "$package" &> /dev/null ;;
xbps) xbps-query -Rs "^$package\$" &> /dev/null ;;
nix-env) nix-env -qaP "^$package$" &> /dev/null ;;
emerge) emerge --search "$package" &> /dev/null ;;
slackpkg) slackpkg search "$package" &> /dev/null ;;
flatpak) flatpak search "$package" &> /dev/null ;;
snap) snap find "$package" &> /dev/null ;;
esac
elif [[ "$action" == "--check" ]]; then
case "$manager" in
apt) dpkg -l | grep -qw "$package" ;;
dnf|yum) rpm -q "$package" &> /dev/null ;;
pacman) pacman -Qi "$package" &> /dev/null ;;
apk) apk info "$package" &> /dev/null ;;
zypper) zypper se --installed-only "$package" &> /dev/null ;;
xbps) xbps-query -Rs "^$package\$" &> /dev/null ;;
nix-env) nix-env -q "$package" &> /dev/null ;;
emerge) equery list "$package" &> /dev/null ;;
slackpkg) slackpkg search "$package" | grep -q "installed" ;;
flatpak) flatpak list | grep -qw "$package" ;;
snap) snap list | grep -qw "$package" ;;
esac
elif [[ "$action" == "--install" ]]; then
case "$manager" in
apt) sudo apt update && sudo apt install -y "$package" ;;
dnf|yum) sudo "$manager" install -y "$package" ;;
pacman) sudo pacman -S --noconfirm "$package" ;;
apk) sudo apk add "$package" ;;
zypper) sudo zypper install -y "$package" ;;
xbps) sudo xbps-install -y "$package" ;;
nix-env) nix-env -i "$package" ;;
emerge) sudo emerge "$package" ;;
slackpkg) sudo slackpkg install "$package" ;;
flatpak) sudo flatpak install -y "$package" ;;
snap) sudo snap install "$package" ;;
esac
elif [[ -z $1 ]]; then
puts " <<- ! > manager: require option --check|install"
exit 1
else
puts " <<- ! > manager: $action: unknown option"
exit 1
fi
return $?
}
local install=
local packages=()
if [[ -n ${configs[packages]} ]]; then
IFS="," read -r -a packages <<< "${configs[packages]}"
for package in ${packages[@]}; do
manager --check "$package"
if [[ $? -eq 0 ]]; then
continue
fi
puts " <<- i > dependency: package: ${package}: package is not installed"
stdin "install" "<<- r > dependency: package: ${package}: install this package [Y/n]"
if [[ "${install,,}" == "y" ]]; then
manager --available "$package"
if [[ $? -eq 1 ]]; then
puts " <<- e > dependency: package: ${package}: is not available on current manager packet"
return 1
fi
manager --install "$package"
if [[ $? -eq 1 ]]; then
puts " <<- e > dependency: package: ${package}: failed install package"
return 1
fi
install=
break
elif [[ "${install,,}" == "n" ]]; then
puts " <<- e > dependency: package: aborted"
return 1
else
install=
fi
done
unset IFS
fi
local libraries=()
if [[ -n ${configs[library]} ]]; then
IFS="," read -r -a libraries <<< "${configs[library]}"
for library in ${libraries[@]}; do
ldconfig -p | grep -q "$library"
if [[ $? -eq 1 ]]; then
puts " <<- e > dependency: library: ${library}: library is not available"
return 1
fi
done
unset IFS
fi
activate
if [[ $? -eq 1 ]]; then
return 1
fi
}
# Deploy the program into servers.
function deploys() {
local archive=$(archive)
local destinate=
local hostname=
local port=
local servers=()
local username=
if [[ -z ${configs[servers]} ]]; then
puts " <<- e > deploys: no servers available"
return 1
fi
backing
if [[ $? -ne 0 ]]; then
puts " <<- e > deploys: something wrong when create archive"
return 1
fi
# [email protected][22]:/path/to/path
IFS="," read -r -a servers <<< "${configs[servers]}"
for server in ${servers[@]}; do
port=$(echo "$server" | grep -oP "\[\K[^\]]+" || echo "")
if [[ -z "$port" ]]; then
port=22
fi
username=$(echo "$server" | grep -oP "^[^@]+" || echo "")
hostname=$(echo "$server" | grep -oP "@\K[^[]+" || echo "")
destinate=$(echo "$server" | grep -oP ":(.+)$" | cut -c2- || echo "")
if [[ -z "$username" ]]; then
puts " <<- e > deploys: username: server username required"
return 1
elif [[ -z "$hostname" ]]; then
puts " <<- e > deploys: username: server hostname required"
return 1
elif [[ -z "$destinate" ]]; then
puts " <<- e > deploys: username: server pathname required"
return 1
else
puts " <<- i >> deploys: scp -v -P ${port} ${archive} ${username}@${hostname}:${destinate}"
scp -v -P ${port} ${archive} ${username}@${hostname}:${destinate}
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
puts " <<- i >> deploys: failed upload archive to ${username}@${hostname}:${destinate}"
return 1
fi
puts " <<- i >> deploys: successfully upload archive to ${username}@${hostname}:${destinate}"
fi
puts " <<- i >> deploys: successfully upload archive to ${#servers[@]} servers"
done
unset IFS
}
# Handling iterrupt keyboard controll.
function interrupt() {
puts ""
puts " <<- k > interrupt: interrupt signal detected"
stdin "close" "<<- k > interrupt: exit this program [Y/n]" "Y"
if [[ "${close^^}" == "Y" ]]; then
puts " <<- k > interrupt: aborted"
exit 1
fi
}
# Set handle interrupt keyboard signal.
trap interrupt SIGINT
# Handle linting python code with pylint.
function linting() {
local filenames=()
local install=
local options=(
"--argument-naming-style=camelCase"
"--attr-naming-style=camelCase"
"--class-attribute-naming-style=any"
"--class-const-naming-style=any"
"--class-naming-style=PascalCase"
"--const-naming-style=any"
"--extension-pkg-allow-list=pyzstd"
"--variable-naming-style=camelCase"
"--function-naming-style=camelCase"
"--max-attributes=30"
"--max-public-methods=60"
"--method-naming-style=camelCase"
"--good-names=\"e,Executor,i,id,ProcessExecutor,u,k,ThreadExecutor,ThreadRunner,v\""
"--ignore-docstrings=y"
"--ignore-long-lines=^([^\\n]+)$"
"--indent-string=\"\t\""
"--disable=arguments-differ"
"--disable=broad-exception-caught"
"--disable=consider-using-f-string"
"--disable=function-redefined"
"--disable=keyword-arg-before-vararg"
"--disable=invalid-name"
"--disable=missing-function-docstring"
"--disable=missing-module-docstring"
"--disable=multiple-statements"
"--disable=non-parent-init-called"
"--disable=protected-access"
"--disable=redefined-argument-from-local"
"--disable=redefined-builtin"
"--disable=redefined-outer-name"
"--disable=too-many-arguments"
"--disable=too-many-boolean-expressions"
"--disable=too-many-branches"
"--disable=too-many-lines"
"--disable=too-many-locals"
"--disable=too-many-nested-blocks"
"--disable=too-many-statements"
"--disable=super-init-not-called"
"--disable=too-few-public-methods"
"--disable=trailing-newlines"
"--disable=trailing-whitespace"
"--disable=unnecessary-dunder-call"
"--disable=unnecessary-ellipsis"
"--disable=unnecessary-lambda-assignment"
"--disable=unnecessary-pass"
"--disable=unexpected-special-method-signature"
"--disable=use-dict-literal"
"--disable=wrong-import-order"
"--disable=wrong-import-position"
)
if [[ -d ${basepath}/src ]]; then
readarray -t filenames <<< "$(find ${basepath}/src -type f -name "*.py")"
if [[ $(pip list | grep pylint) == "" ]]; then
configs[checked]=false
puts " <<- e > linting: pylint is not installed"
if [[ ${configs[virtualize]} == true ]]; then
stdin "install" "<<- r > linting: install pylint from pip [Y/n]" "n"
install=${install,,}
if [[ "$install" == "y" ]]; then
puts " <<- e > linting: pip install pylint"
puts " <<- e > linting: pip: installing pylint"
subshell "pip install pylint" "<<- i > linting: pip: {stdout}"
if [[ $SUBSHELLSTATUS -eq 1 ]]; then
puts " <<- e > linting: pip: something wrong when install pylint"
return 1
fi
elif [[ "$install" == "n" ]]; then
puts " <<- e > linting: aborted"
return 1
fi
else
return 1
fi
fi
puts " <<- i > linting: python3 -m pylint \\"
index=0
for option in ${options[@]}; do puts " <<- i > ${option} \\"; done
for filename in ${filenames[@]}; do
index=$((index+1))
if [[ $index -ne ${#filenames[@]} ]]; then
puts " <<- i > ${filename/$basepath\//} \\"
else
puts " <<- i > ${filename/$basepath\//}"
fi
done
while IFS= read -r stdout; do
puts " <<- e > linting: pylint: ${stdout}"
done < <(python3 -m pylint ${options[@]} ${filenames[@]/$basepath\//})
if [[ $SUBSHELLSTATUS -eq 1 ]]; then
puts " <<- e > linting: pylint: something wrong when linting code"
fi
return $returns
fi
}
# Handle install python program as python module in local machine.
function pyinstall() {
local command="pip install ."
if [[ ${configs[environment]} == "development" ]]; then
command="pip install -e ."
fi
puts " <<- i > pyinstall: ${command}"
puts " <<- i > pyinstall: pip: installing ${configs[program]} as python module"
if [[ $(pip list | grep -q "${configs[program]}") ]]; then
puts "found"
fi
subshell "$command" "<<- i > pyinstall: pip: {stdout}"
if [[ $SUBSHELLSTATUS -eq 0 ]]; then
puts " <<- i > pyinstall: pip: successfully install ${configs[program]} as python module"
else
puts " <<- i > pyinstall: pip: ${SUBSHELLSTATUS}: failed install ${configs[program]} as python module"
fi
unset IFS
return $status
}
# Handle readline with colorized prompt.
# execute: [variable]=; stdin [variable] [prompt] [default]
function stdin() {
local default="$3"
local input=
local prompt="$(puts "$2")"
local variable="$1"
if [[ -z $1 ]]; then
puts " <<- e > stdin: error: variable name required"
return 1
elif [[ -z $2 ]]; then
puts " <<- e > stdin: error: input prompt required"
return 1
fi
while [[ "$input" == "" ]]; do
echo -n " $prompt "
read "input"
if [[ -z "$input" ]] && [[ -n "$default" ]]; then
input="$default"
fi
done
if [[ ${configs[environment]} == "production" ]]; then
eval "$variable=\"${input//\"/\\\"}\"" >> /dev/null 2>&1
else
eval "$variable=\"${input//\"/\\\"}\""
fi
}
# Handle subshell execution.
# I don't have any idea for check last status code from subshell
# because PIPESTATUS always give 1 status code when command work or no error.
# execute: subshell "command" "formatter"
function subshell() {
local command="$1"
local formatter="$2"
while IFS= read -r stdout; do
if [[ $(echo -e "$stdout" | cut -d "=" -f "1" ) != "SUBSHELLSTATUS" ]]; then
puts " $(echo "${formatter/\{stdout\}//$stdout}")"
continue
fi
eval $stdout
done < <($command; echo -e "SUBSHELLSTATUS=${PIPESTATUS[0]}")
}
# Handle upgrade application into new version
# from remote repository e.g github, gitlab.
# execute: upgrade -y
function upgrade() {
if [[ -z ${configs[repository]} ]]; then
puts " <<- i > upgrade: no remote repository available"
return 1
fi
local upgrade=$1
if [[ -z "$upgrade" ]]; then
puts " <<- i > upgrade: ${basepath}: this directory will be deleted permanently"
stdin "upgrade" "<<- r > upgrade: are you sure want to upgrade [Y/n]" "n"
else
upgrade=$(echo -e "$1" | cut -d "-" -f "2")
fi
if [[ "${upgrade^^}" != "Y" ]]; then
puts " <<- e > upgrade: aborted"
return 1
fi
local archive=$(cd ..; pwd)
archive+="/"
archive+=$(echo -e $(archive) | cut -d "-" -f "1")
local branch=$(git remote show origin | grep "HEAD branch" | awk "{print \$NF}")
if [[ -z "$branch" ]]; then
puts " <<- e > upgrade: git: something wrong with your git repository"
return 1
fi
local compression=${configs[compress]}
if [[ -z $archive ]]; then
puts " <<- e > upgrade: ${compression}: unsupported compression"
return 1
fi
local repository="${configs[repository]}"
if [[ "$compression" == "tar" ]]; then
archive+=".tar.gz"
repository+="/archive/refs/heads/${branch}.tar.gz"
else
archive+=".zip"
repository+="/archive/refs/heads/${branch}.zip"
fi
local extracted=$(echo -e "$repository" | cut -d "/" -f "5" )
extracted+="-${branch}"
puts " <<- e > upgrade: wget: $repository: downloading archive"
wget --tries=20 "$repository" --show-progress --progress=bar:force:noscroll -O "$archive"
if [[ $? -ne 0 ]]; then
puts " <<- e > upgrade: wget: something wrong when downloading archive"
puts " <<- e > upgrade: wget: please makesure check your remote repository url"
return 1
fi
local command="tar -xvf $archive"
if [[ "$compression" == "zip" ]]; then
command="unzip $archive"
fi
cd ..
subshell "$command" "<<- e > upgrade: ${compression}: {stdout}"
if [[ $SUBSHELLSTATUS -eq 0 ]]; then
if [[ ! -d "$extracted" ]]; then
puts " <<- e > upgrade: ${extracted}: no such extracted directory"
puts " <<- e > upgrade: please report this insiden into developer"
return 1
fi
puts " <<- e > upgrade: ${basepath}: removing directory"
rm -rf ${basepath}
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
puts " <<- e > upgrade: ${basepath}: failed removing directory"
return 1
fi
puts " <<- e > upgrade: ${extracted}: moving directory"
mv ${extracted} ${basepath}
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
puts " <<- e > upgrade: ${extracted}: failed moving directory"
return 1
fi
puts " <<- e > upgrade: change working directory"
cd -
puts " <<- e > upgrade: successfully upgrade program"
puts " <<- e > upgrade: please re-run this program with option --initial"
else
puts " <<- e > upgrade: ${compression}: ${archive##/}: failed decompress archive"
return 1
fi
}
# Check if configuration file is exists.
if [[ -f $config ]]; then
config --read
if [[ $? -eq 1 ]]; then
return 1
fi
fi
# Check and validate dependency requirement.
dependency
if [[ $? -eq 1 ]]; then
return 1
fi
# Check whether if root previleges require for run the program.
if [[ ${configs[root]} == true ]]; then
if [[ $UID -ne 0 ]]; then
puts " <<- ! > ${configs[program]}: root previleges required"
return 1
fi
fi
if [[ -n "$action" ]]; then
case "$action" in
backing) backing ${arguments[@]} ;;
commits) commits ;;
install) pyinstall ;;
linting) linting ;;
package) pip ${arguments[@]} ;;
deploys) deploys ;;
upgrade) upgrade ${arguments[@]} ;;
*)
puts " "
puts " Usage: bash \"$(basename ${__name__})\" [OPTIONS] [COMMAND] [ARGS]..."
puts " bash \"$(basename ${__name__})\" [[COMMAND]] [OPTIONS] [ARGS]..."
puts " "
puts " ${configs[program]^}"
puts " "
puts " Commands:"
puts " "
puts " - [[backing]] Performing a backup of the program code"
puts " · Arguments:"
puts " - [compression] e.g tar|zip"
puts " - [extension] e.g tar.gz|zip"
puts " - [[commits]] Make a commit to all files except in .gitignore"
puts " - [[help]] Show this message and exit"
puts " - [[install]] Install the program code as a python module"
puts " - [[linting]] Run pylint to perform code analysis"
puts " - [[package]] Alias for pip when you run on venv"
puts " - [[deploys]] Deploy code into multiple entire remote servers"
puts " - [[upgrade]] Upgrade program into latest version from remote repository"
puts " · Options:"
puts " - [-y] Force auto update without confirmation"
puts " "
puts " Example: bash \"$(basename ${__name__})\""
puts " bash \"$(basename ${__name__})\" [[backing]] tar tar.gz"
puts " "
if [[ "$action" != "help" ]]; then
return 1
fi
;;
esac
else
python3 src/${configs[program]}.py ${arguments[@]}
fi
if [[ $(command -v deactivate) ]]; then
deactivate
if [[ $? -eq 1 ]]; then
puts " <<- i > ${configs[program]}: something wrong when deactivate venv"
return 1
fi
fi
if [[ ${configs[clean]} == true ]]; then
for cache in $(find "${basepath}/" | grep "__pycache__"); do
if [[ -d $cache ]]; then
rm -rf $cache
if [[ $? -ne 0 ]]; then
puts " <<- i >> ${configs[program]}: ${cache}: failed remove cache"
return 1
fi
fi
done
fi
return 0
}
main "$@"
exit "$?"