-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcde.sh
More file actions
851 lines (724 loc) · 18.7 KB
/
cde.sh
File metadata and controls
851 lines (724 loc) · 18.7 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
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
#! /usr/bin/env bat
export RED="\033[0;31m"
export GREEN="\033[0;32m"
export BLUE="\033[0;34m"
export LIGHT_RED="\033[1;31m"
export LIGHT_GREEN="\033[1;32m"
export LIGHT_BLUE="\033[1;34m"
if [[ $0 == "$BASH_SOURCE" ]]
then
printf "$GREEN"
printf "This file should be sourced like this"
printf "$ . cde.sh"
printf "$ source cde.sh"
printf "$ whyp_source cde.sh"
printf
printf "$RED"
printf "It should not be run like this" >&2
printf "$ sh cde.sh" >&2
printf "$ bash cde.sh" >&2
exit 1
fi
export CDE_SOURCE=$(readlink -f "$BASH_SOURCE") # .../cde.sh
export CDE_NAME=$(basename "$CDE_SOURCE") # cde.sh
export CDE_DIR=$(dirname "$CDE_SOURCE") # /.../
# x
# xx
., () {
local destination_=$HOME
local top_level_=$(quietly git rev-parse --show-toplevel)
[[ $top_level_ ]] && destination_=$top_level_
}
.c () {
source "$CDE_SOURCE"
}
alias ..=cdup
# xxx
cdd () {
local __doc__="""cde here"""
cde $(here)
}
# rule 1: Leave system commands alone
# So this uses the next key up from "cd"
cde () {
local __doc__="""find a dir and handle it"""
if [[ ! "$@" ]]; then (
cde -q "$CDE_DIR"
cde -
cde --show-known
)
return 0
fi
if [[ $1 =~ ^-$ ]]; then
cd -
cde .
return 0
fi
local quietly_=
[[ $1 =~ -h ]] && cde_help && return 0
[[ $1 =~ -a ]] && python_cde "$@" && return 0
[[ $1 =~ -q ]] && quietly_=-q && shift
if [[ $1 =~ ^[.]$ ]]; then
cdd
return $?
fi
pre_cdpy $quietly_
cdpy "$@" || return 1
[[ -d . ]] || return 1
post_cdpy $quietly_
}
#
# This one is way out-of-order
# Put here to catch the eye of anyone looking at `cde ()`
# Which eventually runs `cdpy` above here
# And runs lotsa checks before reaching
# $ run_cde python3 "$@"
#
# So, to debug:
# 1. Show any bash debugging tips
# 2. Ignore all the checks
# 3. Change the interpreter for cde from `python3` to `pudb`
#
cdg () {
if [[ -z "$1" ]]; then
qt whyp && whyp cdg
return $?
fi
(set -x
local __doc__="""debug cde"""
[[ $1 =~ -h ]] && cde_help >&2 && return 1
[[ $1 =~ -q ]] && echo 👿 >&2 && shift
[[ $1 =~ ^[.]$ ]] && cdd
pre_cdpy
pudb_cde "$@"
# [[ -d . ]] && post_cdpy || echo ". is not a dir! 😳" >&2
)
}
cdgg () {
# KISS
PYTHONPATH=/opt/clones/github/jalanb/cds/cde:/opt/clones/github/jalanb/bashrcs/jab/src/python/site \
/opt/clones/github/jalanb/cds/cde/.venv/bin/pudb -m cde \
"$@"
}
cdl () {
local __doc__="""cde $1; ls [-a]"""
local dir_="$@"
[[ $dir_ ]] || dir_=.
shift
local ls_options_="$@"
[[ $ls_options_ ]] || ls_options_=" -a "
# cde $dir_
cdr $dir_
ls --color $_ls_options "$@"
}
cdq () {
QUIETLY cde -q "$@"
}
cdr () {
cdq "$@"
[[ -d . ]] || return 1
green_line $(here)
return 0
}
cdv () {
cde "$1" || return 1
shift
local files_=$(basename "$@")
[[ $files_ ]] || files_=$(ls -1a)
[[ $files_ ]] || files_=$PWD
[[ "$@" ]] && files_="$@"
[[ $files_ ]] && $EDITOR -p $files_
}
cpp () {
local __doc__="""Show where any args would cde to"""
if [[ -n "$1" ]]; then
python_cde "$@"
else
python_cde .
fi | grep -v -- '->'
}
cls () {
local __doc__="clean, clear, ls"
clear
cl "$@"
[[ "$@" ]] || echo
}
ind () {
local cd_=cd
if [[ $1 == "ind" ]]; then
cd_="cde -q"
shift
fi
local destination_=$(python_cde "$1")
[[ -d "$destination_" ]] || return 1
shift
(
cd "$destination_"
"$@"
)
}
mkc () {
local destination_=$(cde_first "$@")
[[ -d "$destination_" ]] || mkdir -p "$destination_"
cde "$destination_"
}
alias ...="cdup 2"
# xxxx
cdpy () {
local __doc__="""pushd to cde.py's destination"""
[[ $1 == -h || $1 == --help ]] && python_cde --help && return 0
local no_stderr_= no_stdout_=
[[ $1 =~ -q ]] && no_stdout_=-q && shift
[[ $1 =~ -Q ]] && no_stdout_=-Q && no_stderr_=-Q && shift
local cde_error_= cde_output_=$(python_cde "$@")
local cde_return_=$?
[[ $cde_return_ == 0 ]] || cde_error_="cde error: $cde_return_"
if [[ $cde_error_ ]]; then
if [[ $cde_output_ =~ (^$|^Error|Try.again|^[uU]sage) ]]; then
q_echo $no_stderr_ "$cde_output_"
return 1
fi
[[ $cde_output_ =~ ^[uU]sage ]] && q_echo $no_stderr_ $(python_cde --help)
[[ "$@" =~ -[lp] ]] && q_echo $no_stderr_ "$cde_output_"
return 0
fi
[[ $cde_output_ ]] || return $?
python_cde --add $cde_output_
same_path . "$cde_output_" && return 0
local cde_directory_="$cde_output_" readlink_directory_=$(rlf $cde_output_)
same_path . "$readlink_directory_" && return 0
local cdpy_output_="cd $cde_output_"
[[ "$cde_directory_" != "$readlink_directory_" ]] && cdpy_output_="cd ($cde_directory_ ->) $readlink_directory_"
[[ $no_stdout_ ]] || echo $cdpy_output_
pusq "$cde_directory_"
return 0
}
cdrl () {
cdr "$@" || return 1
lo
}
cdrr () {
cdr "$@" || return 1
llr
}
cdup () {
local __doc__="""cde up a few levels, 'cdup' goes up 1 level, 'cdup 2' goes up 2"""
local level_=1
if [[ $1 =~ [1-9] ]]; then
level_=$1
shift
fi
local dir_=$(rlf ..)
pushd >/dev/null 2>&1
while true; do
level_=$(( $level_ - 1 ))
[[ $level_ -le 0 ]] && break
cd ..
dir_=$(here)
done
popd >/dev/null 2>&1
cde $dir_ "$@"
}
here () {
rlf .
}
popq () {
QUIETLY popd
}
pusq () {
QUIETLY pushd "$@"
}
pycd () {
# Adapted from https://news.ycombinator.com/item?id=18898898
local __doc__="""cde to directory of a iven Python module"""
cde $($(python_) -c "import os.path, $1; print(os.path.dirname($1.__file__))");
}
alias ....="cdup 3"
# xxxxx
cdupp () {
local """cd up 2 levels"""
cdup 2 "$@"
}
alias .....="cdup 4"
# xxxxxx
cde_ok () {
local __doc__="""Whether cde would go anywhere"""
[[ -z "$@" ]] && return 1
[[ -d $(cde_first "$@") ]]
}
cduppp () {
local """cd up ... etc, you get the idea"""
cdup 3 "$@"
}
q_echo () {
[[ $1 =~ -[qQ] ]] && return 0 || shift
echo "$@"
}
# xxxxxxx
is_type () {
[[ $1 ]] || return 1
QUIETLY type "$@"
}
whichly () {
quietly which "$@"
}
python_ () {
which python || which python3
}
quietly () {
"$@" 2>/dev/null
}
QUIETLY () {
"$@" >/dev/null 2>&1
}
vim_cde () {
local dot_cd_=
[[ -f .cd ]] && dot_cd_=.cd
[[ -f .cd.sh ]] && dot_cd_="$dot_cd_ .cd.sh"
vim -p $dot_cd_ "$CDE_SOURCE" "${CDE_DIR}" "$@"
. $CDE_SOURCE
. $dot_cd_
[[ -f .cd.sh ]] && bash .cd.sh
}
_active () {
local __doc__="""Whether the $ACTIVATE script is in same dir as current python or virtualenv"""
local activate_dir_=$(_dirnames $ACTIVATE)
local python_dir_=$(_dirnames $(rlf $(command -v python)))
same_path $activate_dir_ $python_dir_ && return 0
local venv_bin_="$VIRTUAL_ENV/bin"
same_path $activate_dir_ $venv_bin_
}
# xxxxxxxx
pre_cdpy () {
cde_deactivate
[[ -n $CDE_header ]] && echo $CDE_header
}
Quietly () {
"$@" >/dev/null
}
quietly () {
"$@" 2>/dev/null
}
QUIETLY () {
"$@" >/dev/null 2>/dev/null
}
qt () {
QUIETLY type "$1"
}
cl () {
local dir_=.
test -d "$1" && dir_="$1"
local cmd_=ls
qt l && cmd_="l"
$cmd_ "$dir_"
}
cde_help () {
echo "cd to a dir and react to it"
echo
cdpy -h
return 0
}
headline () {
[[ $1 ]] && head -n 1 "$1" || cat | head -n 1
}
is_command () {
qt "$1"
}
try_command () {
is_command $1 || return 1
"$@"
}
pudb_cde () {
local __doc__="""Debug the cde program"""
local debugger_=$(venv_app pudb)
[[ $debugger_ ]] || debugger_=$(venv_app pudb3)
[[ $debugger_ ]] || return 1
[[ -e $debugger_ ]] || return 2
run_cde $(basename "$debugger_") "$@" || echo "Failed to run $debugger_" >&2
}
# xxxxxxxxx
echo_dir () {
[[ -e $1 ]] || return 1
[[ -d "$1" ]] && echo $1 || rld "$1"
}
echo_dirs () {
local dirs_=
for dir in "$@"; do
[[ -d $dir ]] && dirs_="$dirs_ $dir"
done
[[ $dirs_ ]] || return 1
echo $dirs_
return 0
}
new_dot () {
[[ -f .cd ]] && return 0
echo "#! /usr/bin/env bash" > .cd
echo "" >> .cd
[[ -d bin ]] && echo "cde_dot_bin" >> .cd
[[ -d .git ]] && echo "cde_dot_git" >> .cd
cde_dot_python && echo "cde_dot_venv" >> .cd
echo "cde_dot_ls && cde_dot_clean" >> .cd
echo "#! /usr/bin/env bash" > .cd.sh
echo "" >> .cd.sh
echo $(here) >> .cd.sh
}
dot_cd () {
local __doc__="""Look for .cd here and source it if found"""
local dot_cd_= cd_sh_=
[[ -f .cd ]] && dot_cd_=.cd
[[ -f .cd.sh ]] && cd_sh_=".cd.sh"
[[ -f $dot_cd_ ]] || return 1
grep -q activate $dot_cd_ && cde_deactivate
. $dot_cd_
[[ -f $cd_sh_ ]] && bash $cd_sh_
}
say_path () {
local path_=$PWD
[[ $path_ == "$HOME" ]] && path_=HOME
[[ $path_ =~ "wwts" ]] && path_="${path_/wwts/dub dub t s}"
[[ $path_ ]] || return 1
local said_=$($(python_) << EOP
import os, sys
path=os.path.expanduser(os.path.expandvars('$path_'))
home='%s/' % os.path.expanduser('~')
if path.startswith(home):
out=path.replace(home, 'home ')
elif path[0] == '/':
out='root %s' % path[1:]
else:
out=path
replacements = (
('/wwts', '/dub dub t s'),
)
for old, new in replacements:
out = out.replace(old, new)
sys.stdout.write(out.replace('/', ' '))
EOP
)
is_type sai && sai "$said_"
}
post_cdpy () {
[[ $1 =~ -q ]] && shift || say_path $path_
# new_dot
dot_cd
}
quiet_out () {
"$@" >/dev/null
}
same_path () {
[[ $1 ]] && [[ ! $2 ]] && return 1
[[ ! $1 ]] && [[ $2 ]] && return 2
[[ $(rlf "$1") == $(rlf "$2") ]]
}
# xxxxxxxxxx
_activate () {
unhash_python_handlers
[[ -f $ACTIVATE ]] && . $ACTIVATE
}
_dirnames () {
local __doc__="""show dirnames for all args that are paths"""
local result_=1
for arg_ in "$@"; do
[[ -e "$arg_" ]] || continue
dirname "$arg_"
result_=0
done
return $result_
}
cde_dot_venv () {
cde_activate_here && return 0
local venvs_=$HOME/.virtualenvs
[[ -d $venvs_ ]] || return 0
local here_=$(rlf $(pwd))
local name_="not_a_name"
[[ -e $here_ ]] && name_=$(basename $here_)
[[ $name_ == "not_a_name" ]] && return 1
local venv_=
for venv_ in $venvs_/*; do
[[ -d "$venv_" ]] || continue
local venv_name_=$(basename "$venv_")
if [[ $venv_name_ == $name_ ]]; then
local venv_activate_="$venv_/bin/activate"
[[ -f "$venv_activate_" ]] || return 1
. "$venv_activate_"
return 0
fi
done
return 0
}
run_cde () {
local __doc__="""Run the cde script, setting PYTHONPATH"""
local app_=$(venv_or_which "$1") || return 1
shift
local command_="$app_ -m cde"
local python_path_="$CDE_DIR"
[[ $PYTHONPATH ]] && python_path_="$python_path_:$PYTHONPATH"
if [[ $app_ =~ venv[/] ]]; then
(
source "$(dirname $app_)/activate"
PYTHONPATH="$python_path_" "$app_" -m cde "$@"
)
else
PYTHONPATH="$python_path_" "$app_" -m cde "$@"
fi
}
python_cde () {
local __doc__="""Run the cde program"""
run_cde python3 "$@"
}
cde_dot_clean () {
for path in $(find . -type f -name '*.sw*'); do
quietly ls -l $path
quietly rm -f $path && continue
[[ $? == 0 ]] || break
done
}
# xxxxxxxxxxx
_deactivate () {
# Thanks to @nxnev at https://unix.stackexchange.com/a/443256/32775
cde_deactivate
if [[ $ACTIVE_PYTHON ]]; then
ACTIVATE="${ACTIVE_PYTHON/%python/activate}"
_activate
fi
}
cde_template () {
echo -n "$1/cd "
return 0
}
# xxxxxxxxxxxx
cde_dot_python () {
local result_=1
[[ -f .venv/bin/activate ]] && result_=0
any_python_scripts_here || return $result_
local dir_=$(here)
local dir_name_=$(basename $dir_)
python_project_here $dir_name_ || return $result_
prune_python_here
cde_activate_here
local egg_info=${dir_name_}.egg-info
[[ -d $egg_info ]] && rm -rf $egg_info
return $result_
}
bin_template () {
[[ -d bin ]] || return 1
echo -n "$1/bin "
return 0
}
git_template () {
[[ -d .git ]] || return 1
echo -n "$1/git "
return 0
}
# xxxxxxxxxxxxx
venv_app () {
quietly ls "${CDE_DIR}/.venv/bin/$1"
}
venv_or_which () {
local __doc__="""find an executable in cde's virtualenv, or which, or which with our PATH"""
[[ "$1" ]] || return 1
local name_="$1"; shift
local app_="${CDE_DIR}/.venv/bin/$name_"
[[ -x "$app_" ]] || app_=$(quietly which $name_)
[[ -x "$app_" ]] || app_=$(PATH=~/bin:/usr/local/bin:/bin:/usr/bin quietly which $name_)
is_type $app_ || return 1
echo $app_
return 0
}
# xxxxxxxxxxxxxx
venv_dirs_here () {
echo_dirs .venv/bin venv/bin bin
}
rld () {
[[ -e "$1" ]] || return 1
quietly dirname $( rlf "$1" )
}
rlf () {
local path_=.
[[ -e "$1" ]] && path_="$1"
quietly readlink -f "$path_"
}
echo_venv_directory_from () {
if echo_dir "$1" || echo_dir "~/.virtualenvs/$1"; then
return 0
fi
return 1
}
# xxxxxxxxxxxxxxx
python_template () {
local python_template_="$1/python"
local echo_template=
[[ $(find . -maxdepth 7 -name __init__.py | wc -l) -gt 3 ]] && echo_template="$python_template_"
[[ $(find . -maxdepth 2 -name activate | wc -l) -gt 1 ]] && echo_template="$python_template_"
[[ $echo_template ]] || return 1
echo -n "$echo_template "
return 0
}
unhash_handlers () {
# Thanks to @nxnev at https://unix.stackexchange.com/a/443256/32775
local result_=1
for arg in "$@"; do
quietly hash -d $arg && result_=0
done
return $result_
}
# xxxxxxxxxxxxxxxx
cat_cde_templates () {
local _template_dir="$CDE_DIR/templates"
cat $(cde_template "$_template_dir")
local _template=
for method in bin git $(python_) ; do
template_=$(${method}template_ "$template_dir_")
[[ $template_ ]] || continue
local cat_=cat
head -n 1 $template_ | grep -q '^#!' && cat_="tail -n +2 "
$cat_ $template_ | grep -v ^$
done
}
# xxxxxxxxxxxxxxxxx
project_venv_dirs () {
local top_dirs_=
local git_top_=$(quietly git rev-parse --show-toplevel . | head -n 1)
[[ -d $git_top_ ]] && top_dirs_="$git_top_/venv/bin $git_top_/.venv/bin $git_top_/bin"
echo_dirs $top_dirs_
}
prune_python_here () {
local here_=$(here)
local home_=$(rlf $HOME)
[[ ${here_:0:${#home_}} == $home_ ]] || return 1
[[ ${#here_} == ${#home_} ]] && return 1
rf -qpr
return 0
}
unhash_deactivate () {
unhash_python_handlers
[[ $VIRTUAL_ENV ]] && deactivate
}
# xxxxxxxxxxxxxxxxxx
# xxxxxxxxxxxxxxxxxxx
python_project_here () {
local __doc__="""Recognise python project dir by presence of known files/dirs"""
local dirname_="$1"
[[ -f setup.py || -f requirements.txt || -f bin/activate || -d ./$dir_name_ || -d .venv || -d .idea ]]
}
# xxxxxxxxxxxxxxxxxxxx
unhash_file_handlers () {
unhash_handlers rm cp mv cat
}
# xxxxxxxxxxxxxxxxxxxxx
# xxxxxxxxxxxxxxxxxxxxxx
unhash_python_handlers () {
# Thanks to @nxnev at https://unix.stackexchange.com/a/443256/32775
unhash_handlers python python2 python3 ipython ipython2 ipython3 pudb pudb3 pdb ipdb pip pip2 pip3
}
# xxxxxxxxxxxxxxxxxxxxxxx
any_python_scripts_here () {
[[ $(find . -type f -name "*.py" -exec echo 1 \; -quit) == 1 ]]
}
# xxxxxxxxxxxxxxxxxxxxxxxx
cde_test_activate_scripts () {
for python_root_ in "$@"; do
[[ -f ${python_root_}/bin/activate ]] && activate_=${python_root_}/bin/activate
[[ -f ${python_root_}/activate ]] && activate_=${python_root_}/activate
if [[ -f "$activate_" ]]; then
echo "$activate_"
return 0
fi
done
return 1
}
cde_find_activate_script () {
local __doc__="""find an activate script in $1 (path to venv, or activate)"""
local python_roots_= activate_=
if [[ -f "$1" && $(basename "$1") == "activate" ]]; then
activate_="$1"
shift
else
[[ "$@" ]] && python_roots_=$(echo_venv_directory_from "$@" )
[[ $python_roots_ ]] || python_roots_="$(pwd) $(venv_dirs_here) $(project_venv_dirs)"
[[ $python_roots_ ]] || echo "No dirs available to find activate scripts" >&2
[[ $python_roots_ ]] || return 1
local activate_=$(cde_test_activate_scripts $python_roots_) || return 1
fi
# [[ -f $activate_ ]] || return 1
ACTIVATE="$(rlf $activate_)"
export ACTIVATE
test -f $ACTIVATE
}
#
# After here functions are intended for use in .cd scripts
#
cde_dot_activate () {
cde_activate_venv
}
cde_activate_venv () {
cde_deactivate
local bin_=.venv/bin
if [[ ! -d $bin_ ]]; then
if [[ -d "$1" ]]; then
if [[ $(basename "$1") == "bin" ]]; then
bin_="$1"
elif [[ -d "$1/bin" ]]; then
bin_="$1/bin"
else
return 1
fi
else
return 1
fi
fi
. $bin_/activate
$bin_/python -V
}
cde_activate_here () {
cde_find_activate_script . || return 1
_active && cde_deactivate
_activate
}
cde_activate_home () {
cde_activate_there ~/.virtualenvs/$1
}
cde_activate_there () {
cde_find_activate_script "$@" || return 1
_active && cde_deactivate
_activate
}
cde_deactivate () {
try_command deactivate 2>&1 | grep -q "deactivate must be sourced" && source deactivate 2>/dev/null
unhash_python_handlers
}
cde_dot_git () {
[[ -d ./.git ]] || return 0
blue_line "$(git remote get-url origin) <$(git config user.name) $(git config user.email)>\n"
git status .
# git rev-parse --abbrev-ref HEAD
echo
[[ -f ".bumpversion.cfg" ]] && grep ^current_version .bumpversion.cfg | grep --colour '\d[0-9a-z.]\+$'
echo
git log -n 8 --color=always --decorate --oneline --graph --abbrev-commit --date=relative --pretty=format:'%Cgreen%cr%Creset, %C(blue)%aN%Creset,%C(auto)%d%Creset %C(auto)%h "%s"'
return 0
}
cde_dot_bin () {
local __doc__="""Adds .cd/../bin to PATH"""
local bin_path_=$(quietly cd bin && pwd)
[[ -d "$1" ]] && bin_path_="$1"
[[ -d "$bin_path_" ]] || return 2
[[ $PATH =~ "${bin_path_//\//.}" ]] && return 0
PATH="$bin_path_:$PATH"
export PATH
}
cde_PYTHONPATH () {
local __doc__="""Adds .cd/.. to PYTHONPATH"""
local here_=$(pwd)
[[ $PYTHONPATH =~ ${here_//\//.} ]] && return 0
[[ $PYTHONPATH ]] && PYTHONPATH="$PYTHONPATH:$here_" || PYTHONPATH="$here_"
export PYTHONPATH
}
cde_clean_eggs () {
rm -rf *.egg-info
}
# And that's nearly complete now
_cde_complete() {
COMPREPLY=($(compgen -d -- "${COMP_WORDS[1]}"))
}
complete -F _cde_complete cde