Skip to content

Commit 5f28219

Browse files
authored
Merge pull request #198 from rstudio/mm-exclude-envs
Exclude environment directories
2 parents 9fb791d + bcad14a commit 5f28219

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

rsconnect/bundle.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"__pycache__/",
3333
"env/",
3434
"packrat/",
35+
"renv/",
3536
"rsconnect-python/",
3637
"rsconnect/",
3738
"venv/",
@@ -401,6 +402,23 @@ def create_glob_set(directory, excludes):
401402
return GlobSet(work)
402403

403404

405+
def is_environment_dir(directory):
406+
python_path = join(directory, "bin", "python")
407+
return exists(python_path)
408+
409+
410+
def list_environment_dirs(directory):
411+
# type: (...) -> typing.List[str]
412+
"""Returns a list of subdirectories in `directory` that appear to contain virtual environments."""
413+
envs = []
414+
415+
for name in os.listdir(directory):
416+
path = join(directory, name)
417+
if is_environment_dir(path):
418+
envs.append(name)
419+
return envs
420+
421+
404422
def _create_api_file_list(
405423
directory, # type: str
406424
requirements_file_name, # type: str
@@ -429,6 +447,7 @@ def _create_api_file_list(
429447
excludes = list(excludes) if excludes else []
430448
excludes.append("manifest.json")
431449
excludes.append(requirements_file_name)
450+
excludes.extend(list_environment_dirs(directory))
432451
glob_set = create_glob_set(directory, excludes)
433452

434453
file_list = []
@@ -470,6 +489,9 @@ def make_api_manifest(
470489
:param excludes: a sequence of glob patterns that will exclude matched files.
471490
:return: the manifest and a list of the files involved.
472491
"""
492+
if is_environment_dir(directory):
493+
excludes = list(excludes or []) + ["bin/", "lib/"]
494+
473495
relevant_files = _create_api_file_list(directory, environment.filename, extra_files, excludes)
474496
manifest = make_source_manifest(entry_point, environment, app_mode)
475497

rsconnect/main.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
)
4040

4141
from . import api
42-
from .bundle import make_manifest_bundle
42+
from .bundle import is_environment_dir, make_manifest_bundle
4343
from .metadata import ServerStore, AppStore
4444
from .models import AppModes
4545

@@ -403,6 +403,20 @@ def _warn_if_no_requirements_file(directory):
403403
)
404404

405405

406+
def _warn_if_environment_directory(directory):
407+
"""
408+
Issue a warning if the deployment directory is itself a virtualenv (yikes!).
409+
410+
:param directory: the directory to check in.
411+
"""
412+
if is_environment_dir(directory):
413+
click.secho(
414+
" Warning: The deployment directory appears to be a python virtual environment.\n"
415+
" Excluding the 'bin' and 'lib' directories.",
416+
fg="yellow",
417+
)
418+
419+
406420
def _warn_on_ignored_conda_env(environment):
407421
"""
408422
Checks for a discovered Conda environment and produces a warning that it will be ignored when
@@ -575,8 +589,10 @@ def deploy_notebook(
575589

576590
click.secho(' Deploying %s to server "%s"' % (file, connect_server.url))
577591

578-
_warn_on_ignored_manifest(dirname(file))
579-
_warn_if_no_requirements_file(dirname(file))
592+
base_dir = dirname(file)
593+
_warn_on_ignored_manifest(base_dir)
594+
_warn_if_no_requirements_file(base_dir)
595+
_warn_if_environment_directory(base_dir)
580596

581597
with cli_feedback("Inspecting Python environment"):
582598
python, environment = get_python_env_info(file, python, conda, force_generate)
@@ -588,7 +604,7 @@ def deploy_notebook(
588604
_warn_on_ignored_conda_env(environment)
589605

590606
if force_generate:
591-
_warn_on_ignored_requirements(dirname(file), environment.filename)
607+
_warn_on_ignored_requirements(base_dir, environment.filename)
592608

593609
with cli_feedback("Creating deployment bundle"):
594610
bundle = create_notebook_deployment_bundle(
@@ -881,6 +897,7 @@ def _deploy_by_framework(
881897

882898
_warn_on_ignored_manifest(directory)
883899
_warn_if_no_requirements_file(directory)
900+
_warn_if_environment_directory(directory)
884901

885902
with cli_feedback("Inspecting Python environment"):
886903
_, environment = get_python_env_info(module_file, python, conda, force_generate)

0 commit comments

Comments
 (0)