#!/usr/bin/python import sys import subprocess import os import glob try: import ConfigParser as configparser except: import configparser class CustomRunner: @staticmethod def get_java_version_default_on_system(): java_default = "Unknown" try: proc = subprocess.Popen(["readlink -f $(which java)"], stdout=subprocess.PIPE,shell=True) out = proc.communicate()[0].decode("utf-8").strip() java_default = out.split("/")[4] except Exception as e: pass return java_default @staticmethod def list_java_available(): proc = subprocess.Popen(["/usr/sbin/update-java-alternatives","-l"],stdout=subprocess.PIPE) list_of_javas = [] for line in iter(proc.stdout.readline,'\n'): path_to_java="" name_of_java="" if line != b'': #the real code does filtering here try: name_of_java,path_to_java=line.rstrip().decode("utf-8").split()[0],line.rstrip().decode("utf-8").split()[2] except IndexError as indexerror: pass if path_to_java != "" and os.path.exists(path_to_java): list_of_javas.append((name_of_java,path_to_java)) else: break return list_of_javas @staticmethod def run_with_java(java_selected,path_to_jar): print(java_selected + " ---> " + path_to_jar) list_of_javas = CustomRunner.list_java_available() for javavm in list_of_javas: if javavm[0] == java_selected: os.environ["JAVA_HOME"]=javavm[1] javacmd=(javavm[1]+"/bin/java"+ " -jar \""+path_to_jar.decode("utf-8")+"\"") CustomRunner.set_java_for_this_jar(java_selected,path_to_jar) print javacmd.encode("utf-8") os.system(javacmd.encode("utf-8")) @staticmethod def usage(): print("USAGE: java-custom-runner") print(" java-custom-runner PATH_TO_JAR : exec with default (get by configuration file)") print(" java-custom-runner -l : List javas") print(" java-custom-runner -j JavaName PATH_TO_JAR : exec Jar with JavaName") print(" java-custom-runner -s|--set JavaName PATH_TO_JAR : save this preference for the Jar given") print("") print(" Ex: java-custom-runner -j java-6-oracle /home/user/myJar.jar") print("") print(" java-custom-runner -h : Show this help") sys.exit(0) @staticmethod def set_java_for_this_jar(java_selected, path_to_jar): ''' Set the java selected for the jar. The result will be saved on user settings at : ~/.config/java-custom-runner/user.conf ''' try: homedir = os.path.expanduser('~') if not os.path.exists(homedir+"/.config/java-custom-runner/user.conf"): if not os.path.isdir(homedir+"/.config/java-custom-runner/"): os.mkdir(homedir+"/.config/java-custom-runner/") user_cfg = open(homedir+"/.config/java-custom-runner/user.conf","w") user_cfg.write("[JAVA]") user_cfg.close() config = configparser.ConfigParser() config.optionxform=str config.read(homedir+"/.config/java-custom-runner/user.conf") if not config.has_section("JAVA"): config.add_section("JAVA") config.set("JAVA",os.path.basename(path_to_jar),java_selected) with open(homedir+"/.config/java-custom-runner/user.conf", 'wb') as configfile: config.write(configfile) except Exception as e: print(" [ CustomRunner ] : set_java_for_this_jar for user not works ... "+ str(e)) @staticmethod def set_java_for_this_jar_at_system(java_selected, path_to_jar): ''' Set the java selected for the jar. The result will be saved on system settings at : /etc/java-custom-runner/admin.conf ''' system_conf="/etc/java-custom-runner/admin.conf" try: if not os.path.exists(system_conf): if not os.path.isdir("/etc/java-custom-runner/"): os.mkdir("/etc/java-custom-runner/") user_cfg = open(system_conf,"w") user_cfg.write("[JAVA]") user_cfg.close() config = configparser.ConfigParser() config.optionxform=str config.read(system_conf) if not config.has_section("JAVA"): config.add_section("JAVA") config.set("JAVA",os.path.basename(path_to_jar),java_selected) with open(system_conf, 'wb') as configfile: config.write(configfile) except Exception as e: print(" [ CustomRunner ] : set_java_for_this_jar_at_system for user not works ... "+ str(e)) @staticmethod def get_java_for_this_jar(path_to_jar): ''' Get the java selected for the jar. The mechanism is : - First the system-package-settings - Second the system-admin-settings - Third the user-settings. ''' java_for_this_java = None homedir = os.path.expanduser('~') key=os.path.basename(path_to_jar).decode("utf-8") # First the system-package-settings if os.path.exists("/etc/java-custom-runner/"): files = glob.glob("/etc/java-custom-runner/*.ini") config = configparser.ConfigParser() for i in files: config.read(files) try: java_for_this_java=config.get("JAVA",key) except Exception as e: print("Not found at dict system: "+ str(e)) # Second the system-admin-settings if os.path.exists("/etc/java-custom-runner/admin.conf"): config = configparser.ConfigParser() config.read("/etc/java-custom-runner/admin.conf") section_dir=config.defaults() try: java_for_this_java=config.get("JAVA",os.path.basename(path_to_jar)) except Exception as e: print("Not found at dict admin.conf: "+ str(e)) # Last user-settings if os.path.exists(homedir+"/.config/java-custom-runner/user.conf"): config = configparser.ConfigParser() config.read(homedir+"/.config/java-custom-runner/user.conf") try: java_for_this_java=config.get("JAVA",os.path.basename(path_to_jar)) except Exception as e: print("Not found at dict user.conf: "+ str(e)) return java_for_this_java @staticmethod def run_as_default(path_to_jar): java_selected = CustomRunner.get_java_for_this_jar(path_to_jar) javas_on_system = CustomRunner.list_java_available() if java_selected in javas_on_system: CustomRunner.run_with_java(java_selected,path_to_jar) else: javacmd=('java'+ ' -jar "'+path_to_jar+'"').decode("utf-8") os.system(javacmd.encode("utf-8")) if __name__ == '__main__': if len(sys.argv) < 2: CustomRunner.usage() if sys.argv[1] == "-l" or sys.argv[1] == "--list": for java in CustomRunner.list_java_available(): print (java[0] + " --> " + java[1] ) elif sys.argv[1] == "-s" or sys.argv[1] == "--set": if sys.argv[2] != "" and sys.argv[3] != "": java_selected=sys.argv[2] path_to_jar=sys.argv[3] if os.getuid() == 0: CustomRunner.set_java_for_this_jar_at_system(java_selected,path_to_jar) else : CustomRunner.set_java_for_this_jar(java_selected, path_to_jar) elif sys.argv[1] == "-j" or sys.argv[1] == "--java": if sys.argv[2] != "" and sys.argv[3] != "": java_selected=sys.argv[2] path_to_jar=sys.argv[3] CustomRunner.run_with_java(java_selected,path_to_jar) else: print ("Unkown number of arguments...see the usage") elif sys.argv[1] == "-h" or sys.argv[1] == "--help": CustomRunner.usage() elif os.path.exists(sys.argv[1]): ext = os.path.splitext(sys.argv[1])[-1].lower() if ext == ".jar": CustomRunner.run_as_default(sys.argv[1]) # In other cases...exit # Exit gracefully sys.exit(0)