This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppm
executable file
·219 lines (190 loc) · 4.56 KB
/
ppm
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
#!/bin/bash
# Author: Jack Jiang
######################## default configuration #########################
# hardcode items
CONFIG_FILE="package.txt"
# configurable items
DOCS="http://ppm.jackjyq.com"
MAIN_FILE="main.py"
DEFAULT_ARGS=""
PACKAGE_LIST="package-lock.txt"
PACKAGE_PATH="python_modules"
# auto generated items
CONFIG_PATH=""
PYTHON_PATH=""
DEFAULT_CMD=""
VERSION_NUMBER=""
VERSION_TIME=""
######################### helper functions #############################
show_usage() {
# show usage and exit
echo "USAGE: ppm <command>"
echo
echo "ppm --version"
echo "ppm install"
echo "ppm install <package> [<package> ... <package>]"
echo "ppm start"
echo "ppm start <Python_file> [<arguments>]"
echo "ppm uninstall <package> [<package> ... <package>]"
echo "ppm upgrade"
echo
exit 1
}
parse_configuration() {
PPM_PATH=$(dirname $0)
CONFIG_PATH="$(pwd)/$CONFIG_FILE"
if [ -e $CONFIG_PATH ]; then
source $CONFIG_PATH
fi
if [ $CONFIG_FILE == $PACKAGE_LIST ]; then
echo "PACKAGE_LIST can not be $CONFIG_FILE"
exit 1
fi
PYTHON_PATH="./$PACKAGE_PATH/bin/activate"
DEFAULT_CMD="$MAIN_FILE $DEFAULT_ARGS"
VERSION_NUMBER=`cd $PPM_PATH && git log --oneline | wc -l | sed -e 's/^[ \t]*//'`
VERSION_TIME=`cd $PPM_PATH && git log -1 --pretty=format:"�"`
}
check_and_add_gitignore(){
if ! [ -e ".gitignore" ]; then
echo "/$PACKAGE_PATH" >> ".gitignore"
fi
}
check_and_add_configuration() {
if ! [ -e $CONFIG_FILE ]; then
echo "DOCS=\"$DOCS\"" >> $CONFIG_FILE
echo "MAIN_FILE=\"$MAIN_FILE\"" >> $CONFIG_FILE
echo "DEFAULT_ARGS=\"$DEFAULT_ARGS\"" >> $CONFIG_FILE
echo "PACKAGE_LIST=\"$PACKAGE_LIST\"" >> $CONFIG_FILE
echo "PACKAGE_PATH=\"$PACKAGE_PATH\"" >> $CONFIG_FILE
fi
}
check_and_add_main() {
if ! [ -e $MAIN_FILE ]; then
echo "print('Hello World!')" >> $MAIN_FILE
fi
}
show_succeed_message() {
echo "Succeed!"
echo
echo "To start, please try:"
echo "ppm start"
echo
}
assert_valid_venv() {
if ! [ -e $PACKAGE_LIST ]; then
echo "$PACKAGE_LIST does not exist"
echo
echo "To fix, please try:"
echo "ppm install"
echo
exit 1
fi
if ! [ -e $PYTHON_PATH ]; then
echo "$PYTHON_PATH does not exist"
echo
echo "To fix, please try:"
echo "ppm install"
echo
exit 1
fi
}
####################### commands implementation ########################
install() {
if ! [ -e $PACKAGE_LIST ]; then
# create new virtual environment
touch $PACKAGE_LIST
check_and_add_gitignore
check_and_add_configuration
check_and_add_main
fi
if ! [ -e $PYTHON_PATH ]; then
python3 -m venv $PACKAGE_PATH
fi
source $PYTHON_PATH &&
pip install --upgrade pip &&
python3 -m pip install -r $PACKAGE_LIST &&
show_succeed_message
}
install_packages() {
packages=$*
assert_valid_venv
source $PYTHON_PATH &&
for package in $packages
do
pip install $package
done
python -m pip freeze > $PACKAGE_LIST
}
uninstall() {
packages=$*
assert_valid_venv
source $PYTHON_PATH &&
for package in $packages
do
pip uninstall $package
done
python -m pip freeze > $PACKAGE_LIST
}
start() {
commands=$*
assert_valid_venv
source $PYTHON_PATH &&
python $commands
}
show_version() {
echo "ppm $VERSION_NUMBER ($VERSION_TIME)"
}
upgrade() {
( cd $PPM_PATH && git pull ) &&
show_version
}
############################ main function #############################
# no argument
if [ $# = 0 ]; then
show_usage
fi
# read package.txt to overide the configuration
parse_configuration
# install
if [ $1 == "install" -o $1 == "i" ]; then
if [ $# = 1 ]; then
install
else
install_packages ${*:2} # all args except the first one
fi
# start
elif [ $1 == "start" -o $1 == "s" ]; then
if [ $# = 1 ]
then
start "$DEFAULT_CMD"
else
start ${*:2} # all args except the first one
fi
# uninstall
elif [ $1 == "uninstall" ]; then
if [ $# = 1 ]; then
show_usage
else
uninstall ${*:2} # all args except the first one
fi
# version
elif [ $1 == "--version" -o $1 == "-v" ]; then
if [ $# = 1 ]; then
show_version
else
show_usage
fi
# upgrade
elif [ $1 == "upgrade" ]; then
if [ $# = 1 ]; then
upgrade
else
show_usage
fi
# bad argument
else
show_usage
fi
# exit successfully
exit 0