Skip to content

Commit 1a49af4

Browse files
committed
Add compile script
1 parent 2e48921 commit 1a49af4

1 file changed

Lines changed: 129 additions & 0 deletions

File tree

core/java11actionloop/bin/compile

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/usr/bin/env python
2+
"""Java Action Builder
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership.
7+
# The ASF licenses this file to You under the Apache License, Version 2.0
8+
# (the "License"); you may not use this file except in compliance with
9+
# the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
"""
20+
21+
from __future__ import print_function
22+
from os.path import abspath, exists, dirname
23+
import os, sys, codecs, subprocess, shutil, logging
24+
25+
def copy(src, dst):
26+
with codecs.open(src, 'r', 'utf-8') as s:
27+
body = s.read()
28+
with codecs.open(dst, 'w', 'utf-8') as d:
29+
d.write(body)
30+
31+
def find_with_ext(basedir, ext):
32+
result = []
33+
for root, _, files in os.walk(basedir, topdown=False):
34+
for name in files:
35+
if name.endswith(ext):
36+
result.append(os.path.join(root,name))
37+
return result
38+
39+
def javac(sources, classpath, target_dir):
40+
cmd = [ "javac",
41+
"-encoding", "UTF-8",
42+
"-cp", ":".join(classpath),
43+
"-d", target_dir
44+
]+sources
45+
#print(cmd)
46+
logging.info(" ".join(cmd))
47+
p = subprocess.Popen(cmd,
48+
stdout=subprocess.PIPE,
49+
stderr=subprocess.PIPE)
50+
(o, e) = p.communicate()
51+
if isinstance(o, bytes) and not isinstance(o, str):
52+
o = o.decode('utf-8')
53+
if isinstance(e, bytes) and not isinstance(e, str):
54+
e = e.decode('utf-8')
55+
ok = True
56+
if o:
57+
ok = False
58+
sys.stdout.write(o)
59+
sys.stdout.flush()
60+
if e:
61+
ok = False
62+
sys.stderr.write(e)
63+
sys.stderr.flush()
64+
return ok
65+
66+
def build(source_dir, classpath, target_dir, mainClass):
67+
68+
# copy exec to <main>.java if it is there
69+
src = "%s/exec" % source_dir
70+
if os.path.isfile(src):
71+
main_java = "%s/%s.java" % (source_dir, mainClass.split(".")[-1])
72+
copy(src,main_java)
73+
logging.info("renamed exec to %s", main_java)
74+
75+
# look for sources and compile
76+
sources = find_with_ext(source_dir, ".java")
77+
if len(sources) > 0:
78+
jars = find_with_ext(source_dir, ".jar")
79+
logging.info("compiling %d sources with %d jars", len(sources),len(jars))
80+
return javac(sources, classpath+jars, source_dir)
81+
82+
return True
83+
84+
def write_exec(target_dir, classpath, main):
85+
launcher = "%s/exec" % target_dir
86+
jars = find_with_ext(target_dir, ".jar")
87+
jars.append(target_dir)
88+
cmd = """#!/bin/bash
89+
cd "%s"
90+
/opt/java/openjdk/bin/java -Dfile.encoding=UTF-8 -cp "%s" Launcher "%s" "$@"
91+
""" %( target_dir, ":".join(classpath+jars), main)
92+
with codecs.open(launcher, 'w', 'utf-8') as d:
93+
d.write(cmd)
94+
os.chmod(launcher, 0o755)
95+
96+
def parseMain(main):
97+
if main == "main":
98+
return "Main", "main"
99+
a = main.split("#")
100+
if(len(a)==1):
101+
return a[0], "main"
102+
return a[0], a[1]
103+
104+
def assemble(argv):
105+
mainClass, mainMethod = parseMain(argv[1])
106+
logging.info("%s %s", mainClass, mainMethod)
107+
source_dir = os.path.abspath(argv[2])
108+
target_dir = os.path.abspath(argv[3])
109+
classpath = ["/usr/java/lib/launcher.jar", "/usr/java/lib/gson-2.8.5.jar"]
110+
111+
# build
112+
if build(source_dir, classpath, target_dir, mainClass):
113+
shutil.rmtree(target_dir)
114+
shutil.move(source_dir, target_dir)
115+
logging.info("moved %s to %s", source_dir, target_dir)
116+
# write the launcher is it is there
117+
write_exec(target_dir, classpath, "%s#%s" % (mainClass, mainMethod))
118+
# launch it to check it can load with immediate exit - it should not produce any output
119+
subprocess.call(["%s/exec" % target_dir, "-exit"])
120+
121+
sys.stdout.flush()
122+
sys.stderr.flush()
123+
124+
if __name__ == '__main__':
125+
if len(sys.argv) < 4:
126+
sys.stdout.write("usage: <main-class> <source-dir> <target-dir>\n")
127+
sys.exit(1)
128+
logging.basicConfig(filename="/var/log/compile.log")
129+
assemble(sys.argv)

0 commit comments

Comments
 (0)