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