Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
getnamo committed Mar 18, 2018
2 parents 02327ae + edbfbe4 commit cdeda03
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
16 changes: 8 additions & 8 deletions Content/Scripts/upycmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def NormalizePaths():
#replace '/' to '\\'
for i in range(len(sys.path)):
currentPath = sys.path[i]
sys.path[i] = currentPath.replace('/','\\')
sys.path[i] = currentPath.replace('\\','/')

#find additional problem paths such as engine bin
currentPath = sys.path[i]
Expand All @@ -30,32 +30,32 @@ def NormalizePaths():
#define some convenience paths
def PythonHomePath():
for path in sys.path:
normalizedPath = path.replace('/','\\')
normalizedPath = AsAbsPath(path)
if ('UnrealEnginePython' in normalizedPath and
normalizedPath.endswith('Binaries\\Win64')):
normalizedPath.endswith('Binaries/Win64')):
return path

#return sys.path[1]
return "not found"

def PythonHomeScriptsPath():
return PythonHomePath() + "/Scripts"
return AsAbsPath(PythonHomePath() + "/Scripts")

def PythonPluginScriptPath():
for path in sys.path:
normalizedPath = path.replace('/','\\')
normalizedPath = AsAbsPath(path)
if ('UnrealEnginePython' in normalizedPath and
normalizedPath.endswith('Content\\Scripts')):
normalizedPath.endswith('Content/Scripts')):
return path

return "not found"

def PythonProjectScriptPath():
relativePath = PythonPluginScriptPath() + "/../../../../Content/Scripts";
return os.path.abspath(relativePath);
return AsAbsPath(relativePath);

def AsAbsPath(path):
return os.path.abspath(path)
return os.path.abspath(path).replace('\\','/')

_PythonHomePath = PythonHomePath()

Expand Down
12 changes: 11 additions & 1 deletion Content/Scripts/upymodule_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ def parseJson(packagePath):
ue.log("upymodule_importer::" + module + " " + version + " installed? " + str(pip.isInstalled(module)))
if not pip.isInstalled(module, version):
ue.log('upymodule_importer::Dependency not installed, fetching via pip...')
pip.install(module + '==' + version)
if version == 'latest':
pip.install(module)
else:
pip.install(module + '==' + version)

dependencyNoun = 'dependencies'
if len(pythonModules) == 1:
Expand All @@ -45,3 +48,10 @@ def parseJson(packagePath):
e = sys.exc_info()[0]
ue.log('upymodule_importer::upymodule.json error: ' + str(e))

def containsModuleFile(packagePath):
try:
with open(packagePath) as data_file:
return True
except:
return False

17 changes: 14 additions & 3 deletions Content/Scripts/upypip.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ def __init__(self):
return None

#blocking actions, should be run on off-thread
def pipModuleAction(self, command, args, verbose=True):
return cmd.run('pip ' + command + ' ' + args, cmd.PythonHomeScriptsPath(), verbose)
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):
Expand All @@ -37,6 +40,8 @@ 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:
Expand Down Expand Up @@ -68,6 +73,11 @@ def list(self):
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
Expand All @@ -83,4 +93,5 @@ def uninstallAll(self):
uninstall = _inst.uninstall
uninstallAll = _inst.uninstallAll
pipModuleAction = _inst.pipModuleAction
listDict = _inst.listDict
listDict = _inst.listDict
action = _inst.action
14 changes: 13 additions & 1 deletion Content/Scripts/upystartup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
from os import path as ospath
import imp
import site
import upymodule_importer as upymod

import upypip as pip

def checkPipDirectory():
#get our python scripts path
configPath = cmd.PythonPluginScriptPath() + '/upyconfig.json'
configPath = cmd.AsAbsPath(cmd.PythonPluginScriptPath() + '/upyconfig.json')
correctPipPath = cmd.PythonHomeScriptsPath()

#check that we have a config file, if not make an empty one
Expand Down Expand Up @@ -89,7 +90,18 @@ def checkPipDirectory():
print('upystartup::system paths normalized.')
cmd.NormalizePaths();

def checkProjectModuleFile():
path = cmd.PythonProjectScriptPath() + '/upymodule.json'
if upymod.containsModuleFile(path):
upymod.parseJson(path)
print("upystartup::project upymodule.json file parsed.")
else:
print("upystartup::project doesn't use a upymodule.json file.")

#add any startup action you wish to perform in python
def startup():
#check that our pip directory matches for pip.exe commands
checkPipDirectory()

#import any project specific modules
checkProjectModuleFile()
2 changes: 1 addition & 1 deletion UnrealEnginePython.uplugin
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"FileVersion": 3,
"Version": 1,
"VersionName": "1.4.2",
"VersionName": "1.4.3",
"FriendlyName": "UnrealEnginePython",
"Description": "Embed a Python VM in your project",
"Category": "Scripting Languages",
Expand Down

0 comments on commit cdeda03

Please sign in to comment.