forked from rubygems/rubygems.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cutter.rb
118 lines (93 loc) · 2.83 KB
/
cutter.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
module Gem
class Cutter
attr_accessor :data, :error, :spec, :exists
def initialize(data)
self.data = data
end
def self.indexer
indexer = Indexer.new(Gemcutter.server_path, :build_legacy => false)
def indexer.say(message) end
indexer
end
def self.find_all
all_gems = Marshal.load(File.open(Gemcutter.server_path("latest_specs.4.8")))
unique_gems = {}
all_gems.each do |gem|
key = gem.first
version = gem[1].version
if !unique_gems[key] || (unique_gems[key] && version > unique_gems[key][1].version)
unique_gems[key] = gem
end
end
unique_gems.values.sort { |a, b| a.first <=> b.first }
end
def self.find(gem)
path = Gemcutter.server_path('specifications', gem + "*")
Specification.load Dir[path].last
end
def self.count
self.find_all.size
end
def validate
temp = Tempfile.new("gem")
File.open(temp.path, 'wb') do |f|
f.write self.data.read
end
if File.size(temp.path).zero?
self.error = "Empty gem cannot be processed."
nil
else
temp
end
end
def save(temp)
begin
self.spec = Format.from_file_by_path(temp.path).spec
# Performing some more validation since rubygems won't do it
self.spec.dependencies.each do |dep|
if !dep.is_a?(Gem::Dependency)
self.error = "Invalid gem dependencies"
return false
end
end
ruby_spec = self.spec.to_ruby
rescue Exception => e
self.error = e
return false
end
name = "#{self.spec.name}-#{self.spec.version}.gem"
cache_path = Gemcutter.server_path('cache', name)
spec_path = Gemcutter.server_path('specifications', name + "spec")
self.exists = File.exists?(spec_path)
FileUtils.cp temp.path, cache_path
File.chmod 0644, cache_path
File.open(spec_path, "w") do |f|
f.write ruby_spec
end
true
end
def index
source_path = Gemcutter.server_path("source_index")
if File.exists?(source_path)
source_index = Marshal.load(File.open(source_path))
else
source_index = SourceIndex.new
end
source_index.add_spec self.spec, self.spec.original_name
File.open(source_path, "wb") do |f|
f.write Marshal.dump(source_index)
end
Cutter.indexer.abbreviate self.spec
Cutter.indexer.sanitize self.spec
quick_path = Gemcutter.server_path("quick", "Marshal.#{Gem.marshal_version}", "#{self.spec.original_name}.gemspec.rz")
zipped = Gem.deflate(Marshal.dump(self.spec))
File.open(quick_path, "wb") do |f|
f.write zipped
end
end
def process
temp = validate
save(temp) && index unless temp.nil?
end
end
end