Method: OpenC3::PythonPackageModel.install

Defined in:
lib/openc3/models/python_package_model.rb

.install(name_or_path, scope:) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/openc3/models/python_package_model.rb', line 70

def self.install(name_or_path, scope:)
  if File.exist?(name_or_path)
    package_file_path = name_or_path
  else
    package_file_path = get(name_or_path)
  end
  package_filename = File.basename(package_file_path)
  begin
    pypi_url = get_setting('pypi_url', scope: scope)
    if pypi_url
      pypi_url += '/simple'
    end
  rescue => e
    Logger.error("Failed to retrieve pypi_url: #{e.formatted}")
  ensure
    if pypi_url.nil?
      # If Redis isn't running try the ENV, then simply pypi.org/simple
      pypi_url = ENV['PYPI_URL']
      if pypi_url
        pypi_url += '/simple'
      end
      pypi_url ||= 'https://pypi.org/simple'
    end
  end
  Logger.info "Installing python package: #{name_or_path}"
  if ENV['PIP_ENABLE_TRUSTED_HOST'].nil?
    pip_args = ["--no-warn-script-location", "-i", pypi_url, package_file_path]
  else
    pip_args = ["--no-warn-script-location", "-i", pypi_url, "--trusted-host", URI.parse(pypi_url).host, package_file_path]
  end
  result = OpenC3::ProcessManager.instance.spawn(["/openc3/bin/pipinstall"] + pip_args, "package_install", package_filename, Time.now + 3600.0, scope: scope)
  return result.name
end