Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 610
cseaton Avatar asked Mar 08 '26 23:03

cseaton


2 Answers

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.

like image 131
cseaton Avatar answered Mar 10 '26 12:03

cseaton


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

like image 39
tangentstorm Avatar answered Mar 10 '26 13:03

tangentstorm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!