From 03df99c1c96e369b5615d188d6a33d4607a54d85 Mon Sep 17 00:00:00 2001 From: Abhishek Choudhary Date: Mon, 11 Jan 2021 13:05:06 +0530 Subject: [PATCH] Adding source as binding platform + version for tracing through the infrastructure. Separating version into a separate readable file to follow canonical Python packaging practice + relevant unit test. More details in code comments. --- browserstack/__init__.py | 7 +++++++ browserstack/local.py | 12 +++++++++++- browserstack/version.py | 1 + setup.py | 13 ++++++++++++- tests/test_local.py | 3 +++ 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 browserstack/version.py diff --git a/browserstack/__init__.py b/browserstack/__init__.py index e69de29..d9e3567 100644 --- a/browserstack/__init__.py +++ b/browserstack/__init__.py @@ -0,0 +1,7 @@ +import version + +# Python modules report to module_name.__version__ with their version +# Ref: https://www.python.org/dev/peps/pep-0008/#module-level-dunder-names +# With this, browserstack.__version__ will respond with + +__version__ = version.__version__ diff --git a/browserstack/local.py b/browserstack/local.py index 43bc237..b8a6b2c 100644 --- a/browserstack/local.py +++ b/browserstack/local.py @@ -1,7 +1,14 @@ import subprocess, os, time, json, psutil +import version from browserstack.local_binary import LocalBinary from browserstack.bserrors import BrowserStackLocalError + +# Python modules report to module_name.__version__ with their version +# Ref: https://www.python.org/dev/peps/pep-0008/#module-level-dunder-names +# With this, browserstack.local.__version__ will respond with +__version__ = version.__version__ + class Local: def __init__(self, key=None, binary_path=None, **kwargs): self.key = os.environ['BROWSERSTACK_ACCESS_KEY'] if 'BROWSERSTACK_ACCESS_KEY' in os.environ else key @@ -16,8 +23,11 @@ def __xstr(self, key, value): else: return ['-' + key, value] + def _get_version(self): + return __version__ + def _generate_cmd(self): - cmd = [self.binary_path, '-d', 'start', '-logFile', self.local_logfile_path, self.key] + cmd = [self.binary_path, '-d', 'start','--source', 'python-' + self._get_version() , '-logFile', self.local_logfile_path, self.key] for o in self.options.keys(): if self.options.get(o) is not None: cmd = cmd + self.__xstr(o, self.options.get(o)) diff --git a/browserstack/version.py b/browserstack/version.py new file mode 100644 index 0000000..10aa336 --- /dev/null +++ b/browserstack/version.py @@ -0,0 +1 @@ +__version__ = "1.2.3" diff --git a/setup.py b/setup.py index 342bcbe..7275665 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,14 @@ + + +# Need to read the single-source-of-truth version file as text +# Importing version_file here can open the door to a cyclic dependency +def get_version(a_path): + with open(a_path, 'r') as version_file: + for a_line in version_file: + if '__version__' in a_line: + # Cleaning up all spaces and quotes around a string of the form __version__ = "x.y.z" + return a_line.split("=")[1].split()[0].replace('"', '').replace("'","") + raise RuntimeError("Unable to find version string.") try: from setuptools import setup except ImportError: @@ -5,7 +16,7 @@ setup( name = 'browserstack-local', packages = ['browserstack'], - version = '1.2.2', + version = get_version("browserstack/version.py"), description = 'Python bindings for Browserstack Local', author = 'BrowserStack', author_email = 'support@browserstack.com', diff --git a/tests/test_local.py b/tests/test_local.py index 3d455d3..446a2da 100644 --- a/tests/test_local.py +++ b/tests/test_local.py @@ -27,6 +27,9 @@ def test_multiple(self): except BrowserStackLocalError as e: self.assertEqual(str(e), "Either another browserstack local client is running on your machine or some server is listening on port 45691") + def test_version(self): + self.assertEqual("1.2.3", self.local._get_version()) + def test_verbose(self): self.local.start(v=True, onlyCommand=True) self.assertIn('-v', self.local._generate_cmd())