How to use need_vendor_bundles method in Behave

Best Python code snippet using behave

Run Behave automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

_setup.py

Source: _setup.py Github

copy
1# -*- coding: utf-8 -*-
2"""
3Decides if vendor bundles are used or not.
4Setup python path accordingly.
5"""
6
7from __future__ import absolute_import, print_function
8import os.path
9import sys
10
11# -----------------------------------------------------------------------------
12# DEFINES:
13# -----------------------------------------------------------------------------
14HERE = os.path.dirname(__file__)
15TASKS_VENDOR_DIR = os.path.join(HERE, "_vendor")
16INVOKE_BUNDLE = os.path.join(TASKS_VENDOR_DIR, "invoke.zip")
17INVOKE_BUNDLE_VERSION = "0.21.0"
18
19DEBUG_SYSPATH = False
20
21
22# -----------------------------------------------------------------------------
23# EXCEPTIONS:
24# -----------------------------------------------------------------------------
25class VersionRequirementError(SystemExit):
26    pass
27
28# -----------------------------------------------------------------------------
29# FUNCTIONS:
30# -----------------------------------------------------------------------------
31def setup_path(invoke_minversion=None):
32    """Setup python search and add ``TASKS_VENDOR_DIR`` (if available)."""
33    # print("INVOKE.tasks: setup_path")
34    if not os.path.isdir(TASKS_VENDOR_DIR):
35        # SILENCE: print("SKIP: TASKS_VENDOR_DIR=%s is missing" % TASKS_VENDOR_DIR)
36        return
37    elif os.path.abspath(TASKS_VENDOR_DIR) in sys.path:
38        # -- SETUP ALREADY DONE:
39        # return
40        pass
41
42    use_vendor_bundles = os.environ.get("INVOKE_TASKS_USE_VENDOR_BUNDLES", "no")
43    if need_vendor_bundles(invoke_minversion):
44        use_vendor_bundles = "yes"
45
46    if use_vendor_bundles == "yes":
47        syspath_insert(0, os.path.abspath(TASKS_VENDOR_DIR))
48        if setup_path_for_bundle(INVOKE_BUNDLE, pos=1):
49            import invoke
50            bundle_path = os.path.relpath(INVOKE_BUNDLE, os.getcwd())
51            print("USING: %s (version: %s)" % (bundle_path, invoke.__version__))
52    else:
53        # -- BEST-EFFORT: May rescue something
54        syspath_append(os.path.abspath(TASKS_VENDOR_DIR))
55        setup_path_for_bundle(INVOKE_BUNDLE, pos=len(sys.path))
56
57    if DEBUG_SYSPATH:
58        for index, p in enumerate(sys.path):
59            print("  %d.  %s" % (index, p))
60
61
62def require_invoke_minversion(min_version, verbose=False):
63    """Ensures that :mod:`invoke` has at the least the :param:`min_version`.
64    Otherwise,
65
66    :param min_version: Minimal acceptable invoke version (as string).
67    :param verbose:     Indicates if invoke.version should be shown.
68    :raises: VersionRequirementError=SystemExit if requirement fails.
69    """
70    # -- REQUIRES: sys.path is setup and contains invoke
71    try:
72        import invoke
73        invoke_version = invoke.__version__
74    except ImportError:
75        invoke_version = "__NOT_INSTALLED"
76
77    if invoke_version < min_version:
78        message = "REQUIRE: invoke.version >= %s (but was: %s)" % \
79                  (min_version, invoke_version)
80        message += "\nUSE: pip install invoke>=%s" % min_version
81        raise VersionRequirementError(message)
82
83    # pylint: disable=invalid-name
84    INVOKE_VERSION = os.environ.get("INVOKE_VERSION", None)
85    if verbose and not INVOKE_VERSION:
86        os.environ["INVOKE_VERSION"] = invoke_version
87        print("USING: invoke.version=%s" % invoke_version)
88
89
90def need_vendor_bundles(invoke_minversion=None):
91    invoke_minversion = invoke_minversion or "0.0.0"
92    need_vendor_answers = []
93    need_vendor_answers.append(need_vendor_bundle_invoke(invoke_minversion))
94    # -- REQUIRE: path.py
95    try:
96        import path
97        need_bundle = False
98    except ImportError:
99        need_bundle = True
100    need_vendor_answers.append(need_bundle)
101
102    # -- DIAG: print("INVOKE: need_bundle=%s" % need_bundle1)
103    # return need_bundle1 or need_bundle2
104    return any(need_vendor_answers)
105
106
107def need_vendor_bundle_invoke(invoke_minversion="0.0.0"):
108    # -- REQUIRE: invoke
109    try:
110        import invoke
111        need_bundle = invoke.__version__ < invoke_minversion
112        if need_bundle:
113            del sys.modules["invoke"]
114            del invoke
115    except ImportError:
116        need_bundle = True
117    except Exception:   # pylint: disable=broad-except
118        need_bundle = True
119    return need_bundle
120
121
122# -----------------------------------------------------------------------------
123# UTILITY FUNCTIONS:
124# -----------------------------------------------------------------------------
125def setup_path_for_bundle(bundle_path, pos=0):
126    if os.path.exists(bundle_path):
127        syspath_insert(pos, os.path.abspath(bundle_path))
128        return True
129    return False
130
131
132def syspath_insert(pos, path):
133    if path in sys.path:
134        sys.path.remove(path)
135    sys.path.insert(pos, path)
136
137
138def syspath_append(path):
139    if path in sys.path:
140        sys.path.remove(path)
141    sys.path.append(path)
142
143
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Behave on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)