我仍然在学习...
使用 python 我想从 shell 输出中提取版本号以确定是否需要升级。
我能够将 subprocess.call 与 一起使用shell=true,但是我读到这是一个安全问题,并且想要一些关于更好方法的建议。然后我打了一个,AttributeError因为它似乎StrictVersion没有将输出视为整数,我想?
以下是我目前正在做的事情。
import subprocess
from distutils.version import StrictVersion
def updateAnsible():
print 'Checking Ansible version'
version = subprocess.call("ansible --version | grep 'ansible [0-9].[0-9].[0-9]' | awk '{ print $2 }'", shell=True)
print version
if StrictVersion(version) < StrictVersion('2.7.0'):
print "Need to upgrade"
else:
print "Do not need to upgrade"
if __name__ == '__main__':
updateAnsible()
我希望 StrictVersion(version) 的输出是 1.2.3
但我得到的是下面的
Checking Ansible version
1.2.3
Traceback (most recent call last):
0
File "test.py", line 32, in <module>
updateAnsible()
File "test.py", line 26, in updateAnsible
if StrictVersion(version) < StrictVersion('2.6.0'):
File "python2.7/distutils/version.py", line 140, in __cmp__
compare = cmp(self.version, other.version)
AttributeError: StrictVersion instance has no attribute 'version'
Process finished with exit code 1
红糖糍粑
相关分类