Skip to content

Commit

Permalink
Merge pull request #508 from rstudio/sagerb-handle-pip-notices
Browse files Browse the repository at this point in the history
Filter out pip freeze output starting w/ "[notice]"
  • Loading branch information
sagerb authored Oct 23, 2023
2 parents b6cbcbc + 4cfbdf5 commit f11851c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Common environment directories (`env, venv, .env, .venv`) are no longer
excluded by name. Environments are detected by the presence of a python
executable in `bin` or `Scripts` and excluded.
- Lines output from `pip freeze` which start with [notice] are filtered out from the generated `requirements.txt`.

### Added
- Added support for the `no_proxy` or `NO_PROXY` environment variables to specify
Expand Down
13 changes: 10 additions & 3 deletions rsconnect/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ def output_file(dirname, filename, package_manager):


def pip_freeze():
"""Inspect the environment using `pip freeze`.
"""Inspect the environment using `pip freeze --disable-pip-version-check version`.
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", "freeze"],
[sys.executable, "-m", "pip", "freeze", "--disable-pip-version-check"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
Expand All @@ -168,7 +168,7 @@ def pip_freeze():
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 = filter_pip_freeze_output(pip_stdout)

pip_stdout = (
"# requirements.txt generated by rsconnect-python on " + str(datetime.datetime.utcnow()) + "\n" + pip_stdout
Expand All @@ -182,6 +182,13 @@ def pip_freeze():
}


def filter_pip_freeze_output(pip_stdout):
# Filter out dependency on `rsconnect` and ignore output lines from pip which start with `[notice]`
return "\n".join(
[line for line in pip_stdout.split("\n") if (("rsconnect" not in line) and (line.find("[notice]") != 0))]
)


def strip_ref(line):
# remove erroneous conda build paths that will break pip install
return line.split(" @ file:", 1)[0].strip()
Expand Down
2 changes: 1 addition & 1 deletion rsconnect/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ def _warn_if_no_requirements_file(directory):
"""
if not exists(join(directory, "requirements.txt")):
click.secho(
" Warning: Capturing the environment using 'pip freeze'.\n"
" Warning: Capturing the environment using 'pip freeze --disable-pip-version-check'.\n"
" Consider creating a requirements.txt file instead.",
fg="yellow",
)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
detect_environment,
get_default_locale,
get_python_version,
filter_pip_freeze_output,
)
from .utils import get_dir

Expand Down Expand Up @@ -72,3 +73,24 @@ def test_pip_freeze(self):
source="pip_freeze",
)
self.assertEqual(expected, result)

def test_filter_pip_freeze_output(self):
raw_stdout = "numpy\npandas\n[notice] A new release of pip is available: 23.1.2 -> 23.3\n\
[notice] To update, run: pip install --upgrade pip"
filtered = filter_pip_freeze_output(raw_stdout)
expected = "numpy\npandas"

self.assertEqual(filtered, expected)

raw_stdout = "numpy\npandas"
filtered = filter_pip_freeze_output(raw_stdout)
expected = "numpy\npandas"

self.assertEqual(filtered, expected)

raw_stdout = "numpy\npandas\nnot at beginning [notice]\n\
[notice] To update, run: pip install --upgrade pip"
filtered = filter_pip_freeze_output(raw_stdout)
expected = "numpy\npandas\nnot at beginning [notice]"

self.assertEqual(filtered, expected)

0 comments on commit f11851c

Please sign in to comment.