Class: OpenC3::PythonPackageModel

Inherits:
Object
  • Object
show all
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.

Class Method Summary collapse

Class Method Details

.destroy(name, scope:) ⇒ Object



84
85
86
87
88
89
# File 'lib/openc3/models/python_package_model.rb', line 84

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



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/openc3/models/python_package_model.rb', line 91

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
# 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)
  rescue
    # If Redis isn't running try the ENV, then simply pypi.org/simple
    pypi_url = ENV['PYPI_URL']
    pypi_url ||= 'https://pypi.org/simple'
  end
  Logger.info "Installing python package: #{name_or_path}"
  result = OpenC3::ProcessManager.instance.spawn(["pip", "install", "--user", "-i", pypi_url, package_file_path], "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