๐Ÿ“ฆ yochem / venvs

๐Ÿ“„ venvs.bash ยท 82 lines
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
82venvs() {
	# check if XDG_DATA_HOME and venvsdir exist
	xdgdata="${XDG_DATA_HOME:-$HOME/.local/share}"
	venvsdir="$xdgdata/venvs"

	# if venvsdir is not a directory, remove the 'file' venvsdir and make dir
	if [ ! -d "$venvsdir" ]; then
		rm "$venvsdir" >/dev/null 2>&1
		mkdir "$venvsdir"
	fi

	command="$1"
	name="$2"
	shift
	shift

	case "$command" in
		'')
			source {.,}*/bin/activate >/dev/null 2>&1
			;;
		'new')
			[ -z "$name" ] && return
			if [[ "$name" = ./* ]]; then
				path="$name"
			else
				path="$venvsdir/$name"
			fi
			python3 -m venv "$path" $@ --upgrade-deps >/dev/null
			;;
		'ls'|'list')
			command ls -1 "$venvsdir"
			;;
		'rm'|'remove')
			[ -z "$name" ] && return
			if [ -z "$name" ]; then
				echo "no venv name given"
				return
			fi
			if [ ! -d "$venvsdir/$name" ]; then
				echo "no venv named $name"
				return
			fi
			command rm -rdI "$venvsdir/$name"
			;;
		'deactivate')
			deactivate
			;;
		'-h'|'--help')
			cat << END
venvs - wrapper for Python's venv

Gather all venvs in one folder and globally access them.

Global venvs directory:
$venvsdir
To move it, set \$XDG_DATA_HOME

Usage:
venvs <venv>                    activate venv in the *global venvs directory*
venvs new <venv> <options>      create new venv in *global venvs directory*
                                note: run python3 -m venv --help to see options

venvs                           activate venv in *current directory*
venvs new ./<venv> <options>    create new venv in *current directory*
                                note: run python3 -m venv --help to see options

venvs [ls | list]               list all *global* venvs
venvs [rm | remove] <venv>      remove a venv
venvs deactivate                deactivate current venv
venvs [-h | --help]             show this help
END
			;;
		*)
			if [ ! -d "$venvsdir/$command" ]; then
				echo "no venv named $command"
				return
			fi
			source "$venvsdir/$command/bin/activate"
			;;
	esac
}