Jython的os.name问题
下面的这段脚本是在python中调用java来运行ant命令, 可以在Jython中却运行失败, 找不到ant的Main类, 什么原因呢?
#!/usr/bin/python""" runant.py Assumptions: - the "java" executable/script is on the command path - ANT_HOME has been set"""import os, os.path, string, sys# Change it to 1 to get extra debug informationdebug = 0######################################################################### check to make sure environment is setup#if not os.environ.has_key('ANT_HOME'):print '\n\nANT_HOME *MUST* be set!\n\n'sys.exit(1)else:ANT_HOME = os.environ['ANT_HOME']if not os.environ.has_key('JAVACMD'):JAVACMD = 'java'else:JAVACMD = os.environ['JAVACMD']# Sets the separator char for CLASSPATHSEPARATOR = ':'if os.name == 'dos' or os.name == 'nt':SEPARATOR = ';'# Build up standard classpathlocalpath = ''if os.environ.has_key('CLASSPATH'):localpath = os.environ['CLASSPATH']else:if debug:print 'Warning: no initial classpath\n'# Add jar filesLIBDIR = os.path.join(ANT_HOME, 'lib')jarfiles = []for file in os.listdir(LIBDIR):if file[-4:] == '.jar':jarfiles.append(os.path.join(LIBDIR,file))if debug:print 'Jar files:'for jar in jarfiles:print jarlocalpath = localpath + SEPARATOR + string.join(jarfiles, SEPARATOR)# If JAVA_HOME is defined, look for tools.jar & classes.zip# and add to classpathif os.environ.has_key('JAVA_HOME') and os.environ['JAVA_HOME'] != '':JAVA_HOME = os.environ['JAVA_HOME']TOOLS = os.path.join(JAVA_HOME, os.path.join('lib', 'tools.jar'))if os.path.exists(TOOLS):localpath = localpath + SEPARATOR + TOOLSCLASSES = os.path.join(JAVA_HOME, os.path.join('lib', 'classes.zip'))if os.path.exists(CLASSES):localpath = localpath + SEPARATOR + CLASSESelse:print '\n\nWarning: JAVA_HOME environment variable is not set.\n', \'If the build fails because sun.* classes could not be found\n', \'you will need to set the JAVA_HOME environment variable\n', \'to the installation directory of java\n'# JikesANT_OPTS = []if os.environ.has_key('ANT_OPTS'):ANT_OPTS = string.split(os.environ['ANT_OPTS'])if os.environ.has_key('JIKESPATH'):ANT_OPTS.append('-Djikes.class.path=' + os.environ['JIKESPATH'])# Builds the commandlinecmdline = '%s -classpath %s -Dant.home=%s %s org.apache.tools.ant.Main %s' \ % (JAVACMD, localpath, ANT_HOME, string.join(ANT_OPTS,' '), \ string.join(sys.argv[1:], ' '))if debug:print '\n%s\n\n' % (cmdline)# Run the biniou!os.system(cmdline)
首先想到classpath配的有问题, 的确如此. 如果你在Jython中运行下面这段脚本, 会发现ant.jar的前的分隔符是':', 我是在windows上运行的所以应该是';' 但这段关于分隔符的判定有似乎没问题
# Sets the separator char for CLASSPATHSEPARATOR = ':'if os.name == 'dos' or os.name == 'nt':SEPARATOR = ';'
如果我们调试一下会发现os.name的输出其实是既不是'dos', 也不是'nt', 而是'java'
再查看一下python的doc, http://docs.python.org/library/os.html, os.name的枚举值里的确有'java'.
这说明Python和Jython的实现肯定还是有差别的, 毕竟Jython是用纯java的实现的. 在java中可以用System.getProperty("os.name")取得系统类型. 不知道Jython为何将os.name设为'java'. 希望哪位python大牛给出jython的初衷.
找了一下os.environ中的键值对, 找到下面的方法可以在Jython中正确运行
# Sets the separator char for CLASSPATHSEPARATOR = ';'if not os.environ['OS'] == 'Windows_NT':SEPARATOR = ':'