Skip to content

Commit cf43591

Browse files
committed
fleshout startup function to reinstall pip if the project moves
1 parent ffef365 commit cf43591

2 files changed

Lines changed: 55 additions & 13 deletions

File tree

‎Content/Scripts/upycmd.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ def PythonHomeScriptsPath():
2121
home = PythonHomePath()
2222
return home + "/Scripts"
2323

24+
def PythonPluginScriptPath():
25+
tempPath = "not found"
26+
27+
for path in sys.path:
28+
if ('UnrealEnginePython' in path and
29+
path.endswith('Content/Scripts')):
30+
tempPath = path
31+
break
32+
33+
return tempPath
34+
2435
_PythonHomePath = PythonHomePath()
2536

2637
def FolderCommand(folder):
@@ -42,6 +53,13 @@ def run(process, path=_PythonHomePath, verbose=True):
4253
ue.log(stdoutdata[1])
4354
return stdoutdata[1] #return the data for dependent functions
4455

56+
#convenience override
57+
def runLogOutput(process, path=_PythonHomePath):
58+
fullcommand = FolderCommand(path) + process
59+
stdoutdata = subprocess.getstatusoutput(fullcommand)
60+
ue.log(stdoutdata[1])
61+
return stdoutdata[1]
62+
4563
#convenience wrappers
4664
def dir(path=_PythonHomePath):
4765
run('dir', path)

‎Content/Scripts/upystartup.py‎

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,60 @@ def checkPipDirectory():
1111
configPath = cmd.PythonPluginScriptPath() + '/upyconfig.json'
1212
correctPipPath = cmd.PythonHomeScriptsPath()
1313

14-
#let's test our pip directory path
14+
#compare our current pip directory with the installed one, if they differ reinstall pip
1515
try:
16-
with open(configPath) as data_file:
17-
configs = json.load(data_file)
16+
with open(configPath, "r+") as configFile:
17+
configs = json.load(configFile)
1818

1919
#grab currently stored path
2020
storedPipPath = configs['pipDirectoryPath']
2121

22-
print(storedPipPath)
23-
print(tempPath)
22+
print('upystartup::Checking pip location on startup')
23+
print('upystartup::stored loc: ' + storedPipPath)
24+
print('upystartup::correct loc: ' + correctPipPath)
2425

2526
#compare paths
2627
if (storedPipPath != correctPipPath):
2728
#if they don't match, remove the pip module and reinstall pip for this module
28-
29+
print('upystartup::Pip installation directory is stale, re-installing.')
30+
2931
libPath = cmd.PythonHomePath() + '/Lib/site-packages'
3032
dirs = listdir(libPath)
31-
print(libPath)
32-
print(dirs)
3333

3434
tempPath = None
35+
#find the directory that contains pip and ends with .dist-info
3536
for directory in dirs:
36-
if ('pip' in directory and
37+
#print(directory)
38+
if (directory.startswith('pip') and
3739
directory.endswith('.dist-info')):
38-
tempPath = libPath + directory
40+
tempPath = libPath + "/" + directory
41+
break
42+
43+
if(tempPath != None):
44+
#remove the old directory
45+
print('removing old: ' + tempPath)
46+
cmd.run('rmdir /S /Q "' + tempPath + '"')
47+
48+
#install pip
49+
print(cmd.PythonHomePath() + '/get-pip.py')
50+
51+
print('Installing pip...')
52+
cmd.runLogOutput('InstallPip.bat')
53+
54+
#update our stored location
55+
configs['pipDirectoryPath'] = correctPipPath
56+
configFile.seek(0)
57+
configFile.write(json.dumps(configs))
58+
configFile.truncate()
59+
60+
#done
61+
print('upystartup::updated pip.exe location in <' + configPath + '>')
62+
63+
else:
64+
print('upystartup::pip location is up to date.')
3965

40-
print(tempPath)
41-
#rmdir Li
4266
except:
43-
e = sys.exc_info()[0]
67+
e = sys.exc_info()
4468
ue.log('upyconfig.json error: ' + str(e))
4569

4670
#add any startup action you wish to perform in python

0 commit comments

Comments
 (0)