-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpip_sn_pack.py
95 lines (75 loc) · 2.22 KB
/
pip_sn_pack.py
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
import os
import subprocess
from optparse import OptionParser
def cmd_uninstall(pack):
"""
Function to generate the command to uinstall a package using pip
Parameters
--------------
pack: str
name of the package to uninstall
Returns
-----------
cmd: str
cmd to apply
"""
cmd = "pip uninstall {}".format(pack)
return cmd
def cmd_list():
"""
Function to to generate the command giving the list of packages installed with pip
Returns
-----------
cmd: str
cmd to apply
"""
cmd = "pip freeze | grep sn- | cut -d \'=\' -f1"
return cmd
def cmd_install(package, verbose):
"""
Function to generate the command to install a package using pip
Parameters
--------------
pack: str
name of the package to install
gitbranch: str
git branch (master, dev, ...) of the package to install
Returns
-----------
cmd: str
cmd to apply
"""
if verbose:
cmd = "pip -v install . --user --install-option=\"--package={}\"".format(
pack)
else:
cmd = "pip install . --user --install-option=\"--package={}\"".format(
pack)
return cmd
parser = OptionParser()
parser.add_option("--package", type="str", default='sn_pipe',
help="package name to install [%default]")
parser.add_option("--verbose", type=int, default=0,
help="verbose mode for pip installation [%default]")
parser.add_option("--action", type="str", default='list',
help="action to perform: list, install, uninstall [%default]")
opts, args = parser.parse_args()
pack = opts.package
verbose = opts.verbose
action = opts.action
if action == 'install':
os.system(cmd_install(pack, verbose))
if action == 'list':
os.system(cmd_list())
if action == 'uninstall':
if pack != 'all':
os.system(cmd_uninstall(pack))
else:
# this will uninstall the entire pipeline
# get all the packages
packgs = subprocess.Popen(
cmd_list(), shell=True, stdout=subprocess.PIPE).stdout.read()
listpk = packgs.decode().split('\n')
for pp in listpk:
if pp != '':
os.system(cmd_uninstall(pp))