forked from Wabri/TemplateRepositoryPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.sh
More file actions
150 lines (137 loc) · 3.32 KB
/
source.sh
File metadata and controls
150 lines (137 loc) · 3.32 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
#!/bin/sh
ROOT=$(pwd)
_sep_echo(){
echo '-----> '$1
}
_help(){
echo 'Usage: ./source.sh [OPTION]'
echo ''
echo 'Mandatory arguments to long options are mandatory for short options too.'
echo ''
echo ' -h, --help \t\t\t Print help page'
echo ''
echo ' -q, --quiet \t\t\t Quiet mode with no output'
echo ''
echo ' -n, --name NAME\t\t Specify the name of virtual environment'
echo ''
echo ' -p, --package NAME\t\t Specify the name of the package'
echo ''
echo ' -c, --cache \t\t\t Use environment if already exists'
echo ''
}
_venv_create() {
rm -rf $1 &>/dev/null
python3 -m venv $1
}
_source() {
cache=$3
package_path=$2
environment_name=$package_path/$1
tools=$ROOT/scripts/development/tools
# Install python3 dependencies
_sep_echo 'Install python3 venv and pip dependencies'
sudo apt install python3-venv
sudo apt install python3-pip
# Create Environment
_sep_echo 'Deactivate precedent environment'
if type deactivate &>/dev/null; then
deactivate
else
echo 'There are not any environment activated'
fi
if [ $cache -eq 0 ] ;
then
_sep_echo 'Setting up virtual environment'
echo 'Create environment with name '$environment_name
_venv_create $environment_name
elif [ $cache -eq 1 ]
then
if [ ! -f $environment_name/bin/activate ] ;
then
_sep_echo 'Environment not exists, create new one'
echo 'Create environment with name '$environment_name
_venv_create $environment_name
else
_sep_echo 'Cashing environment'
echo 'Using environment with path: '$environment_name
_venv_create $environment_name
fi
fi
# Activate up Environment
_sep_echo 'Activate Environment'
. $environment_name/bin/activate
echo 'The python environment is in path '$(which python)
echo 'The pip environment is in path '$(which pip)
# Upgrade pip and install requirements
_sep_echo 'Upgrade pip'
pip install --upgrade pip
_sep_echo 'Install requirements of project'
pip install -r $package_path/requirements.txt
# Create aliases
_sep_echo 'Create aliases'
if [ "$package_path" = "$ROOT" ] ; then
alias run='$tools/run.sh --package '
else
run_file_package=$package_path/run.py
alias run='$tools/run.sh --main $run_file_package'
fi
if [ "$package_path" = "$ROOT" ] ; then
alias requirement='$tools/pip-update-requirements.sh --requirements $ROOT/requirements.txt'
else
alias requirement='$tools/pip-update-requirements.sh --requirements $package_path/requirements.txt'
fi
}
environment_name=venv
package_path=$ROOT
quiet=0
cache=0
error=0
while [ $# -ne 0 ] && [ $error -eq 0 ]
do
key="$1"
case $key in
--help|-h)
error=-1
;;
--quiet|-q)
quiet=1
shift
;;
--cache|-c)
cache=1
shift
;;
--name|-n)
environment_name=$2
shift
shift
;;
--package|-p)
package_path=$ROOT'/packages/'$2
if [ ! -d $package_path ]; then
echo 'The '$2' package not exists'
error=2
fi
shift
shift
;;
*)
echo 'Argument '$key' not valid'
error=1
esac
done
if [[ $error -eq -1 ]] ;
then
_help
elif [[ $error -eq 0 ]]
then
if [[ $quiet -eq 0 ]] ; then
_source $environment_name $package_path $cache
else
_source $environment_name $package_path $cache &>/dev/null
fi
else
echo 'ERROR '$error
echo 'Need help?'
echo 'Try using -h or --help arguments'
fi