-
Notifications
You must be signed in to change notification settings - Fork 0
/
planterbox
executable file
·566 lines (519 loc) · 18.5 KB
/
planterbox
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
#!/bin/bash
# ===============================================================================
# Virtual Environment Utility - PlanterBox
# ===============================================================================
# This file must be used with "source path/ve.sh" *from bash*
# you cannot run it directly. (from venv activate script,.. applies)
#
# The script itself must be sourced to run correctly.
#
# Add the following line to .bash_profile or .bashrc to employ the 've' alias
# alias ve="source /Users/tim/Projects/timve/timve.sh $1 $2" <--UPDATE THIS
# or?: export cmd..
#
# -------------------------------------------------------------------------------
# List of unavailable venv names(being used as keywords:
# (aliases and built-ins? problem too)
#
# Because script takes as a first argument either a command, OR the name of a
# virtual env to be loaded, the two can't be equal. The payoff is shorthand load commands
# like 've tacoApp' rather than 've load tacoApp'. These keywords are forbidden:
taken_names=("" "blank" "-l" "l" "ls" "--list" "list" \
"-t" "t" "--trash" "trash" \
"-delete" "delete" "--delete" \
"-c" "c" "--create" "create" \
"-cd" "cd" "--chdir" "chdir" \
"-s" "s" "--search" "search" \
"-cpy" "cpy" "--copy" "copy" \
"-rm" "rm" "--remove" "remove" \
"-r" "r" "restore" "restore" \
"-h" "h" "--help" "help" \
"-p" "p" "--paths" "paths" \
"-v" "v" "--version" "version" \
"-hs" "hs" "-hist" "hist" "--history" "history")
#
# -------------------------------------------------------------------------------
# Load config file, composed of user-defined Constants and Utilities.
BASE_P="${BASH_SOURCE%/*}/" #could test is dir: if [[ ! -d "$BASE_P" ]]; then BASE_P="$PWD"; fi
VERSION='planterbox 1.0'
CONFIG="${BASE_P}planterbox.cfg"
UTILS="${BASE_P}utils"
source "$CONFIG"
source "$UTILS" #needs to have .sh ext and not be executable, timve should lose .sh
# Remove slashes from all CL args. For tab autocomplete because we are tabbing
# for directories. Done here before arguments are touched anywhere else.
z1="${1%/}" && z2="${2%/}" && z3="${3%/}"
set -- "$z1" "$z2" "$z3"
# build from constants all paths needed for script to operate,
# no-matter if they are unused.
# * refacto{r; new name scheme
env_path="${VENVS_PATH}${2}/"
proj_path="${PROJS_PATH}${2}/"
trash_path="${TRASHCAN_PATH}${2}/"
trash_venv="${trash_path}ve/"
trash_proj="${trash_path}proj/"
trash_edit="${trash_path}subl/"
reqs_default="${REQS_PATH}default-requirements.txt"
templ_path="${TEMPLATES_PATH}${3}/"
templ_default="${TEMPLATES_PATH}default/"
# copy directories not individual files
# remove sublime name for generic editor
trash_subl_proj="${trash_edit}${2}.sublime-project"
trash_subl_work="${trash_edit}${2}.sublime-workspace"
subl_proj_path="${SUBL_PROJS_PATH}${2}.sublime-project"
subl_work_path="${SUBL_PROJS_PATH}${2}.sublime-workspace"
# common formatting strings shortened.
# success and alert used in feedback, the rest in help and blank ve command.
# cant use readonly because file must be sourced(cant switch envs?)
LB="${LC}[${NC}"
RB="${LC}]${NC}"
NN="(${EC}number${NC})"
NM="(${EC}name${NC})"
NME="(${EC}name of env${NC})"
NMC="(${EC}copy name${NC})"
success="[${LC}success${NC}]"
alert="[${EC}alert${NC}]"
#
# ===============================================================================
# Formatting functions
#
#
# template for alert message
# takes a message, a variable, and suppress newline condition
lert () {
local end="\\n"
[ "$3" = true ] && end="" #suppress newline
printf " %b %b%b%b %b%b" "$alert" "$TC" "$1" "$NC" "$2" "$end"
}
# template for success message
succ () {
printf " %b %b%b%b %b\\n" "$success" "$TC" "$1" "$NC" "$2"
}
# indent output by piping through sed. take the indent as param
dent () {
local margin="${1:- }"
sed "s/^/${margin}/"
}
# print a divider
print_div () {
#printf "%b ---------------------------------------------------------%b\\n" "$LC" "$NC"
printf "%b ---------------------------------------------------------------------%b\\n" "$LC" "$NC"
}
# template to begin output of commands.
start_temp () {
local condition="$1"
if [ "$condition" = true ]; then
printf "\\n %b* planterbox %b%s%b%s%b \\n" "$LC" "$TC" "$2" "$EC" "$3" "$NC"
print_div
fi
}
# template to end.
end_temp () {
print_div
new_line
}
# new line printed
new_line () {
printf "\\n"
}
# print short help section
print_help_short () {
printf " %b list:%b ve list \\t %b create:%b ve create %b\\n" "$LC" "$NC" "$LC" "$NC" "$NME"
printf " %b load:%b ve %b %b remove:%b ve remove %b\\n" "$LC" "$NC" "$NME" "$LC" "$NC" "$NME"
#new_line
#printf " %b For expanded commands and options:%b ve help\\n" "$TC" "$NC"
new_line
}
# print detailed help section
print_help () {
printf " %-14b %30b %-40b \\n" "${LB} ${RB}" "${TC} Load virtual env:" "${NC} ve ${NM}"
printf " %-14b %30b %-40b \\n" "${LB}l${RB}" "${TC} List virtual envs:" "${NC} ve list"
printf " %-14b %30b %-40b \\n" "${LB}s${RB}" "${TC} Search virtual envs:" "${NC} ve search ${NM}"
printf " %-14b %30b %-40b \\n" "${LB}c${RB}" "${TC} Create virtual env:" "${NC} ve create ${NM}"
printf " %-14b %30b %-40b \\n" "${LB}rm${RB}" "${TC} Remove virtual env: " "${NC} ve remove ${NM}"
printf " %-14b %30b %-40b \\n" "${LB}t${RB}" "${TC} List trash:" "${NC} ve trash"
printf " %-14b %30b %-40b \\n" "${LB}r${RB}" "${TC} Restore virtual env:" "${NC} ve restore ${NM}"
printf " %-14b %30b %-40b \\n" "${LB}cpy${RB}" "${TC} Copy virtual env: " "${NC} ve copy ${NM} ${NMC}"
printf " %-14b %30b %-40b \\n" "${LB}h${RB}" "${TC} Help:" "${NC} ve help"
printf " %-14b %30b %-40b \\n" "${LB}p${RB}" "${TC} Check paths:" "${NC} ve paths"
printf " %-14b %30b %-40b \\n" "${LB}hist${RB}" "${TC} History: " "${NC}ve history"
printf " %-14b %30b %-40b \\n" "${LB}cd${RB}" "${TC} CD to project: " "${NC} ve cd"
print_div
printf "%b\\n" " ${TC} * ${LB}source ve.sh list${RB} ${TC}can become ${LB}ve l${RB} ${TC}with alias"
new_line
}
# Print usage
print_help2() {
echo -n "${scriptName} [OPTION]... [FILE]...
This is my script template.
Options:
l, ls, --list, list List virtual environments.
t, --trash, trash List virtual environments in trash.
c, --create, create Create new virtual environment and extras.
cd, --chdir, chdir Change directory to current
-u, --username Username for script
-p, --password User password
--force Skip all user interaction. Implied 'Yes' to all actions.
-q, --quiet Quiet (no output)
-l, --log Print log to file
-s, --strict Exit script with null variables. i.e 'set -o nounset'
-v, --verbose Output more information. (Items echoed to 'verbose')
-d, --debug Runs script in BASH debug mode (set -x)
-h, --help Display this help and exit
--version Output version information and exit
"
}
# print list of unavailable names/keywords
print_taken () {
lert "Sorry!" "The following can't be used for a name:\\n"
printf '%s \n' "${taken_names[@]}" | dent " " | column
}
# returns timestamp according to formats in config. Takes spacer string.
timestamp () {
local spacer="$1"
date +"$DATE_FORMAT$spacer$TIME_FORMAT"
}
# Record errors to log.
# * lineno not working? needs to be passed at the line?
error_log_it () {
printf "%b\\t%b\\t%b\\t%b\\t%b\\t%b\\t \\n" "ERROR" "line:$LINENO" "$USER" "$(timestamp " ")" "$1" "$2" >> "$ERROR_LOG_PATH"
}
# Record history of commands to file when log enabed.
log_it () {
if $LOG_HISTORY; then
printf "%b\\t%b\\t%b\\t%b\\t%b\\t\\n" "$USER" "$(timestamp "\\t")" "$1" "$2" "$3" >> "$LOG_PATH"
fi
}
# Record history of commands for a project. this file identifies a project.
update_dotfile () {
local name="$1"
proj_log="${name}.plbox"
printf "%b\\t%b\\t%b\\t%b\\t%b\\n" "$(timestamp "\\t")" "$USER" "$2" "$3" "$4" >> "$proj_log"
}
#
# ===============================================================================
# Command functions
#
# List a directory: used for Envs and trash.
# cd to that directory for tab-completion.
# ls dirs, pipe to indent()
# tail formatting, print total number of records.
# * expand with wildcards at $2? --> have done a search
m_list () {
local path="$1"
local search="$2"
local count=""
#echo $path
ch_dir "$CD_TO_LS" "$path"
if [ "$search" = "" ]; then
ls_dirs true "$path" "d" | dent " " | pr -t -3 -w70
count="$(ls_dirs true "$path" "d" | wc -l | xargs)" #xargs for easy trim
else
ls_dirs true "$path" "d" | grep "$search" | pr -t -3 -w70
count="$(ls_dirs true "$path" "d" | grep "$search" | wc -l | xargs)"
fi
print_div
printf " %b* %b%b total" "$LC" "$NC" "$count"
printf "%b For expanded commands and options:%b ve help\\n" "$TC" "$NC"
new_line
}
# Source the venv script to activate virtual environment
# * think i'm capturing error correctly now.
m_load () {
local name="$1"
local p_path="${PROJS_PATH}${1}/"
source "${VENVS_PATH}${name}/bin/activate"
error_chk "Unable to load:" "Loaded:" "$name" || return 1
#echo "$p_path" #testing
ch_dir "$CD_ON_LOAD" "$p_path" || return 1
return 0
}
# cd to project name taken, or the current env if empty
m_cd () {
local name="$1"
get_current_env # sets $current_env.
if [ "$name" = "" ]; then
command cd "${PROJS_PATH}${current_env}"
error_chk "Unable to cd:" "Loaded:" "$current_env" && return 0 || return 1
else
command cd "${PROJS_PATH}${name}"
error_chk "Unable to cd:" "Loaded:" "$name" && return 0 || return 1
fi
}
# Create new environment consisting from these elements and options:
# - venv python3 virtual environment: load on create
# - project folder: can be populated by contents of a default
# - sublime text project and workspace files: customized
# TODO: decide about using a trip, vs rollback
m_create () {
local name="$1"
local template="$2"
local list_of_temps=(${2})
local editor_file_path
# Check name given is ok.
if array_contains "$name" "${taken_names[@]}"; then #TODO -make own func?
lert "Forbidden name/keyword:" "$name"
print_taken "$name"
return 1
fi
# Create project folder, check for template arg
make_dir "$MAKE_PROJ_FOLDER" "$proj_path" || return 1
if [ "$template" = "" ]; then
copy_file "$USE_TEMPLATES" "$templ_default" "$proj_path" || return 1
else
for var in ${list_of_temps[@]}; do # copy all templates
#echo "-$var-" #testing
templ_path="${TEMPLATES_PATH}${var}/"
copy_file "$USE_TEMPLATES" "$templ_path" "$proj_path" || return 1
done
#copy_file "$USE_TEMPLATES" "$templ_path" "$proj_path" || return 1
fi
# Copy editor files, replace strings in files
for filename in $EDITOR_TEMPLATES_PATH*; do
extension="${filename##*.}"
editor_file_path="${EDITOR_PATH}${name}.${extension}"
copy_file "$MAKE_EDITOR_FILES" "$filename" "$editor_file_path" || return 1
sed -i "" "s/${EDITOR_TEMPLATE_STRING}/${name}/g" "$editor_file_path"
error_chk "Unable to update editor template with sed." || return 1
done
# Create VE and load depending
python3 -m venv "$env_path"
error_chk "Unable to create VE:" "Created Python3 venv:" "$name" || return 1
[ "$LOAD_ON_CREATE" = true ] && m_load "$name"
ch_dir "$CD_ON_CREATE" "$proj_path" || return 1
# Update pip and install requirements
upgrade_pip
reqs_pip "$reqs_default"
# Git init, and git commit
git_init "$name" || return 1
succ "Created VE:" "$name"
print_div
new_line
return 0
}
#
# Actual delete
# seperated delete from remove
# all editor files by "${name}." are deleted, might remove wrong files
# ex: ve delete env_name
# "tim_test.file" "blahwtest.txt" "test.slammajamma" all deleted
m_delete () {
local name="$1"
lert "Files will be Deleted. Continue?" "(y/n) " true
if yes_no; then # system delete
remove_file "$RM_PROJ_FILES" "$proj_path" || return 1
remove_file "$RM_VENV_FILES" "$env_path" || return 1
find "$EDITOR_PATH" -type f -name "${name}.*" -delete
error_chk "Unable to delete editor files:" "Removed:" "${EDITOR_PATH}${name}.*"
succ "Deleted VE:" "$name"
fi
}
# Copy files to script trash, or if trash is not enabled, system remove.
# Ask if user if sure?
# return on any issue, at issue.
# * currently it stops on fail anywhere along the long, maybe it shouldn't
# * what behaviour do i want here?
m_remove () {
local name="$1"
if [ "$COLLECT_TRASH" = true ]; then # Send to trash
mkdir "$trash_venv" "$trash_proj" "$trash_edit"
error_chk "Unable to create trash directories" || return 1
copy_file "$RM_PROJ_FILES" "${proj_path}" "$trash_proj" || return 1
remove_file "$RM_PROJ_FILES" "$proj_path" || return 1
copy_file "$RM_VENV_FILES" "${env_path}" "$trash_venv" || return 1
remove_file "$RM_VENV_FILES" "$env_path" || return 1
find "$EDITOR_PATH" -type f -name "${name}.*" -exec cp {} "$trash_edit" \;
error_chk "Unable to copy editor files:" "Coppied:" "${EDITOR_PATH}${name}.*"
find "$EDITOR_PATH" -type f -name "${name}.*" -delete
error_chk "Unable to remove editor files:" "Removed:" "${EDITOR_PATH}${name}.*"
succ "Removed VE:" "$name"
else
m_delete "$name"
fi
}
# Make a full copy of env
# * need to test sed for error
m_copy () {
local old="$1"
local new="$2"
# editor files - done with once created? or fix this, or change scheme
#copy_file "$MAKE_SUBL_FILES" "${subl_proj_path}" "${SUBL_PROJS_PATH}${new}.sublime-project"
#sed -i "" "s/$old/$new/g" "${SUBL_PROJS_PATH}${new}.sublime-project"
#copy_file "$MAKE_SUBL_FILES" "${subl_work_path}" "${SUBL_PROJS_PATH}${new}.sublime-workspace"
#sed -i "" "s/$old/$new/g" "${SUBL_PROJS_PATH}${new}.sublime-workspace"
copy_file "$MAKE_PROJ_FOLDER" "${proj_path}" "${PROJS_PATH}${new}"
copy_file "true" "${env_path}" "${VENVS_PATH}${new}"
}
# Copy full env from the trash back.
# * integrate comments below
# TODO: decide about using a trip, vs rollback
m_restore () {
local name="$1"
local trip=false #in the event of any folder not copy, don't delete?
if [ "$COLLECT_TRASH" = true ]; then # restore project from trash
find "$trash_edit" -type f -name "${name}.*" -exec cp {} "$EDITOR_PATH" \;
error_chk "Unable to copy editor files:" "Coppied:" "$trash_edit" #"${EDITOR_PATH}${name}.*"
copy_file "$RM_PROJ_FILES" "$trash_proj" "$proj_path" || trip=true
copy_file true "$trash_venv" "$env_path" || trip=true
if [ $trip = false ]; then # delete trash folder
remove_file "true" "$trash_path"
succ "Restored:" "$name"
else
lert "Something happened, kept in trash:" "$name"
fi
else
lert "Trash disabled, unable to restore:" "$name"
fi
}
# detailed print of commands and options
# * more?
m_help () { #change help and history func names?
print_help2
}
# Print command history -if kept- with the length: default(10), "user defined", all
m_history () {
local amount="$1"
case $amount in
"")
start_temp true "Command history: " "$DEFAULT_HIST"
tail "-n$DEFAULT_HIST" "$LOG_PATH" | dent
#these are not really checking tail/cat, just indent
error_chk "Unable to print history:" "" "$DEFAULT_HIST"
;;
-a | a | --all | all)
start_temp true "Command history: " "COMPLETE"
cat "$LOG_PATH" | dent
error_chk "Unable to print history:" "" "COMPLETE"
;;
*)
start_temp true "Command history: " "$1"
tail "-n$1" "$LOG_PATH" | dent
error_chk "Unable to print history:" "" "$1"
;;
esac
print_div
}
m_pip () {
local path="$1"
local name="$2"
local list_of_reqs=(${2})
# first check reqs dir, if no, try to install invidual package
ch_dir true "$path"
if [ "$name" = "" ]; then
ls_dirs true "$path" "f" | dent ' ' | pr -t -3 -w70
new_line
pip list | dent ' '
else
for var in ${list_of_reqs[@]}; do # install all reqs
#echo "-$var-" #testing
pip install -r "$var" #| dent ' ' #killed errorchk
error_chk "Unable to install requirements file:" "Pip installed:" "$var"
done
fi
}
#
# ===============================================================================
# Main
#
# main branch for script.
# take arguments and route them to appropriate functions.
# print start templates when verbose.
# on blank command, print ve directory and useful commands.
main () {
if [ "$1" != "" ]; then
case $1 in
-a | a | --archive | archive)
#tar -czvf "${ARCH_PATH}${2}.tar.gz" "/var/config"
;;
-c | c | --create | create)
start_temp "$VERBOSE" "Creating new Virtual Env: " "$2"
#m_create "$@" || return 1
m_create "$2" "$3" || return 1
update_dotfile "$proj_path" "$1" "$2" "$3" || return 1
;;
-cd | cd | --chdir | chdir)
m_cd "$2"
;;
-cpy | cpy | --copy | copy) # 'cp' conflicts
start_temp "$VERBOSE" "Copying VE: " "$2 to $3"
m_copy "$2" "$3"
update_dotfile "${PROJS_PATH}${3}/" "$1" "$2" "$3"
;;
-delete | delete | --delete)
start_temp "$VERBOSE" "Deleting Virtual Env: " "$2"
m_delete "$2"
;;
-h | h | --help | help)
start_temp true "Help: " "commands and options"
m_help
;;
-hs | hs | -hist | hist | --history | history)
# start_temp is variable, so in history function
m_history "$2"
;;
-l | l | ls | --list | list) #"l" and single letters not working, alias??
start_temp true "VE Directory: " "$VENVS_PATH"
m_list "$VENVS_PATH" "$2"
;;
-o | o | --open | open)
find "$EDITOR_PATH" -type f -name "${2}.*" -exec subl {} \;
;;
-p | p | -pip | pip | --pip)
start_temp true "Requirements: " "$REQS_PATH"
m_pip "$REQS_PATH" "$2"
end_temp
;;
--paths | paths)
start_temp true "Paths:" "check 'em while you got 'em"
check_paths "false"
;;
-r | r | --restore | restore)
start_temp "$VERBOSE" "Restoring Virtual Env: " "$2"
m_restore "$2"
update_dotfile "$proj_path" "$1" "$2"
;;
-rm | rm | --remove | remove)
start_temp "$VERBOSE" "Removing Virtual Env: " "$2"
m_remove "$2"
[ "$COLLECT_TRASH" = true ] && update_dotfile "$trash_proj" "$1" "$2" "$3"
end_temp
;;
-s | s | --search | search)
start_temp true "Searching Trash for: " "$2"
m_list "$TRASHCAN_PATH" "$2"
start_temp true "Searching Virtual Envs for: " "$2"
m_list "$VENVS_PATH" "$2"
;;
-t | t | temp | -temp | --templates)
start_temp true "Templates Directory: " "$TEMPLATES_PATH"
m_list "$TEMPLATES_PATH"
;;
-trash | --trash | trash)
start_temp true "Trash Directory: " "$TRASHCAN_PATH"
m_list "$TRASHCAN_PATH" "$2"
;;
-v | v | --version | version)
printf "%s\\n" "$VERSION"
;;
*) # load env given
m_load "$1"
;;
esac
else
start_temp true "VE Directory: " "$VENVS_PATH"
m_list "$VENVS_PATH"
print_help_short
fi
}
# -------------------------------------------------------------------------------
# check paths, false is for verbose.
# run main, pass args.
# log command. (((add $3)))
#
# if config paths bad, exit
check_paths "true" || return 1
# log next?
log_it "$1" "$2" "$3"
# run main
main "$1" "$2" "$3"
#main "$@"