Skip to content
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

detecting os and windows path #25

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

andersoncarlosfs
Copy link

Detecting OS and installation path of Chrome on Windows

@danielkaiser
Copy link
Owner

Hi @andersoncarlosfs
thnaks for the PR and sorry for the late response - the last days were quite busy for me.

I just had a look at your code and I'm wondering if this approach has any advantages over the much simpler way of querying the windows registry? This code gives the complete version string at least on my system and even does not require the exact location of the Chrome installation:

import winreg
with winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER, r"Software\Google\Chrome\BLBeacon") as key:
   version = winreg.QueryValueEx(key, "version")[0]

With the check for Windows/Cygwin you added and your refactored get_major_version lambda this might be sufficient and require much less code to maintain.
Could you check if this also works for your use case?

@danielkaiser
Copy link
Owner

Hi @andersoncarlosfs ,

any update on this?

@andersoncarlosfs
Copy link
Author

Hello @danielkaiser,

Well, I was testing your suggestion and I found that it does not work if Chromium/Chrome was not installed by a wizard. However, my solution still works in any case. Besides, I found an answer on Stack Overflow proposing a code less complex:

import subprocess
output = subprocess.check_output(
    r'wmic datafile where name="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe" get Version /value',
    shell=True
)
print(output.decode('utf-8').strip())
Screenshot 2023-05-15 at 11 55 10

@danielkaiser
Copy link
Owner

Hi @andersoncarlosfs, sorry for the late response, I haven't had access to my windows machine lately to test things.

The issue I see with this solution is that it needs to know the exact file path (for example I have the 64bit version of Chrome installed and thus had to remove (x86)from the path to make it work. chromium probably has a different name for the executable and there might be users with non-standard installation paths as well.

As there seems to be no perfect solution for Windows a combination of the last two discussed options might be the best option. So getting the information from registry and if that fails try to find chrome.exe and get the version from there?

Also I would think it would be better to remove the shell=True from the call in your comment and instead pass the command args as a list:

output = subprocess.check_output(['wmic', 'datafile', 'where', r'name="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"', 'get', 'Version', '/value'])

For a hardcoded string this should be no issue but as this part might be changed in the future this could be a problem

@andersoncarlosfs
Copy link
Author

import winreg
with winreg.OpenKeyEx(winreg.HKEY_CURRENT_USER, r"Software\Google\Chrome\BLBeacon") as key:
   version = winreg.QueryValueEx(key, "version")[0]

Done!

@andersoncarlosfs
Copy link
Author

Hi @andersoncarlosfs, sorry for the late response, I haven't had access to my windows machine lately to test things.

The issue I see with this solution is that it needs to know the exact file path (for example I have the 64bit version of Chrome installed and thus had to remove (x86)from the path to make it work. chromium probably has a different name for the executable and there might be users with non-standard installation paths as well.

As there seems to be no perfect solution for Windows a combination of the last two discussed options might be the best option. So getting the information from registry and if that fails try to find chrome.exe and get the version from there?

Also I would think it would be better to remove the shell=True from the call in your comment and instead pass the command args as a list:

output = subprocess.check_output(['wmic', 'datafile', 'where', r'name="C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"', 'get', 'Version', '/value'])

For a hardcoded string this should be no issue but as this part might be changed in the future this could be a problem

Done!

@andersoncarlosfs
Copy link
Author

Hello @danielkaiser ,

Please, feel free to make suggestions!

Have a nice day,

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

Successfully merging this pull request may close these issues.

2 participants