@@ -33,9 +33,34 @@ def obtain_auth_code(listen_port, auth_uri=None): # Historically only used in t
3333 ).get ("code" )
3434
3535
36+ def is_wsl ():
37+ # "Official" way of detecting WSL: https://github.com/Microsoft/WSL/issues/423#issuecomment-221627364
38+ # Run `uname -a` to get 'release' without python
39+ # - WSL 1: '4.4.0-19041-Microsoft'
40+ # - WSL 2: '4.19.128-microsoft-standard'
41+ import platform
42+ uname = platform .uname ()
43+ platform_name = getattr (uname , 'system' , uname [0 ]).lower ()
44+ release = getattr (uname , 'release' , uname [2 ]).lower ()
45+ return platform_name == 'linux' and 'microsoft' in release
46+
47+
3648def _browse (auth_uri ): # throws ImportError, possibly webbrowser.Error in future
3749 import webbrowser # Lazy import. Some distro may not have this.
38- return webbrowser .open (auth_uri ) # Use default browser. Customizable by $BROWSER
50+ browser_opened = webbrowser .open (auth_uri ) # Use default browser. Customizable by $BROWSER
51+
52+ # In WSL which doesn't have www-browser, try launching browser with PowerShell
53+ if not browser_opened and is_wsl ():
54+ try :
55+ import subprocess
56+ # https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe
57+ # Ampersand (&) should be quoted
58+ exit_code = subprocess .call (
59+ ['powershell.exe' , '-NoProfile' , '-Command' , 'Start-Process "{}"' .format (auth_uri )])
60+ browser_opened = exit_code == 0
61+ except FileNotFoundError : # WSL might be too old
62+ pass
63+ return browser_opened
3964
4065
4166def _qs2kv (qs ):
@@ -245,4 +270,3 @@ def __exit__(self, exc_type, exc_val, exc_tb):
245270 timeout = 60 ,
246271 state = flow ["state" ], # Optional
247272 ), indent = 4 ))
248-
0 commit comments