Skip to content

Instantly share code, notes, and snippets.

@anatolinicolae
Last active March 8, 2016 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anatolinicolae/8aba46a709988e606bf0 to your computer and use it in GitHub Desktop.
Save anatolinicolae/8aba46a709988e606bf0 to your computer and use it in GitHub Desktop.
Simple script that compiles Netbeans projects and runs Tomcat in Docker.
#!/usr/bin/env python
import click, tempfile, shutil, subprocess, os
"""Simple script that compiles Netbeans projects and runs Tomcat in Docker."""
class jsprun:
def __init__(self, d, n):
self.d = d
self.n = n
self.CWD = os.getcwd()
self.JAVAC = 'javac -encoding UTF-8 -cp .:/usr/local/tomcat/lib/* -d '
self.tempdir = tempfile.mkdtemp()
os.makedirs(self.tempdir + '/META-INF')
os.makedirs(self.tempdir + '/WEB-INF/classes')
self.JAVAC += self.tempdir + '/WEB-INF/classes'
self.tomdir = '/usr/local/tomcat/webapps/'
# Check if dir has been given
if not d:
d = self.CWD
else:
d = self.create_path(d)
# Compile sources
self.compile(d)
# Copy files to a new dir
self.copy_files(d)
# Create WAR file
self.create_war()
# Run Docker
self.run_docker()
def compile(self, path):
path = self.rts(path) + '/src/java'
if os.path.isdir(path):
click.echo("Compiling Java sources...")
for file in os.listdir(path):
if file.endswith(".java"):
subprocess.call("%s %s/%s" % (self.JAVAC, path, file), shell=True)
else:
click.echo("Couldn't find src folder...")
def copy_files(self, path):
path = self.rts(path)
subprocess.call("cp -r %s/web/* %s" % (path, self.tempdir), shell=True)
def create_path(self, path):
if not path.startswith('/'):
return self.CWD + '/' + path
else:
return path
def create_war(self):
if not self.n:
f = 'project.war'
else:
f = "%s.war" % self.n
subprocess.call("cd %s && jar cf %s ." % (self.tempdir, self.tomdir + f), shell=True)
# rts: Remove Trailing Slash
def rts(self, s):
if s.endswith('/'):
s = s[:-1]
return s
def run_docker(self):
subprocess.call("docker run -it --rm -p 8080:8080 -v /usr/local/tomcat:/usr/local/tomcat tomcat:jre8", shell=True)
@click.command()
@click.option('-d', default='', help='Project-to-compile\'s directory.')
@click.option('-n', default='', help='Name of the output project.')
def app(d, n):
try:
app = jsprun(d, n)
except KeyboardInterrupt:
print "Quitting..."
if __name__ == '__main__':
app()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment