From 4c095c4d7f678fd65c21db21e6266425bcff8f19 Mon Sep 17 00:00:00 2001 From: Tom Christensen Date: Thu, 27 Oct 2022 11:31:48 -0600 Subject: [PATCH 1/2] Centralize pyenv install logic --- .../file_updater/pip_compile_file_updater.rb | 13 ++----------- .../file_updater/pipfile_file_updater.rb | 6 +----- .../file_updater/poetry_file_updater.rb | 8 ++------ python/lib/dependabot/python/helpers.rb | 19 +++++++++++++++++++ .../pip_compile_version_resolver.rb | 13 ++----------- .../update_checker/pipenv_version_resolver.rb | 9 +-------- .../update_checker/poetry_version_resolver.rb | 11 ++--------- 7 files changed, 29 insertions(+), 50 deletions(-) create mode 100644 python/lib/dependabot/python/helpers.rb diff --git a/python/lib/dependabot/python/file_updater/pip_compile_file_updater.rb b/python/lib/dependabot/python/file_updater/pip_compile_file_updater.rb index c99a2fc126a..0dcd0767733 100644 --- a/python/lib/dependabot/python/file_updater/pip_compile_file_updater.rb +++ b/python/lib/dependabot/python/file_updater/pip_compile_file_updater.rb @@ -7,6 +7,7 @@ require "dependabot/python/file_parser/python_requirement_parser" require "dependabot/python/file_updater" require "dependabot/shared_helpers" +require "dependabot/python/helpers" require "dependabot/python/native_helpers" require "dependabot/python/python_versions" require "dependabot/python/name_normaliser" @@ -65,7 +66,7 @@ def fetch_updated_dependency_files def compile_new_requirement_files SharedHelpers.in_a_temporary_directory do write_updated_dependency_files - install_required_python + Helpers.install_required_python(python_version) filenames_to_compile.each do |filename| # Shell out to pip-compile, generate a new set of requirements. @@ -212,16 +213,6 @@ def write_updated_dependency_files end end - def install_required_python - # The leading space is important - return if run_command("pyenv versions").include?(" #{python_version}") - - run_command("pyenv install -s #{python_version}") - run_command("pyenv exec pip install --upgrade pip") - run_command("pyenv exec pip install -r " \ - "#{NativeHelpers.python_requirements_path}") - end - def sanitized_setup_file_content(file) @sanitized_setup_file_content ||= {} return @sanitized_setup_file_content[file.name] if @sanitized_setup_file_content[file.name] diff --git a/python/lib/dependabot/python/file_updater/pipfile_file_updater.rb b/python/lib/dependabot/python/file_updater/pipfile_file_updater.rb index 81fd384a271..828e245a615 100644 --- a/python/lib/dependabot/python/file_updater/pipfile_file_updater.rb +++ b/python/lib/dependabot/python/file_updater/pipfile_file_updater.rb @@ -302,11 +302,7 @@ def install_required_python nil end - return if run_command("pyenv versions").include?("#{python_version}\n") - - requirements_path = NativeHelpers.python_requirements_path - run_command("pyenv install -s #{python_version}") - run_command("pyenv exec pip install -r #{requirements_path}") + Helpers.install_required_python(python_version) end def sanitized_setup_file_content(file) diff --git a/python/lib/dependabot/python/file_updater/poetry_file_updater.rb b/python/lib/dependabot/python/file_updater/poetry_file_updater.rb index 1fba530ea2f..de8f0d35128 100644 --- a/python/lib/dependabot/python/file_updater/poetry_file_updater.rb +++ b/python/lib/dependabot/python/file_updater/poetry_file_updater.rb @@ -4,6 +4,7 @@ require "open3" require "dependabot/dependency" require "dependabot/shared_helpers" +require "dependabot/python/helpers" require "dependabot/python/version" require "dependabot/python/requirement" require "dependabot/python/python_versions" @@ -170,12 +171,7 @@ def updated_lockfile_content_for(pyproject_content) write_temporary_dependency_files(pyproject_content) add_auth_env_vars - if python_version && !pre_installed_python?(python_version) - run_poetry_command("pyenv install -s #{python_version}") - run_poetry_command("pyenv exec pip install --upgrade pip") - run_poetry_command("pyenv exec pip install -r" \ - "#{NativeHelpers.python_requirements_path}") - end + Helpers.install_required_python(python_version) # use system git instead of the pure Python dulwich unless python_version&.start_with?("3.6") diff --git a/python/lib/dependabot/python/helpers.rb b/python/lib/dependabot/python/helpers.rb new file mode 100644 index 00000000000..8e30125e228 --- /dev/null +++ b/python/lib/dependabot/python/helpers.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true +require "dependabot/logger" + +module Dependabot + module Python + module Helpers + def self.install_required_python(python_version) + # The leading space is important in the version check + return if SharedHelpers.run_shell_command("pyenv versions").include?(" #{python_version}") + + Dependabot.logger.info("Installing required Python #{python_version}.") + SharedHelpers.run_shell_command("pyenv install -s #{python_version}") + SharedHelpers.run_shell_command("pyenv exec pip install --upgrade pip") + SharedHelpers.run_shell_command("pyenv exec pip install -r" \ + "#{NativeHelpers.python_requirements_path}") + end + end + end +end diff --git a/python/lib/dependabot/python/update_checker/pip_compile_version_resolver.rb b/python/lib/dependabot/python/update_checker/pip_compile_version_resolver.rb index e6deffdbf71..f3e5983a135 100644 --- a/python/lib/dependabot/python/update_checker/pip_compile_version_resolver.rb +++ b/python/lib/dependabot/python/update_checker/pip_compile_version_resolver.rb @@ -11,6 +11,7 @@ require "dependabot/python/file_updater/setup_file_sanitizer" require "dependabot/python/version" require "dependabot/shared_helpers" +require "dependabot/python/helpers" require "dependabot/python/native_helpers" require "dependabot/python/python_versions" require "dependabot/python/name_normaliser" @@ -70,7 +71,7 @@ def fetch_latest_resolvable_version_string(requirement:) SharedHelpers.in_a_temporary_directory do SharedHelpers.with_git_configured(credentials: credentials) do write_temporary_dependency_files(updated_req: requirement) - install_required_python + Helpers.install_required_python(python_version) filenames_to_compile.each do |filename| # Shell out to pip-compile. @@ -319,16 +320,6 @@ def write_original_manifest_files end end - def install_required_python - # The leading space is important - return if run_command("pyenv versions").include?(" #{python_version}") - - run_command("pyenv install -s #{python_version}") - run_command("pyenv exec pip install --upgrade pip") - run_command("pyenv exec pip install -r" \ - "#{NativeHelpers.python_requirements_path}") - end - def sanitized_setup_file_content(file) @sanitized_setup_file_content ||= {} return @sanitized_setup_file_content[file.name] if @sanitized_setup_file_content[file.name] diff --git a/python/lib/dependabot/python/update_checker/pipenv_version_resolver.rb b/python/lib/dependabot/python/update_checker/pipenv_version_resolver.rb index 4f4be792b22..c5bb2f9848a 100644 --- a/python/lib/dependabot/python/update_checker/pipenv_version_resolver.rb +++ b/python/lib/dependabot/python/update_checker/pipenv_version_resolver.rb @@ -320,14 +320,7 @@ def install_required_python nil end - # The leading space is important - return if run_command("pyenv versions").include?(" #{python_version}") - - requirements_path = NativeHelpers.python_requirements_path - run_command("pyenv install -s #{python_version}") - run_command("pyenv exec pip install --upgrade pip") - run_command("pyenv exec pip install -r " \ - "#{requirements_path}") + Helpers.install_required_python(python_version) end def sanitized_setup_file_content(file) diff --git a/python/lib/dependabot/python/update_checker/poetry_version_resolver.rb b/python/lib/dependabot/python/update_checker/poetry_version_resolver.rb index 6be187f5374..125c73d8a8b 100644 --- a/python/lib/dependabot/python/update_checker/poetry_version_resolver.rb +++ b/python/lib/dependabot/python/update_checker/poetry_version_resolver.rb @@ -93,14 +93,7 @@ def fetch_latest_resolvable_version_string(requirement:) write_temporary_dependency_files(updated_req: requirement) add_auth_env_vars - if python_version && !pre_installed_python?(python_version) - run_poetry_command("pyenv install -s #{python_version}") - run_poetry_command("pyenv exec pip install --upgrade pip") - run_poetry_command( - "pyenv exec pip install -r " \ - "#{NativeHelpers.python_requirements_path}" - ) - end + Helpers.install_required_python(python_version) # use system git instead of the pure Python dulwich unless python_version&.start_with?("3.6") @@ -346,7 +339,7 @@ def run_poetry_command(command) stdout, process = Open3.capture2e(command) time_taken = Time.now - start - # Raise an error with the output from the shell session if Pipenv + # Raise an error with the output from the shell session if poetry # returns a non-zero status return if process.success? From f248bc86c5e5ed7c5cafb89903d087f8cf3f154e Mon Sep 17 00:00:00 2001 From: Tom Christensen Date: Thu, 27 Oct 2022 11:53:32 -0600 Subject: [PATCH 2/2] Rubocop fixes --- python/lib/dependabot/python/helpers.rb | 1 + .../dependabot/python/update_checker/poetry_version_resolver.rb | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/python/lib/dependabot/python/helpers.rb b/python/lib/dependabot/python/helpers.rb index 8e30125e228..baa49f30f1d 100644 --- a/python/lib/dependabot/python/helpers.rb +++ b/python/lib/dependabot/python/helpers.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true + require "dependabot/logger" module Dependabot diff --git a/python/lib/dependabot/python/update_checker/poetry_version_resolver.rb b/python/lib/dependabot/python/update_checker/poetry_version_resolver.rb index 125c73d8a8b..46084fff17b 100644 --- a/python/lib/dependabot/python/update_checker/poetry_version_resolver.rb +++ b/python/lib/dependabot/python/update_checker/poetry_version_resolver.rb @@ -82,7 +82,6 @@ def resolvable?(version:) private - # rubocop:disable Metrics/PerceivedComplexity def fetch_latest_resolvable_version_string(requirement:) @latest_resolvable_version_string ||= {} return @latest_resolvable_version_string[requirement] if @latest_resolvable_version_string.key?(requirement) @@ -116,7 +115,6 @@ def fetch_latest_resolvable_version_string(requirement:) end end end - # rubocop:enable Metrics/PerceivedComplexity def fetch_version_from_parsed_lockfile(updated_lockfile) version =