Jython vs CPython - sys模块参数解析

时间:2016-10-31 21:16:09

标签: python batch-file jython wlst command-line-parsing

在编写用于WebLogic Sc​​ripting Tool(12.1.3)的部署脚本时,我遇到了Python 2.2.1和Jython 2.2.1之间的这种不一致。如果您将命令行参数传递给每个参数,则会对其进行不同的解析,如此测试程序所示:

$ cat pytest.py
import sys
print sys.argv

运行时,以下是每个解释器的结果。

CPython 2.2.1:

$ /cygdrive/c/Python22/python.exe pytest.py a b 'c,d,e'
['pytest.py', 'a', 'b', 'c,d,e']

Jython 2.2.1:

$ /cygdrive/c/jython2.2.1/jython.bat pytest.py a b 'c,d,e'
['pytest.py', 'a', 'b', 'c', 'd', 'e']

我使用Jython 2.2.1的原因是因为它是WLST使用的Python实现,所以我不相信我可以升级到更高版本或使用CPython解释器来规避我的使用中的问题情况下。

这是一个错误吗? Jython的解析似乎违反直觉。有没有办法在Jython中解析CPython方式的参数?提前谢谢。

jython.bat的内容(包含在Jython安装中):

$ cat jython.bat
@echo off
rem This file was generated by the Jython installer
rem Created on Mon Oct 31 13:19:59 PDT 2016 by smcgloth

set ARGS=

:loop
if [%1] == [] goto end
    set ARGS=%ARGS% %1
    shift
    goto loop
:end

"C:\Program Files\Java\jre1.8.0_101\bin\java.exe" -Dpython.home="C:\jython2.2.1" -classpath "C:\jython2.2.1\jython.jar;%CLASSPATH%" org.python.util.jython %ARGS%

1 个答案:

答案 0 :(得分:1)

我今天早上和我的团队讨论了这个问题,我们已经弄明白为什么我在Linux环境中遇到这种情况,所以我想我会发布一个答案,以防其他人帮助。我的一个队友为WLST编写了一个我忽略的启动脚本:

$ cat wlst
#!/bin/sh
wlst.sh -skipWLSModuleScanning $@

我案中的罪魁祸首是-skipWLSModuleScanning旗帜。来自the documentation

  

使用此选项可以通过跳过WebLogic Server模块的程序包扫描和缓存来缩短启动时间。

跳过程序包扫描似乎必须跳过一些影响Jython解析命令行参数的方法。

以下是跳过WLS模块扫描时的结果:

$ wlst ~/pytest.py a b 'c,d,e'

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

['pytest.py', 'a', 'b', 'c', 'd', 'e']

以下是标准wlst.sh调用的结果,这是我期望的结果:

$ wlst.sh ~/pytest.py a b 'c,d,e'

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

['/home/tdmsadm/pytest.py', 'a', 'b', 'c,d,e']