Get Cygwin installation path in a Python script
I'm writing a cross-platform python script that needs to know if and where Cygwin is installed if the platform is NT. Right now I'm just using a naive check for the existence of the default install path 'C:\Cygwin'. I would like to be able to determine th开发者_JAVA百科e installation path programmatically.
The Windows registry doesn't appear to be an option since Cygwin no longer stores it's mount points in the registry. Because of this is it even possible to programmatically get a Cygwin installation path?
Valid for Cygwin 1.7 only:
You need to check both HKEY_CURRENT_USER and HKEY_LOCAL_MAHINE for the Cygwin registry key. Depending on how Cygwin was installed it could be under either key.
The following is an example of how to query the value using the current user.
CYGWIN_KEY = "SOFTWARE\\Cygwin\\setup"
hk_user = winreg.HKEY_CURRENT_USER
key = winreg.OpenKey(hk_user, CYGWIN_KEY)
root = winreg.QueryValueEx(key, "rootdir")[0]
When writing a script you should probably check the global HKEY_LOCAL_MACHINE first. However, keep in mind that it is possible to have multiple installations of Cygwin on a single machine.
That's what I'd do. There are registry entries for the cygwin drive mount points:
http://www.cygwin.com/ml/cygwin/2004-12/msg00200.html
You can use the _winreg (or winreg in python 3.0) module to look the values up:
http://docs.python.org/library/_winreg.html
You can use the HKEY_LOCAL_MACHINE\SOFTWARE\Cygwin\setup\rootdir
value for Cygwin 1.7
精彩评论