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

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

_build_cmd_output_string, _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_target_info, get_all_tlm, 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_buffer, get_tlm_cnt, get_tlm_cnts, get_tlm_packet, get_tlm_values, inject_tlm, interface_cmd, interface_protocol_cmd, limits_enabled?, list_configs, list_settings, load_config, map_target_to_interface, normalize_tlm, offline_access_needed, override_tlm, router_cmd, router_protocol_cmd, 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_variable, tlm_with_units

Class Method Details

.destroy(name, scope:) ⇒ Object



97
98
99
100
101
102
# File 'lib/openc3/models/python_package_model.rb', line 97

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

.extract_name_and_version(name) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/openc3/models/python_package_model.rb', line 104

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



41
42
43
44
45
46
47
48
49
# File 'lib/openc3/models/python_package_model.rb', line 41

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



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

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}"
  result = OpenC3::ProcessManager.instance.spawn(["/openc3/bin/pipinstall", "--user", "--no-warn-script-location", "-i", pypi_url, package_file_path], "package_install", package_filename, Time.now + 3600.0, scope: scope)
  return result.name
end

.namesObject



32
33
34
35
36
37
38
39
# File 'lib/openc3/models/python_package_model.rb', line 32

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



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

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