Class: OpenC3::PythonPackageModel

Inherits:
Object
  • Object
show all
Extended by:
Api
Defined in:
lib/openc3/models/python_package_model.rb

Overview

This class acts like a Model but doesn’t inherit from Model because it doesn’t actual interact with the Store (Redis). Instead we implement names, get, put and destroy to allow interaction with python package files from the PluginModel and the PackagesController.

Constant Summary collapse

DIST_INFO =
'.dist-info'

Constants included from Api

Api::DELAY_METRICS, Api::DURATION_METRICS, Api::SUBSCRIPTION_DELIMITER, Api::SUM_METRICS

Constants included from ApiShared

ApiShared::DEFAULT_TLM_POLLING_RATE

Constants included from Extract

Extract::SCANNING_REGULAR_EXPRESSION

Class Method Summary collapse

Methods included from Api

_cmd_implementation, _extract_target_command_names, _extract_target_command_parameter_names, _extract_target_packet_item_names, _extract_target_packet_names, _get_and_set_cmd, _get_item, _limits_group, _set_tlm_process_args, _tlm_process_args, _validate_tlm_type, build_cmd, cmd, cmd_no_checks, cmd_no_hazardous_check, cmd_no_range_check, cmd_raw, cmd_raw_no_checks, cmd_raw_no_hazardous_check, cmd_raw_no_range_check, config_tool_names, connect_interface, connect_router, delete_config, disable_cmd, disable_limits, disable_limits_group, disconnect_interface, disconnect_router, enable_cmd, enable_limits, enable_limits_group, get_all_cmd_names, get_all_cmds, get_all_interface_info, get_all_router_info, get_all_settings, get_all_tlm, get_all_tlm_item_names, get_all_tlm_names, get_cmd, get_cmd_buffer, get_cmd_cnt, get_cmd_cnts, get_cmd_hazardous, get_cmd_time, get_cmd_value, get_interface, get_interface_names, get_item, get_limits, get_limits_events, get_limits_groups, get_limits_set, get_limits_sets, get_metrics, get_out_of_limits, get_overall_limits_state, get_overrides, get_packet_derived_items, get_packets, get_param, get_router, get_router_names, get_setting, get_settings, get_target, get_target_interfaces, get_target_names, get_tlm, get_tlm_available, get_tlm_buffer, get_tlm_cnt, get_tlm_cnts, get_tlm_packet, get_tlm_values, inject_tlm, interface_cmd, interface_details, interface_protocol_cmd, interface_target_disable, interface_target_enable, limits_enabled?, list_configs, list_settings, load_config, map_target_to_interface, map_target_to_router, normalize_tlm, offline_access_needed, override_tlm, router_cmd, router_details, router_protocol_cmd, router_target_disable, router_target_enable, save_config, send_raw, set_limits, set_limits_set, set_offline_access, set_setting, set_tlm, start_raw_logging_interface, start_raw_logging_router, stash_all, stash_delete, stash_get, stash_keys, stash_set, stop_raw_logging_interface, stop_raw_logging_router, subscribe_packets, tlm, tlm_formatted, tlm_raw, tlm_with_units, unmap_target_from_interface, unmap_target_from_router, update_news, update_plugin_store

Methods included from CmdLog

#_build_cmd_output_string

Class Method Details

.destroy(name, scope:) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/openc3/models/python_package_model.rb', line 99

def self.destroy(name, scope:)
  package_name, version = self.extract_name_and_version(name)
  Logger.info "Uninstalling package: #{name}"
  pip_args = ["-y", package_name]
  result = OpenC3::ProcessManager.instance.spawn(["/openc3/bin/pipuninstall"] + pip_args, "package_uninstall", name, Time.now + 3600.0, scope: scope)
  return result.name
end

.extract_name_and_version(name) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/openc3/models/python_package_model.rb', line 107

def self.extract_name_and_version(name)
  split_name = name.split('-')
  if split_name.length > 1
    package_name = split_name[0..-2].join('-')
    version = File.basename(split_name[-1], DIST_INFO)
  else
    package_name = name
    version = "Unknown"
  end

  return package_name, version
end

.get(name) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/openc3/models/python_package_model.rb', line 38

def self.get(name)
  path = "#{ENV['PYTHONUSERBASE']}/cache"
  FileUtils.mkdir_p(path) unless Dir.exist?(path)
  result = Pathname.new(path).children.select { |c| c.file? and File.basename(c, File.extname(c)) == name }
  if result.length > 0
    return result[0] if File.exist?(result[0])
  end
  raise "Package #{name} not found"
end

.install(name_or_path, scope:) ⇒ Object



65
66
67
68
69
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
# File 'lib/openc3/models/python_package_model.rb', line 65

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 = ["-i", pypi_url, package_file_path]
  else
    pip_args = ["-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

.namesObject



29
30
31
32
33
34
35
36
# File 'lib/openc3/models/python_package_model.rb', line 29

def self.names
  paths = Dir.glob("#{ENV['PYTHONUSERBASE']}/lib/*")
  results = []
  paths.each do |path|
    results.concat(Pathname.new(File.join(path, 'site-packages')).children.select { |c| c.directory? and File.extname(c) == DIST_INFO }.collect { |p| File.basename(p, DIST_INFO) })
  end
  return results.sort
end

.put(package_file_path, package_install: true, scope:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/openc3/models/python_package_model.rb', line 48

def self.put(package_file_path, package_install: true, scope:)
  if File.file?(package_file_path)
    package_filename = File.basename(package_file_path)
    FileUtils.mkdir_p("#{ENV['PYTHONUSERBASE']}/cache") unless Dir.exist?("#{ENV['PYTHONUSERBASE']}/cache")
    cache_path = "#{ENV['PYTHONUSERBASE']}/cache/#{File.basename(package_file_path)}"
    FileUtils.cp(package_file_path, cache_path)
    if package_install
      return self.install(cache_path, scope: scope)
    end
  else
    message = "Package file #{package_file_path} does not exist!"
    Logger.error message
    raise message
  end
  return nil
end