-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmake_apps.py
198 lines (184 loc) · 9.04 KB
/
make_apps.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
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
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
from __future__ import print_function
import os
import stat
import sys
from configurePOM import archs, detectArch, checkPythonVersion
import subprocess
java_apps = {
"BLNinfer": {"class": "probcog.srl.directed.inference.BLNinfer"},
"BLN2MLN": {"class": "probcog.BLN2MLN"},
"BLNprintCPT": {"class": "probcog.BLNprintCPT"},
"BLOGDB2MLNDB": {"class": "probcog.BLOGDB2MLNDB"},
"BNprintCPT": {"class": "probcog.BNprintCPT"},
"BNinfer": {"class": "probcog.bayesnets.inference.BNinfer"},
"ABL2MLN": {"class": "probcog.ABL2MLN"},
"BN2CSV": {"class": "probcog.bayesnets.conversion.BN2CSV"},
"BNsaveAs": {"class": "probcog.bayesnets.conversion.BNsaveAs"},
"BNlistCPTs": {"class": "probcog.BNlistCPTs"},
"BNrandomEvidence": {"class": "probcog.BNrandomEvidence"},
"MLN2WCSP": {"class": "probcog.MLN2WCSP"},
"MLNinfer": {"class": "probcog.MLNinfer"},
"bnj": {"class": "probcog.BNJ"},
"genDB": {"class": "probcog.genDB"},
"groundABL": {"class": "probcog.groundABL"},
"learnABL": {"class": "probcog.srl.directed.learning.BLNLearner"},
"learnABLSoft": {"class": "dev.learnABLSoft"},
"jython": {"class": 'org.python.util.jython'},
"syprolog": {"class": "probcog.PrologShell"},
"yprolog": {"class": "yprolog.Go"},
"blogdb2ergevid": {"class": "blogdb2ergevid"},
"bndb2inst": {"class": "probcog.bayesnets.conversion.BNDB2Inst"},
}
java_apps["netEd"] = java_apps["bnj"]
java_apps["pcjython"] = java_apps["jython"]
python_apps = [
{"name": "mlnquery", "script": "$SRLDB_HOME/src/main/python/mlnQueryTool.py"},
{"name": "mlnlearn", "script": "$SRLDB_HOME/src/main/python/mlnLearningTool.py"},
{"name": "amlnlearn", "script": "$SRLDB_HOME/src/main/python/amlnLearn.py"},
{"name": "blnquery", "script": "$SRLDB_HOME/src/main/python/blnQueryTool.py"},
{"name": "bnquery", "script": "$SRLDB_HOME/src/main/python/bnQueryTool.py"},
{"name": "blnlearn", "script": "$SRLDB_HOME/src/main/python/blnLearningTool.py"},
{"name": "fixCR", "script": "$SRLDB_HOME/src/main/python/fixCR.py"},
{"name": "MLN", "script": "$SRLDB_HOME/src/main/python/MLN.py"},
{"name": "trajvis", "script": "$SRLDB_HOME/src/main/python/trajvis.py"},
{"name": "evalSeqLabels", "script": "$SRLDB_HOME/src/main/python/evalSeqLabels.py"},
{"name": "pmml2graphml", "script": "$SRLDB_HOME/src/main/python/pmml2graphml.py"},
]
pythonInterpreter = "python"
def adapt(name, arch):
home = os.path.abspath(".")
if arch == "msyss":
sep = "/"
home_drive = home[0]
home = "/" + home_drive + "/" + home[3:]
home = home.replace(os.path.sep, sep)
else:
sep = os.path.sep
return name.replace("<ARCH>", arch).replace("$SRLDB_HOME", home).replace("/", sep)
def getDependencyClasspath():
p = subprocess.Popen("mvn dependency:build-classpath", shell=True,
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(child_stdin, child_stdout) = (p.stdin, p.stdout)
lines = child_stdout.readlines()
classpath = None
for i, line in enumerate(lines):
if "Dependencies classpath" in line:
classpath = lines[i+1].strip()
if classpath is None:
print("ERROR: Could not determine classpath via maven. Check for problems in maven's output below:\n\n")
print("".join(lines))
sys.exit(1)
return classpath
def createEnvScript(arch):
appsDir = adapt("$SRLDB_HOME/apps", arch)
if arch != "msys":
pythonDir = adapt("$SRLDB_HOME/src/main/python", arch)
jythonDir = adapt("$SRLDB_HOME/src/main/jython", arch)
else: # msys must use Windows-style paths in PYTHONPATH/JYTHONPATH when using regular Windows Python
pythonDir = adapt("$SRLDB_HOME/src/main/python", "win64")
jythonDir = adapt("$SRLDB_HOME/src/main/jython", "win64")
if not "win" in arch:
with open("env.sh", "w") as f:
f.write("export PATH=\"$PATH:%s\"\n" % appsDir)
f.write("export PROBCOG_HOME=\"%s\"\n" % adapt("$SRLDB_HOME", arch))
pythonpath = ["$PYTHONPATH", pythonDir]
jythonpath = ["$JYTHONPATH", jythonDir, pythonDir]
pythonpath_sep = ":" if not arch == "msys" else ";"
f.write("export PYTHONPATH=\"%s\"\n" % pythonpath_sep.join(pythonpath))
f.write("export JYTHONPATH=\"%s\"\n" % pythonpath_sep.join(jythonpath))
if arch == "msys":
# create aliases to be able to run batch files
apps = []
for appname, app in java_apps.iteritems():
apps.append(appname)
for app in python_apps:
apps.append(app["name"])
for app in apps:
f.write("alias %s='\"%s/%s.bat\"'\n" % (app, appsDir, app))
else:
with open("env.bat", "w") as f:
f.write("SET PATH=%%PATH%%;%s\r\n" % appsDir)
f.write("SET PYTHONPATH=%%PYTHONPATH%%;%s\r\n" % pythonDir)
f.write("SET JYTHONPATH=%%JYTHONPATH%%;%s;%s\r\n" % (jythonDir, pythonDir))
f.write("SET PROBCOG_HOME=%s\n" % adapt("$SRLDB_HOME", arch))
return appsDir, pythonDir, jythonDir
if __name__ == '__main__':
checkPythonVersion()
print("\nProbCog Apps Generator\n\n")
print(" usage: make_apps [--arch=%s] [additional JVM args]\n" % "|".join(archs))
print()
print(" Note: Some useful JVM args include")
print(" -Xmx8000m set maximum Java heap space to 8000 MB")
print(" -ea enable assertions")
print()
args = sys.argv[1:]
# check if ProbCog binaries exist
if not os.path.exists(os.path.join("target", "classes")):
print("ERROR: No ProbCog binaries found. If you are using the source version of ProbCog, please compile it first using 'mvn compile'")
sys.exit(1)
# determine architecture
arch = None
if len(args) > 0 and args[0].startswith("--arch="):
arch = args[0][len("--arch="):].strip()
args = args[1:]
else:
arch = detectArch()
if arch is None:
print("Could not automatically determine your system's architecture. Please supply the --arch argument")
sys.exit(1)
if arch not in archs:
print("Unknown architecture '%s'" % arch)
sys.exit(1)
jvm_userargs = " ".join(args)
if not os.path.exists("apps"):
os.mkdir("apps")
print("\nDetermining dependency classpath...")
dep_classpath = getDependencyClasspath()
print("\nCreating application files for %s..." % arch)
classpath = os.path.pathsep.join([adapt("$SRLDB_HOME/target/classes", arch), dep_classpath])
isWindows = "win" in arch
isMacOSX = "macosx" in arch
preamble = "@echo off\r\n" if isWindows else "#!/bin/sh\n"
allargs = '%*' if isWindows else '"$@"'
pathsep = os.path.pathsep
for appname, app in java_apps.iteritems():
filename = os.path.join("apps", "%s%s" % (appname, {True:".bat", False:""}[isWindows]))
print(" %s" % filename)
with file(filename, "w") as f:
f.write(preamble)
addargs = "-XstartOnFirstThread" if arch in ("macosx", "macosx64") else ""
f.write('java %s -cp "%s" %s %s %s\n' % (addargs, classpath, jvm_userargs, adapt(app["class"], arch), allargs))
f.close()
if not isWindows: os.chmod(filename, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
for app in python_apps:
filename = os.path.join("apps", "%s%s" % (app["name"], {True:".bat", False:""}[isWindows]))
print(" %s" % filename)
f = file(filename, "w")
f.write(preamble)
f.write("%s -O \"%s\" %s\n" % (pythonInterpreter, adapt(app["script"], arch), allargs))
f.close()
if not isWindows: os.chmod(filename, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH)
print()
# write shell script for environment setup
if not isWindows:
createEnvScript(arch)
print('Now, to set up your environment type:')
print(' source env.sh')
print()
print('To permantly configure your environment, add this line to your shell\'s initialization script (e.g. ~/.bashrc):')
print(' source %s' % adapt("$SRLDB_HOME/env.sh", arch))
print()
else:
appsDir, pythonDir, jythonDir = createEnvScript(arch)
createEnvScript("msys")
print('To temporarily set up your environment for the current session, type:')
print(' env.bat [when using CMD]')
print(' source env.sh [when using MSYS bash/git-bash]')
print()
print('To permanently configure your environment, use Windows Control Panel to set the following environment variables:')
print(' To the PATH variable add the directory "%s"' % appsDir)
print(' To the PYTHONPATH variable add the directory "%s"' % pythonDir)
print(' To the JYTHONPATH variable add the directories "%s" and "%s"' % (jythonDir, pythonDir))
print('Should any of these variables not exist, simply create them.')