|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Requirements Manager class to pull in client dependencies from a .txt or .yml file""" |
| 14 | +from __future__ import absolute_import |
| 15 | +import logging |
| 16 | +import os |
| 17 | +import subprocess |
| 18 | + |
| 19 | +from typing import Optional |
| 20 | + |
| 21 | +logger = logging.getLogger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +class RequirementsManager: |
| 25 | + """Manages dependency installation by detecting file types""" |
| 26 | + |
| 27 | + def capture_and_install_dependencies(self, dependencies: Optional[str] = None) -> str: |
| 28 | + """Detects the type of file dependencies will be installed from |
| 29 | +
|
| 30 | + If a req.txt or conda.yml file is provided, it verifies their existence and |
| 31 | + returns the local file path |
| 32 | +
|
| 33 | + Args: |
| 34 | + dependencies (str): Local path where dependencies file exists. |
| 35 | +
|
| 36 | + Returns: |
| 37 | + file path of the existing or generated dependencies file |
| 38 | + """ |
| 39 | + _dependencies = dependencies or self._detect_conda_env_and_local_dependencies() |
| 40 | + |
| 41 | + # Dependencies specified as either req.txt or conda_env.yml |
| 42 | + if _dependencies.endswith(".txt"): |
| 43 | + self._install_requirements_txt() |
| 44 | + elif _dependencies.endswith(".yml"): |
| 45 | + self._update_conda_env_in_path() |
| 46 | + else: |
| 47 | + raise ValueError(f'Invalid dependencies provided: "{_dependencies}"') |
| 48 | + |
| 49 | + def _install_requirements_txt(self): |
| 50 | + """Install requirements.txt file using pip""" |
| 51 | + logger.info("Running command to pip install") |
| 52 | + subprocess.run("pip install -r in_process_requirements.txt", shell=True, check=True) |
| 53 | + logger.info("Command ran successfully") |
| 54 | + |
| 55 | + def _update_conda_env_in_path(self): |
| 56 | + """Update conda env using conda yml file""" |
| 57 | + logger.info("Updating conda env") |
| 58 | + subprocess.run("conda env update -f conda_in_process.yml", shell=True, check=True) |
| 59 | + logger.info("Conda env updated successfully") |
| 60 | + |
| 61 | + def _get_active_conda_env_name(self) -> str: |
| 62 | + """Returns the conda environment name from the set environment variable. None otherwise.""" |
| 63 | + return os.getenv("CONDA_DEFAULT_ENV") |
| 64 | + |
| 65 | + def _get_active_conda_env_prefix(self) -> str: |
| 66 | + """Returns the conda prefix from the set environment variable. None otherwise.""" |
| 67 | + return os.getenv("CONDA_PREFIX") |
| 68 | + |
| 69 | + def _detect_conda_env_and_local_dependencies(self) -> str: |
| 70 | + """Generates dependencies list from the user's local runtime. |
| 71 | +
|
| 72 | + Raises RuntimeEnvironmentError if not able to. |
| 73 | +
|
| 74 | + Currently supports: conda environments |
| 75 | + """ |
| 76 | + |
| 77 | + # Try to capture dependencies from the conda environment, if any. |
| 78 | + conda_env_name = self._get_active_conda_env_name() |
| 79 | + logger.info("Found conda_env_name: '%s'", conda_env_name) |
| 80 | + conda_env_prefix = None |
| 81 | + |
| 82 | + if conda_env_name is None: |
| 83 | + conda_env_prefix = self._get_active_conda_env_prefix() |
| 84 | + |
| 85 | + if conda_env_name is None and conda_env_prefix is None: |
| 86 | + local_dependencies_path = os.path.join(os.getcwd(), "in_process_requirements.txt") |
| 87 | + logger.info(local_dependencies_path) |
| 88 | + |
| 89 | + return local_dependencies_path |
| 90 | + |
| 91 | + if conda_env_name == "base": |
| 92 | + logger.warning( |
| 93 | + "We recommend using an environment other than base to " |
| 94 | + "isolate your project dependencies from conda dependencies" |
| 95 | + ) |
| 96 | + |
| 97 | + local_dependencies_path = os.path.join(os.getcwd(), "conda_in_process.yml") |
| 98 | + logger.info(local_dependencies_path) |
| 99 | + |
| 100 | + return local_dependencies_path |
0 commit comments