SLIDER-153 refactor .py script slightly for use elsewhere
Project: http://git-wip-us.apache.org/repos/asf/incubator-slider/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-slider/commit/0ad4fb4f Tree: http://git-wip-us.apache.org/repos/asf/incubator-slider/tree/0ad4fb4f Diff: http://git-wip-us.apache.org/repos/asf/incubator-slider/diff/0ad4fb4f Branch: refs/heads/feature/SLIDER-151_Implement_full_slider_API_in_REST_and_switch_client_to_it Commit: 0ad4fb4f7b5cc4e590cbb9e51aa7cefb59a0ae2d Parents: a048c9f Author: Steve Loughran <ste...@apache.org> Authored: Mon Jun 30 16:16:08 2014 +0100 Committer: Steve Loughran <ste...@apache.org> Committed: Mon Jun 30 16:16:08 2014 +0100 ---------------------------------------------------------------------- slider-assembly/src/main/scripts/slider.py | 107 ++++++++++++++---------- 1 file changed, 64 insertions(+), 43 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-slider/blob/0ad4fb4f/slider-assembly/src/main/scripts/slider.py ---------------------------------------------------------------------- diff --git a/slider-assembly/src/main/scripts/slider.py b/slider-assembly/src/main/scripts/slider.py index d48eca6..e60bed3 100644 --- a/slider-assembly/src/main/scripts/slider.py +++ b/slider-assembly/src/main/scripts/slider.py @@ -60,10 +60,10 @@ def confDir(sliderdir): localconf = os.path.join(sliderdir, CONF) return os.environ.get(SLIDER_CONF_DIR,localconf) -def dirMustExist(dir): - if not os.path.exists(dir): - raise Exception("Directory does not exist: %s " % dir) - return dir +def dirMustExist(dirname): + if not os.path.exists(dirname): + raise Exception("Directory does not exist: %s " % dirname) + return dirname def read(pipe, line): """ @@ -83,7 +83,61 @@ def read(pipe, line): return line, True else: return line, False - + + +def runProcess(commandline): + """ + Run a process + :param commandline: command line + :return:the return code + """ + print "ready to exec : %s" % commandline + exe = subprocess.Popen(commandline, + stdin=None, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + shell=False) + stdout = exe.stdout + stderr = exe.stderr + outline = "" + errline = "" + while exe.poll() is None: + # process is running; grab output and echo every line + outline, done = read(stdout, outline) + if done: + print outline + outline = "" + errline, done = read(stderr, errline) + if done: + print errline + errline = "" + + # get tail + out, err = exe.communicate() + print outline + out.decode() + print errline + err.decode() + return exe.returncode + + +def java(classname, args, classpath, jvm_opts_list): + """ + Execute a java process, hooking up stdout and stderr + and printing them a line at a time as they come in + :param classname: classname + :param args: arguments to the java program + :param classpath: classpath + :param jvm_opts_list: list of JVM options + :return: the exit code. + """ + # split the JVM opts by space + # java = "/usr/bin/java" + commandline = ["java"] + commandline.extend(jvm_opts_list) + commandline.append("-classpath") + commandline.append(classpath) + commandline.append(classname) + commandline.extend(args) + return runProcess(commandline) def usage(): @@ -98,14 +152,13 @@ def main(): """ if len(sys.argv)==1 : return usage() - print "stdout encoding: "+ sys.stdout.encoding + # print "stdout encoding: "+ sys.stdout.encoding args = sys.argv[1:] slider_home = sliderDir() libdir = dirMustExist(libDir(slider_home)) confdir = dirMustExist(confDir(slider_home)) default_jvm_opts = DEFAULT_JVM__OPTS % confdir slider_jvm_opts = os.environ.get(SLIDER_JVM_OPTS, default_jvm_opts) - # split the JVM opts by space jvm_opts_split = slider_jvm_opts.split() slider_classpath_extra = os.environ.get(SLIDER_CLASSPATH_EXTRA, "") p = os.pathsep # path separator @@ -119,42 +172,10 @@ def main(): print "slider_jvm_opts = \"%s\"" % slider_jvm_opts print "slider_classpath = \"%s\"" % slider_classpath - #java = "/usr/bin/java" - commandline = ["java", ] - commandline.append("-classpath") - commandline.append(slider_classpath) - commandline.extend(jvm_opts_split) - commandline.append(SLIDER_CLASSNAME) - commandline.extend(args) - print "ready to exec : %s" % commandline - # docs warn of using PIPE on stderr - exe = subprocess.Popen(commandline, - stdin=None, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - shell=False) - stdout = exe.stdout - stderr = exe.stderr - outline = "" - errline = "" - while exe.poll() is None: - # process is running; grab output and echo every line - outline, done = read(stdout, outline) - if done: - print outline - outline = "" - errline, done = read(stderr, errline) - if done: - print errline - errline = "" - - # get tail - out, err = exe.communicate() - print outline + out.decode() - print errline + err.decode() - return exe.returncode - - + return java(SLIDER_CLASSNAME, + args, + slider_classpath, + jvm_opts_split) if __name__ == '__main__': """