From d6a433e4671f4e376b75653ff2c59e9fc39d7412 Mon Sep 17 00:00:00 2001 From: "Mike A. Trethewey" Date: Wed, 17 Mar 2021 17:50:51 -0700 Subject: [PATCH] Add pathing intelligence --- resources/ci/common/common.py | 3 ++ resources/ci/common/get_get_pip.py | 83 ++++++++++++++++-------------- resources/ci/common/install.py | 27 +++++++--- resources/ci/common/my_path.py | 33 ++++++++++++ 4 files changed, 99 insertions(+), 47 deletions(-) create mode 100644 resources/ci/common/my_path.py diff --git a/resources/ci/common/common.py b/resources/ci/common/common.py index 0dac9fb2..b89a266b 100644 --- a/resources/ci/common/common.py +++ b/resources/ci/common/common.py @@ -1,6 +1,7 @@ import os # for env vars import stat # file statistics import sys # default system info +from my_path import get_py_path global UBUNTU_VERSIONS global DEFAULT_EVENT @@ -45,6 +46,8 @@ def prepare_env(): APP_VERSION = f.readlines()[0].strip() # ci data env["CI_SYSTEM"] = os.getenv("CI_SYSTEM","") + # py data + (env["PYTHON_EXE_PATH"],env["PY_EXE_PATH"],env["PIP_EXE_PATH"]) = get_py_path() # git data env["BRANCH"] = os.getenv("TRAVIS_BRANCH","") env["GITHUB_ACTOR"] = os.getenv("GITHUB_ACTOR","MegaMan.EXE") diff --git a/resources/ci/common/get_get_pip.py b/resources/ci/common/get_get_pip.py index 4724ebd7..a0e127ba 100644 --- a/resources/ci/common/get_get_pip.py +++ b/resources/ci/common/get_get_pip.py @@ -1,53 +1,54 @@ import common import argparse +import os import urllib.request, ssl import subprocess # do stuff at the shell level env = common.prepare_env() def get_get_pip(PY_VERSION): - try: - import pip - except ImportError: - print("Getting pip getter!") - #make the request! - url = "https://bootstrap.pypa.io/get-pip.py" - context = ssl._create_unverified_context() - req = urllib.request.urlopen(url, context=context) - got_pip = req.read().decode("utf-8") + try: + import pip + except ImportError: + print("Getting pip getter!") + #make the request! + url = "https://bootstrap.pypa.io/get-pip.py" + context = ssl._create_unverified_context() + req = urllib.request.urlopen(url, context=context) + got_pip = req.read().decode("utf-8") - with open("get-pip.py", "w") as g: - req = urllib.request.Request( - url, - data=None, - headers={ - "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" - } - ) - req = urllib.request.urlopen(req, context=context) - data = req.read().decode("utf-8") - g.write(data) + with open("get-pip.py", "w") as g: + req = urllib.request.Request( + url, + data=None, + headers={ + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" + } + ) + req = urllib.request.urlopen(req, context=context) + data = req.read().decode("utf-8") + g.write(data) - # get executables - # python - # linux/windows: python - # macosx: python3 - PYTHON_EXECUTABLE = "python3" if "osx" in env["OS_NAME"] else "python" - if PY_VERSION == None: - PY_VERSION = 0 + # get executables + # python + # linux/windows: python + # macosx: python3 + PYTHON_EXECUTABLE = "python3" if "osx" in env["OS_NAME"] else "python" + if PY_VERSION == None: + PY_VERSION = 0 - if float(PY_VERSION) > 0: - PYTHON_EXECUTABLE = "py" + if float(PY_VERSION) > 0: + PYTHON_EXECUTABLE = "py" - print("Getting pip!") - args = [ - PYTHON_EXECUTABLE, - '-' + str(PY_VERSION), - "get-pip.py" - ] - if PY_VERSION == 0: - del args[1] - subprocess.check_call(args) + print("Getting pip!") + args = [ + env["PYTHON_EXE_PATH"] + PYTHON_EXECUTABLE, + '-' + str(PY_VERSION), + "get-pip.py" + ] + if PY_VERSION == 0: + del args[1] + subprocess.check_call(args) if __name__ == "__main__": parser = argparse.ArgumentParser(add_help=False) @@ -55,4 +56,8 @@ if __name__ == "__main__": command_line_args = parser.parse_args() PY_VERSION = vars(command_line_args)["py"] - get_get_pip(PY_VERSION) + try: + import pip + print("pip is installed") + except ImportError: + get_get_pip(PY_VERSION) diff --git a/resources/ci/common/install.py b/resources/ci/common/install.py index f32b4866..0a541742 100644 --- a/resources/ci/common/install.py +++ b/resources/ci/common/install.py @@ -1,5 +1,7 @@ import common import argparse +import os +import platform import subprocess # do stuff at the shell level env = common.prepare_env() @@ -12,7 +14,9 @@ def run_install(PY_VERSION,USER): # pip # linux/macosx: pip3 # windows: pip + PYTHON_PATH = env["PYTHON_EXE_PATH"] PYTHON_EXECUTABLE = "python3" if "osx" in env["OS_NAME"] else "python" + PIP_PATH = env["PIP_EXE_PATH"] PIP_EXECUTABLE = "pip" if "windows" in env["OS_NAME"] else "pip3" PIP_EXECUTABLE = "pip" if "osx" in env["OS_NAME"] and "actions" in env["CI_SYSTEM"] else PIP_EXECUTABLE @@ -23,13 +27,17 @@ def run_install(PY_VERSION,USER): if float(PY_VERSION) > 0: PYTHON_EXECUTABLE = "py" - print("Installing to Python %.1f" % float(PY_VERSION)) - if USER: - print("Installing packages at User level") + PYTHON_PATH = env["PY_EXE_PATH"] + print("Installing to Python %.1f via Py Launcher" % float(PY_VERSION)) + else: + print("Installing to Python %s" % platform.python_version()) + print("Installing packages at %s level" % ("User" if USER else "Global")) + print() + print("Upgrading pip-") # upgrade pip args = [ - PYTHON_EXECUTABLE, + PYTHON_PATH + PYTHON_EXECUTABLE, '-' + str(PY_VERSION), "-m", "pip", @@ -44,12 +52,11 @@ def run_install(PY_VERSION,USER): del args[1] subprocess.check_call(args) - # pip version - subprocess.check_call([PIP_EXECUTABLE,"--version"]) # if pip3, install wheel if PIP_EXECUTABLE == "pip3": + print("Installing Wheel!") args = [ - PIP_EXECUTABLE, + PIP_PATH + PIP_EXECUTABLE, "install", "--user", "-U", @@ -58,9 +65,13 @@ def run_install(PY_VERSION,USER): if not USER: args.remove("--user") subprocess.check_call(args) + + print() # install listed dependencies + print("Installing dependencies") + print("-----------------------") args = [ - PIP_EXECUTABLE, + PIP_PATH + PIP_EXECUTABLE, "install", "--user", "-r", diff --git a/resources/ci/common/my_path.py b/resources/ci/common/my_path.py new file mode 100644 index 00000000..7d91f15e --- /dev/null +++ b/resources/ci/common/my_path.py @@ -0,0 +1,33 @@ +import os +import sys + +def get_py_path(): + user_paths = os.environ["PATH"].split(os.pathsep) + (python,py) = ("","") + + for path in user_paths: + parts = path.split(os.sep) + part = parts[len(parts) - 1].lower() + if ("python" in part) and ('.' not in part): + path.replace(os.sep,os.sep + os.sep) + if path not in user_paths: + py = path + + for path in sys.path: + parts = path.split(os.sep) + part = parts[len(parts) - 1].lower() + if ("python" in part) and ('.' not in part): + path.replace(os.sep,os.sep + os.sep) + if path not in user_paths: + python = path + + paths = ( + os.path.join(python,"") if python != "" else "", + os.path.join(py,"") if py != "" else "", + os.path.join(python,"Scripts","") if python != "" else "" + ) + # print(paths) + return paths + +if __name__ == "__main__": + get_py_path()