Skip to content

Instantly share code, notes, and snippets.

@ShahriyarR
Created March 20, 2014 09:11
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 ShahriyarR/9659973 to your computer and use it in GitHub Desktop.
Save ShahriyarR/9659973 to your computer and use it in GitHub Desktop.
"""
Written in Python 3.
Running JAR file within Python file.
Especially for DWH super boy (after Shahmaddin) Rahim's JAR file (mail sender) :D :D
Using Oracle cx_Oracle library + Python 3's subprocess.Popen
"""
import cx_Oracle
import os
import subprocess
import shlex
import logging
os.environ['JAVA_HOME'] = "/usr/java/jre1.7.0_17/bin/java"
os.environ['HOME'] = "/root"
os.environ['PATH'] = "/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/opt/oracle/dwh/bin"
os.environ['ORACLE_SID'] = "xxx"
os.environ['ORACLE_HOME'] = "/opt/oracle/xxx"
os.environ['LD_LIBRARY_PATH'] = "/opt/oracle/xxx/lib"
os.environ['ORA_NLS33'] = "/opt/oracle/xxx/nls/data"
class OracleConn:
def __init__(self):
try:
self.con = cx_Oracle.connect('user_name/password@ip_address/oracle_sid')
except Exception as exc:
print(type(exc))
print(exc)
else:
self.cur = self.con.cursor()
def run_sql(self):
self.cur.execute("select to_char(getsystem_date,'DAY') day from dual")
for result in self.cur:
return result[0].rstrip()
def close_all(self):
self.cur.close()
self.con.close()
class RunJar:
def __init__(self):
self.path = '/test-test/rehim/iclr'
self.java_command = '/usr/java/jre1.7.0_17/bin/java -jar %s/IndividualCarLoanReporter.jar' % self.path
self.sql_result = OracleConn().run_sql()
def run_jar(self):
if self.sql_result == "MONDAY":
try:
args = shlex.split(self.java_command)
fb = subprocess.Popen(args, stdout=subprocess.PIPE)
except Exception as exc2:
print(type(exc2))
print(exc2)
else:
print(str(fb.stdout.read()))
else:
print("Wait for Monday")
w = RunJar()
w.run_jar()
x = OracleConn()
x.close_all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment