Skip to content

Commit

Permalink
make wrapper for creating environments
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarchetti committed Oct 13, 2020
1 parent 3a0ca37 commit a4f714f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
4 changes: 2 additions & 2 deletions rsconnect/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
manifest_add_file,
read_manifest_file,
)
from .environment import Environment, EnvironmentException
from .environment import Environment, MakeEnvironment, EnvironmentException
from .log import logger
from .metadata import AppStore
from .models import AppModes
Expand Down Expand Up @@ -141,7 +141,7 @@ def inspect_environment(
environment_json = check_output(args, universal_newlines=True)
except subprocess.CalledProcessError as e:
raise api.RSConnectException("Error inspecting environment: %s" % e.output)
return Environment(**json.loads(environment_json)) # type: ignore
return MakeEnvironment(**json.loads(environment_json)) # type: ignore


def _verify_server(connect_server):
Expand Down
36 changes: 19 additions & 17 deletions rsconnect/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

try:
import typing
from typing import Optional
except ImportError:
typing = None

Expand All @@ -25,22 +26,23 @@
exec_dir = os.path.dirname(sys.executable)


if sys.version_info[:2] < (3, 7):
Environment = collections.namedtuple(
"Environment", ("conda", "contents", "error", "filename", "locale", "package_manager", "pip", "python", "source",),
)

Environment = collections.namedtuple(
"Environment",
("conda", "contents", "error", "filename", "locale", "package_manager", "pip", "python", "source",),
)
Environment.__new__.__defaults__ = (None, "", None, "", "", "", None, None, None)

else:

Environment = collections.namedtuple(
"Environment",
("conda", "contents", "error", "filename", "locale", "package_manager", "pip", "python", "source",),
defaults=(None, "", None, "", "", "", None, None, None),
)
Environment.__doc__ = "Data class encapsulating values needed by various deployment and metadata functions"
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):
Expand Down Expand Up @@ -76,20 +78,20 @@ def detect_environment(dirname, force_generate=False, conda_mode=False, conda=No

if result is not None:
if conda_mode and result["package_manager"] != "conda":
return Environment(
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(Environment(**result))
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 Environment(**result)
return MakeEnvironment(**result)


def get_conda(conda=None):
Expand Down
10 changes: 5 additions & 5 deletions tests/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
which_python,
)
from rsconnect.api import RSConnectException, RSConnectServer
from rsconnect.environment import Environment
from rsconnect.environment import MakeEnvironment

from .test_data_util import get_manifest_path, get_api_path, get_dir

Expand Down Expand Up @@ -322,7 +322,7 @@ def test_inspect_environment(self):
False,
False,
sys.executable,
Environment(
MakeEnvironment(
conda=None,
filename="requirements.txt",
locale="en_US.UTF-8",
Expand All @@ -337,7 +337,7 @@ def test_inspect_environment(self):
False,
False,
sys.executable,
Environment(
MakeEnvironment(
conda=None,
filename="requirements.txt",
locale="en_US.UTF-8",
Expand All @@ -352,7 +352,7 @@ def test_inspect_environment(self):
True,
True,
"/very/serious/whython",
Environment(
MakeEnvironment(
conda="/opt/Conda/bin/conda",
filename="requirements.txt",
locale="en_US.UTF-8",
Expand All @@ -367,7 +367,7 @@ def test_inspect_environment(self):
False,
True,
"unused",
Environment(error="Could not even do things"),
MakeEnvironment(error="Could not even do things"),
id="exploding",
),
],
Expand Down
7 changes: 4 additions & 3 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from rsconnect.environment import (
Environment,
EnvironmentException,
MakeEnvironment,
detect_environment,
get_default_locale,
get_python_version,
Expand All @@ -23,7 +24,7 @@ def python_version():

def test_get_python_version(self):
self.assertEqual(
get_python_version(Environment(package_manager="pip")), self.python_version(),
get_python_version(MakeEnvironment(package_manager="pip")), self.python_version(),
)

def test_get_default_locale(self):
Expand All @@ -40,7 +41,7 @@ def test_file(self):
self.assertIsInstance(result.locale, str)
self.assertIn(".", result.locale)

expected = Environment(
expected = MakeEnvironment(
contents="numpy\npandas\nmatplotlib\n",
filename="requirements.txt",
locale=result.locale,
Expand All @@ -63,7 +64,7 @@ def test_pip_freeze(self):
self.assertIsInstance(result.locale, str)
self.assertIn(".", result.locale)

expected = Environment(
expected = MakeEnvironment(
contents=result.contents,
filename="requirements.txt",
locale=result.locale,
Expand Down

0 comments on commit a4f714f

Please sign in to comment.