From feaed103754dc34bf32af2a265fcd2ccd1838080 Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Tue, 20 Apr 2021 17:41:19 -0500 Subject: [PATCH] Build: Get installed packages This is a PoC to be used to get structured data about the used dependencies. --- readthedocs/doc_builder/environments.py | 15 +++-- .../doc_builder/python_environments.py | 62 +++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/readthedocs/doc_builder/environments.py b/readthedocs/doc_builder/environments.py index 16af312f732..3fb3f961e70 100644 --- a/readthedocs/doc_builder/environments.py +++ b/readthedocs/doc_builder/environments.py @@ -84,6 +84,8 @@ class BuildCommand(BuildCommandResultMixin): or ``user``. Defaults to ``RTD_DOCKER_USER``. :param build_env: build environment to use to execute commands :param bin_path: binary path to add to PATH resolution + :param bool stdout: Include stdout in the output. + :param bool stderr: Include stderr in the output. :param description: a more grokable description of the command being run :param kwargs: allow to subclass this class and extend it """ @@ -97,6 +99,8 @@ def __init__( user=None, build_env=None, bin_path=None, + stdout=True, + stderr=True, description=None, record_as_success=False, **kwargs, @@ -115,6 +119,9 @@ def __init__( self.start_time = None self.end_time = None + self.stdout = stdout + self.stderr = stderr + self.bin_path = bin_path self.description = description or '' self.record_as_success = record_as_success @@ -157,8 +164,8 @@ def run(self): # as we want docker to expand inside the container cwd=os.path.expandvars(self.cwd), stdin=None, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, + stdout=subprocess.PIPE if self.stdout else None, + stderr=subprocess.STDOUT if self.stderr else None, env=environment, ) cmd_stdout, cmd_stderr = proc.communicate() @@ -302,8 +309,8 @@ def run(self): cmd=self.get_wrapped_command(), environment=self.environment, user=self.user, - stdout=True, - stderr=True, + stdout=self.stdout, + stderr=self.stderr, ) cmd_output = client.exec_start(exec_id=exec_cmd['Id'], stream=False) diff --git a/readthedocs/doc_builder/python_environments.py b/readthedocs/doc_builder/python_environments.py index beecb562ea1..27eaa5e7c9c 100644 --- a/readthedocs/doc_builder/python_environments.py +++ b/readthedocs/doc_builder/python_environments.py @@ -455,6 +455,30 @@ def install_requirements_file(self, install): bin_path=self.venv_bin(), ) + def get_installed_packages(self): + args = [ + self.venv_bin(filename='python'), + '-m', + 'pip', + 'list', + # Inlude pre-release versions. + '--pre', + # Don't include global packages if --system-site-packages was used. + '--local', + # List only top packages, not their dependencies. + '--not-required', + '--format', + 'json', + ] + # TODO: return the output. + self.build_env.run( + *args, + # Don't add warnings to the output. + stderr=False, + cwd=self.checkout_path, + bin_path=self.venv_bin(), + ) + def list_packages_installed(self): """List packages installed in pip.""" args = [ @@ -709,6 +733,44 @@ def install_requirements_file(self, install): # defined by the user, there is nothing to update at this point pass + def get_installed_packages(self): + args = [ + self.conda_bin_name(), + 'list', + '--json', + '--name', + self.version.slug, + ] + self.build_env.run( + *args, + # Don't add warnings to the output. + stderr=False, + cwd=self.checkout_path, + bin_path=self.venv_bin(), + ) + args = [ + self.venv_bin(filename='python'), + '-m', + 'pip', + 'list', + # Inlude pre-release versions. + '--pre', + # Don't include global packages if --system-site-packages was used. + '--local', + # List only top packages, not their dependencies. + '--not-required', + '--format', + 'json', + ] + # TODO: return the output. + self.build_env.run( + *args, + # Don't add warnings to the output. + stderr=False, + cwd=self.checkout_path, + bin_path=self.venv_bin(), + ) + def list_packages_installed(self): """List packages installed in conda.""" args = [