-
Notifications
You must be signed in to change notification settings - Fork 3
/
conda.rb
76 lines (60 loc) · 1.77 KB
/
conda.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true
require "./channel"
require "singleton"
class Conda
include Singleton
attr_reader :channels
def initialize
@channels = {
"Main" => Channel.new("main", "repo.anaconda.com/pkgs"),
"CondaForge" => Channel.new("conda-forge", "conda.anaconda.org"),
}
@lock = Concurrent::ReadWriteLock.new
new_packages = all_packages
@lock.with_write_lock { @packages = new_packages }
end
def logger
@logger ||= Logger.new($stdout)
end
def packages_by_channel(channel)
raise Sinatra::NotFound unless @channels.key?(channel)
@channels[channel].packages
end
def package_by_channel(channel, name, version)
raise Sinatra::NotFound unless @channels.key?(channel)
@channels[channel].package_version(name, version)
end
def packages
@lock.with_read_lock { @packages }
end
def all_packages
global_packages = {}
@channels.each_value do |channel|
channel.packages.each do |package_name, package|
if global_packages.key?(package_name)
global_packages[package_name][:versions] += package[:versions].clone
else
global_packages[package_name] = package.clone
end
end
end
global_packages
end
def package(channel, name)
packs = packages_by_channel(channel)
raise Sinatra::NotFound unless packs.key?(name)
packs[name]
end
def find_package(name)
package = @channels.values.find { |channel| channel.packages.key?(name) }&.packages&.dig(name)
raise Sinatra::NotFound if package.nil?
package
end
def reload_all
logger.info "Reloading packages...."
@channels.each_value(&:reload)
new_packages = all_packages
@lock.with_write_lock { @packages = new_packages }
logger.info "Reload finished...."
end
end