获取 OS X 的版本信息
我有一个旧的脚本,之前在Ubuntu系统上运行。现在我换了Mac,想把这个脚本再用一遍。
有没有人知道在Mac OS上,下面这些命令相当于什么?
def runCmd(cmd):
p = subprocess.Popen(cmd,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=True)
result=p.stdout.readlines()
s=result[0].split()[0]
return s
def getKernelVer():
cmd="uname -r| cut --delim=\'.\' -f1-2"
return runCmd(cmd)
def getUbuntuVer():
cmd="lsb_release -a | grep Release | cut -f 2"
return runCmd(cmd)
谢谢
2 个回答
1
你可以使用Python的“platform”模块(我没有Ubuntu的访问权限,请你试试看并分享你的发现 :)
- 用platform.system()来区分是Linux还是Darwin(Mac的系统名)
- 调用platform.release()来获取内核版本
- 调用platform.linux_distribution()或者platform.mac_ver()来获取特定厂商的版本号。
在CentOS上:
$ python
Python 2.7.5 (default, Jul 23 2013, 17:26:16)
[GCC 4.7.2 20121015 (Red Hat 4.7.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.system()
'Linux'
>>> platform.release()
'2.6.32-358.18.1.el6.x86_64'
>>> platform.linux_distribution()
('CentOS', '6.4', 'Final')
>>>
在OS X上:
$ python
Python 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import platform
>>> platform.system()
'Darwin'
>>> platform.release()
'13.0.0'
>>> platform.mac_ver()
('10.9', ('', '', ''), 'x86_64')
3
uname -r
在Darwin系统下的工作方式和其他系统一样。内核版本其实是个很少有人讨论或关心的东西,但它确实存在。唯一需要注意的是,cut
这个命令不支持 --delim
这个长选项,所以可以试试下面的方式:
uname -r | cut -d. -f1-2
不过,Darwin的内核版本管理和Linux是有很大不同的,所以在这里使用 cut
的目的并不明确。(实际上,在Linux上也不太清楚,因为从3.0版本开始,版本管理的方式发生了很大变化。)
如果你想查看当前的Mac OS版本(大致相当于你在Ubuntu上获取的“发布”版本),可以使用以下命令:
sw_vers -productVersion