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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
#!/usr/bin/env python
"""
Environment data class abstraction that is usable as an executable module
```bash
python -m rsconnect.environment
```
"""
import collections
import datetime
import json
import locale
import os
import re
import subprocess
import sys
try:
import typing
from typing import Optional
except ImportError:
typing = None
version_re = re.compile(r"\d+\.\d+(\.\d+)?")
conda_version_re = re.compile(r"^(?:\s*-\s*)?python=(\d+\.\d+(?:\.\d+)?)", re.MULTILINE)
exec_dir = os.path.dirname(sys.executable)
Environment = collections.namedtuple(
"Environment", ("conda", "contents", "error", "filename", "locale", "package_manager", "pip", "python", "source",),
)
def MakeEnvironment(
conda=None, # type: Optional[str]
contents="", # type: Optional[str]
error=None, # type: Optional[str]
filename="", # type: Optional[str]
locale="", # type: Optional[str]
package_manager="", # type: Optional[str]
pip=None, # type: Optional[str]
python=None, # type: Optional[str]
source=None, # type: Optional[str]
):
return Environment(conda, contents, error, filename, locale, package_manager, pip, python, source)
class EnvironmentException(Exception):
pass
def detect_environment(dirname, force_generate=False, conda_mode=False, conda=None):
# type: (str, bool, bool, typing.Optional[str]) -> Environment
"""Determine the python dependencies in the environment.
`pip freeze` will be used to introspect the environment unless `conda_mode` is
set to `True`. In that case, an attempt will be made to use Conda to introspect
the environment.
:param: dirname Directory name
:param: force_generate Force the generation of an environment
:param: conda_mode inspect the environment assuming Conda
:return: a dictionary containing the package spec filename and contents if successful,
or a dictionary containing `error` on failure.
"""
conda = get_conda(conda)
if conda_mode and conda:
if force_generate:
result = conda_env_export(conda)
else:
result = output_file(dirname, "environment.yml", "conda") or conda_env_export(conda)
else:
if force_generate:
result = pip_freeze()
else:
result = output_file(dirname, "requirements.txt", "pip") or pip_freeze()
if result is not None:
if conda_mode and result["package_manager"] != "conda":
return MakeEnvironment(
error=(
'Conda was requested but no activated Conda environment was found. See "conda activate '
'--help" for more information.'
)
)
result["python"] = get_python_version(MakeEnvironment(**result))
result["pip"] = get_version("pip")
if conda:
result["conda"] = get_conda_version(conda)
result["locale"] = get_default_locale()
return MakeEnvironment(**result)
def get_conda(conda=None):
"""get_conda tries to find the conda executable if we're in
a conda environment. If not, or if we cannot find the executable,
return None.
:returns: conda string path to conda or None.
"""
if os.environ.get("CONDA_PREFIX", None) is None and conda is None:
return None
else:
return conda or os.environ.get("CONDA_EXE", None)
def get_python_version(environment):
# type: (Environment) -> str
if environment.package_manager == "conda":
versions = conda_version_re.findall(environment.contents)
if len(versions) > 0:
version = versions[0]
if version.count(".") == 1:
version = version + ".0"
return version
v = sys.version_info
return "%d.%d.%d" % (v[0], v[1], v[2])
def get_conda_version(conda):
try:
args = [conda, "-V"]
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True,)
stdout, stderr = proc.communicate()
match = version_re.search(stdout or stderr)
if match:
return match.group()
msg = "Failed to get version of conda from the output of: %s - standard output: %s; standard error: %s" % (
" ".join(args),
stdout,
stderr,
)
raise EnvironmentException(msg)
except Exception as exception:
raise EnvironmentException("Error getting conda version: %s" % str(exception))
def get_default_locale(locale_source=locale.getdefaultlocale):
result = ".".join([item or "" for item in locale_source()])
return "" if result == "." else result
def get_version(module):
try:
args = [sys.executable, "-m", module, "--version"]
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True,)
stdout, stderr = proc.communicate()
match = version_re.search(stdout)
if match:
return match.group()
msg = "Failed to get version of '%s' from the output of: %s" % (module, " ".join(args),)
raise EnvironmentException(msg)
except Exception as exception:
raise EnvironmentException("Error getting '%s' version: %s" % (module, str(exception)))
def output_file(dirname, filename, package_manager):
"""Read an existing package spec file.
Returns a dictionary containing the filename and contents
if successful, None if the file does not exist,
or a dictionary containing 'error' on failure.
"""
try:
path = os.path.join(dirname, filename)
if not os.path.exists(path):
return None
with open(path, "r") as f:
data = f.read()
data = "\n".join([line for line in data.split("\n") if "rsconnect" not in line])
return {
"filename": filename,
"contents": data,
"source": "file",
"package_manager": package_manager,
}
except Exception as exception:
raise EnvironmentException("Error reading %s: %s" % (filename, str(exception)))
def pip_freeze():
"""Inspect the environment using `pip freeze`.
Returns a dictionary containing the filename
(always 'requirements.txt') and contents if successful,
or a dictionary containing 'error' on failure.
"""
try:
proc = subprocess.Popen(
[sys.executable, "-m", "pip", "list", "--format=freeze"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
pip_stdout, pip_stderr = proc.communicate()
pip_status = proc.returncode
except Exception as exception:
raise EnvironmentException("Error during pip freeze: %s" % str(exception))
if pip_status != 0:
msg = pip_stderr or ("exited with code %d" % pip_status)
raise EnvironmentException("Error during pip freeze: %s" % msg)
pip_stdout = "\n".join([line for line in pip_stdout.split("\n") if "rsconnect" not in line])
pip_stdout = (
"# requirements.txt generated by rsconnect-python on " + str(datetime.datetime.utcnow()) + "\n" + pip_stdout
)
return {
"filename": "requirements.txt",
"contents": pip_stdout,
"source": "pip_freeze",
"package_manager": "pip",
}
def conda_env_export(conda):
"""Inspect the environment using `conda env export`
:param: conda path to the `conda` tool
:return: dictionary containing the key "environment.yml" and the data inside
"""
try:
proc = subprocess.Popen(
[conda, "env", "export"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True,
)
conda_stdout, conda_stderr = proc.communicate()
conda_status = proc.returncode
except Exception as exception:
raise EnvironmentException("Error during conda env export: %s" % str(exception))
if conda_status != 0:
msg = conda_stderr or ("exited with code %d" % conda_status)
raise EnvironmentException("Error during conda env export: %s" % msg)
return {
"filename": "environment.yml",
"contents": conda_stdout,
"source": "conda_env_export",
"package_manager": "conda",
}
def main():
"""
Run `detect_environment` and dump the result as JSON.
"""
try:
if len(sys.argv) < 2:
raise EnvironmentException("Usage: %s [-fc] DIRECTORY" % sys.argv[0])
# directory is always the last argument
directory = sys.argv[len(sys.argv) - 1]
flags = ""
force_generate = False
conda_mode = False
if len(sys.argv) > 2:
flags = sys.argv[1]
if "f" in flags:
force_generate = True
if "c" in flags:
conda_mode = True
json.dump(
detect_environment(directory, force_generate, conda_mode)._asdict(), sys.stdout, indent=4,
)
except EnvironmentException as exception:
json.dump(dict(error=str(exception)), sys.stdout, indent=4)
if __name__ == "__main__":
main()
|