Skip to content

Commit a7cabd6

Browse files
Added source version string
1 parent 33dc3eb commit a7cabd6

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

browserstack/local.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
from ast import Try
2+
from re import T
13
import subprocess, os, time, json, psutil
24
from browserstack.local_binary import LocalBinary
35
from browserstack.bserrors import BrowserStackLocalError
46

7+
try:
8+
from importlib.metadata import version as package_version, PackageNotFoundError
9+
except:
10+
import pkg_resources
11+
12+
513
class Local:
614
def __init__(self, key=None, binary_path=None, **kwargs):
715
self.key = os.environ['BROWSERSTACK_ACCESS_KEY'] if 'BROWSERSTACK_ACCESS_KEY' in os.environ else key
@@ -18,8 +26,30 @@ def __xstr(self, key, value):
1826
else:
1927
return ['-' + key, value]
2028

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+
2151
def _generate_cmd(self):
22-
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()]
2353
for o in self.options.keys():
2454
if self.options.get(o) is not None:
2555
cmd = cmd + self.__xstr(o, self.options.get(o))
@@ -51,6 +81,9 @@ def start(self, **kwargs):
5181
if "onlyCommand" in kwargs and kwargs["onlyCommand"]:
5282
return
5383

84+
if 'source' in self.options:
85+
del self.options['source']
86+
5487
self.proc = subprocess.Popen(self._generate_cmd(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
5588
(out, err) = self.proc.communicate()
5689

0 commit comments

Comments
 (0)