forked from 20tab/UnrealEnginePython
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathupypip.py
97 lines (80 loc) · 2.83 KB
/
upypip.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
96
97
import upycmd as cmd
import sys
from threading import Thread
from distutils.version import LooseVersion
class PipInstall:
#global modules class static variable
modules = None
def __init__(self):
return None
#blocking actions, should be run on off-thread
def pipModuleAction(self, command, args=None, verbose=True):
if args:
return cmd.run('pip ' + command + ' ' + args, cmd.PythonHomeScriptsPath(), verbose)
else:
return cmd.run('pip ' + command, cmd.PythonHomeScriptsPath(), verbose)
#use this if you need to work on the resulting list to query current dependencies
def listDict(self, verbose=True):
#get the list of all the modules from pip
resultString = self.pipModuleAction('list','--format=columns', verbose)
#convert to lines for parsing
lines = resultString.split("\n")
resultDict = {}
#exclude first two lines, then pair up the results
for i in range(2,len(lines)):
splitEntry = lines[i].split() #split and ignore all whitespace
resultDict[splitEntry[0]] = splitEntry[1]
#save list to cache
PipInstall.modules = resultDict
return resultDict
def isDesiredVersionSufficient(self, desired, current):
return LooseVersion(desired) <= LooseVersion(current)
def isInstalled(self, module, desiredVersion=None):
if desiredVersion == 'latest':
desiredVersion = None
if PipInstall.modules == None:
PipInstall.modules = self.listDict(False)
if module in PipInstall.modules:
#did we specify a version? ensure we've installed that version at least
if desiredVersion:
#print('current version: ' + str(PipInstall.modules[module]))
#print('desired version: ' + str(desiredVersion))
return self.isDesiredVersionSufficient(desiredVersion, PipInstall.modules[module])
else:
return True
else:
return False
#Threaded actions
def install(self, module):
PipInstall.modules = None #our cache is no longer valid
action = self.pipModuleAction
t = Thread(target=action, args=('install',module,))
t.start()
def uninstall(self, module):
PipInstall.modules = None #our cache is no longer valid
action = self.pipModuleAction
t = Thread(target=action, args=('uninstall -y',module,))
t.start()
def list(self):
action = self.listDict
t = Thread(target=action)
t.start()
def action(self, command):
action = self.pipModuleAction
t = Thread(target=action, args=(command,))
t.start()
def uninstallAll(self):
PipInstall.modules = None #our cache is no longer valid
action = self.pipModuleAction
t = Thread(target=action, args=('freeze','> unins && pip uninstall -y -r unins && del unins',))
t.start()
#instance forward declarations
_inst = PipInstall()
list = _inst.list
isInstalled = _inst.isInstalled
install = _inst.install
uninstall = _inst.uninstall
uninstallAll = _inst.uninstallAll
pipModuleAction = _inst.pipModuleAction
listDict = _inst.listDict
action = _inst.action