views:

225

answers:

3

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 the 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?

+1  A: 

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/%5Fwinreg.html

cygwin 1.7 doesn't use the registry for mount points anymore.
an0nym0usc0ward
Is there another way to get the installation path of Cygwin since it no longer stores mount points in the registry?
chris.nullptr
A: 

You can use the HKEY_LOCAL_MACHINE\SOFTWARE\Cygwin\setup\rootdir value for Cygwin 1.7

zim
That key doesn't exist on my machine.
chris.nullptr
The key was actually under HKKEY_CURRENT_USER because of the way cygwin was installed on my system.
chris.nullptr
+1  A: 

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.

chris.nullptr