Skip to content

Use winreg to find GIT_PYTHON_GIT_EXECUTABLE #814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tardyp opened this issue Dec 14, 2018 · 2 comments
Open

Use winreg to find GIT_PYTHON_GIT_EXECUTABLE #814

tardyp opened this issue Dec 14, 2018 · 2 comments

Comments

@tardyp
Copy link

tardyp commented Dec 14, 2018

As per https://stackoverflow.com/questions/8507368/programmatically-not-manually-finding-the-path-where-git-is-installed-on-a-win

It looks like one could use the winreg in order to find git executable. Is this something you tried or are willing to take patch?

@tardyp
Copy link
Author

tardyp commented Dec 17, 2018

FWIW, the following method do works for me:

import sys

if sys.platform == 'win32':
    import os
    import winreg
    def get_git_binary_path():
        proc_arch = os.getenv('PROCESSOR_ARCHITECTURE', '').lower()
        proc_arch64 = os.getenv('PROCESSOR_ARCHITEW6432','').lower()

        if proc_arch == 'x86' and not proc_arch64:
            arch_keys = {0}
        elif proc_arch == 'x86' or proc_arch == 'amd64':
            arch_keys = {winreg.KEY_WOW64_32KEY, winreg.KEY_WOW64_64KEY}
        else:
            raise Exception("Unhandled arch: %s" % proc_arch)

        for arch_key in arch_keys:
            for base_key in [winreg.HKEY_LOCAL_MACHINE, winreg.HKEY_CURRENT_USER]:
                try:
                    with winreg.OpenKey(
                            base_key, r"Software\GitForWindows",
                            0, winreg.KEY_READ | arch_key) as key:
                        path = winreg.QueryValueEx(key, 'InstallPath')[0]
                        git_path = os.path.join(path, 'bin', 'git.exe')
                        if os.path.exists(git_path):
                            return git_path
                except FileNotFoundError:
                    continue
        return None
else:
    # on normal platforms, git is just in the PATH
    def get_git_binary_path():
        return 'git'

@Byron
Copy link
Member

Byron commented Dec 22, 2018

@tardyp thanks a lot for the suggestion and the research!
If you could provide the code above in a PR I would happily merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants