| 1 |
# -*- coding: utf-8 -*- |
| 2 |
""" |
| 3 |
Decides if vendor bundles are used or not. |
| 4 |
Setup python path accordingly. |
| 5 |
""" |
| 6 |
|
| 7 |
from __future__ import absolute_import, print_function |
| 8 |
import os.path |
| 9 |
import sys |
| 10 |
|
| 11 |
# ----------------------------------------------------------------------------- |
| 12 |
# DEFINES: |
| 13 |
# ----------------------------------------------------------------------------- |
| 14 |
HERE = os.path.dirname(__file__) |
| 15 |
TASKS_VENDOR_DIR = os.path.join(HERE, "_vendor") |
| 16 |
INVOKE_BUNDLE = os.path.join(TASKS_VENDOR_DIR, "invoke.zip") |
| 17 |
INVOKE_BUNDLE_VERSION = "0.13.0" # pylint: disable=invalid-name |
| 18 |
|
| 19 |
DEBUG_SYSPATH = False |
| 20 |
|
| 21 |
|
| 22 |
# ----------------------------------------------------------------------------- |
| 23 |
# EXCEPTIONS: |
| 24 |
# ----------------------------------------------------------------------------- |
| 25 |
class VersionRequirementError(SystemExit): |
| 26 |
pass |
| 27 |
|
| 28 |
# ----------------------------------------------------------------------------- |
| 29 |
# FUNCTIONS: |
| 30 |
# ----------------------------------------------------------------------------- |
| 31 |
def 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 |
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 |
|
| 62 |
def 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 |
def need_vendor_bundles(invoke_minversion=None): |
| 90 |
invoke_minversion = invoke_minversion or "0.0.0" |
| 91 |
need_vendor_answers = [] |
| 92 |
need_vendor_answers.append(need_vendor_bundle_invoke(invoke_minversion)) |
| 93 |
# -- REQUIRE: path.py |
| 94 |
try: |
| 95 |
import path |
| 96 |
need_bundle = False |
| 97 |
except ImportError: |
| 98 |
need_bundle = True |
| 99 |
need_vendor_answers.append(need_bundle) |
| 100 |
|
| 101 |
# -- DIAG: print("INVOKE: need_bundle=%s" % need_bundle1) |
| 102 |
# return need_bundle1 or need_bundle2 |
| 103 |
return any(need_vendor_answers) |
| 104 |
|
| 105 |
def need_vendor_bundle_invoke(invoke_minversion="0.0.0"): |
| 106 |
# -- REQUIRE: invoke |
| 107 |
try: |
| 108 |
import invoke |
| 109 |
need_bundle = invoke.__version__ < invoke_minversion |
| 110 |
if need_bundle: |
| 111 |
del sys.modules["invoke"] |
| 112 |
del invoke |
| 113 |
except ImportError: |
| 114 |
need_bundle = True |
| 115 |
except Exception: # pylint: disable=broad-except |
| 116 |
need_bundle = True |
| 117 |
return need_bundle |
| 118 |
|
| 119 |
# ----------------------------------------------------------------------------- |
| 120 |
# UTILITY FUNCTIONS: |
| 121 |
# ----------------------------------------------------------------------------- |
| 122 |
def setup_path_for_bundle(bundle_path, pos=0): |
| 123 |
if os.path.exists(bundle_path): |
| 124 |
syspath_insert(pos, os.path.abspath(bundle_path)) |
| 125 |
return True |
| 126 |
return False |
| 127 |
|
| 128 |
def syspath_insert(pos, path): |
| 129 |
if path in sys.path: |
| 130 |
sys.path.remove(path) |
| 131 |
sys.path.insert(pos, path) |
| 132 |
|
| 133 |
def syspath_append(path): |
| 134 |
if path in sys.path: |
| 135 |
sys.path.remove(path) |
| 136 |
sys.path.append(path) |