55from browserstack .bserrors import BrowserStackLocalError
66
77logger = logging .getLogger (__name__ )
8+ try :
9+ from importlib .metadata import version as package_version , PackageNotFoundError
10+ except :
11+ import pkg_resources
812
913class Local :
1014 def __init__ (self , key = None , binary_path = None , ** kwargs ):
@@ -22,8 +26,30 @@ def __xstr(self, key, value):
2226 else :
2327 return ['-' + key , value ]
2428
29+ def get_package_version (self ):
30+ name = "browserstack-local"
31+ version = 'None'
32+ use_fallback = False
33+ try :
34+ temp = package_version
35+ except NameError : # Only catch if package_version is not defined(and not other errors)
36+ use_fallback = True
37+
38+ if use_fallback :
39+ try :
40+ version = pkg_resources .get_distribution (name ).version
41+ except pkg_resources .DistributionNotFound :
42+ version = 'None'
43+ else :
44+ try :
45+ version = package_version (name )
46+ except PackageNotFoundError :
47+ version = 'None'
48+
49+ return version
50+
2551 def _generate_cmd (self ):
26- cmd = [self .binary_path , '-d' , 'start' , '-logFile' , self .local_logfile_path , "-k" , self .key ]
52+ cmd = [self .binary_path , '-d' , 'start' , '-logFile' , self .local_logfile_path , "-k" , self .key , '--source' , 'python:' + self . get_package_version () ]
2753 for o in self .options .keys ():
2854 if self .options .get (o ) is not None :
2955 cmd = cmd + self .__xstr (o , self .options .get (o ))
@@ -55,7 +81,11 @@ def start(self, **kwargs):
5581 if "onlyCommand" in kwargs and kwargs ["onlyCommand" ]:
5682 return
5783
58- self .proc = subprocess .Popen ('ls' , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
84+
85+ if 'source' in self .options :
86+ del self .options ['source' ]
87+
88+ self .proc = subprocess .Popen (self ._generate_cmd (), stdout = subprocess .PIPE , stderr = subprocess .PIPE )
5989 (out , err ) = self .proc .communicate ()
6090
6191 os .system ('echo "" > "' + self .local_logfile_path + '"' )
0 commit comments