-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage.rb
More file actions
executable file
·276 lines (240 loc) · 6.01 KB
/
package.rb
File metadata and controls
executable file
·276 lines (240 loc) · 6.01 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/ruby
require 'logging'
require 'commands'
require 'rubygems'
require 'fileutils'
require 'httpclient'
require 'erb'
include Logging
$packages = {}
def lookup(name)
p = $packages[name]
if not p
$stderr.puts "could not find package named #{name}"
exit 1
else
p
end
end
def sh(text)
log text
raise "shell error with #{text}" unless system(text)
end
def sh_f(text)
system("#{text} > /dev/null")
end
def all(coll, pred)
coll.each {|c| return false unless pred.call(c) }
return true
end
def some(coll, pred)
coll.each {|c| return true if pred.call(c) }
return false
end
class Package
attr_accessor :name
attr_accessor :package_commands, :package_dependencies
attr_accessor :package_directories, :package_repository
attr_accessor :package_downloads, :project_directory
attr_accessor :package_description
def initialize(name, settings)
@name = name
@flags = []
@c_flags = []
@c_descriptions = []
@project_directory = "#{settings['global']['directory']}/#{name}"
@support = "support/#{name}"
@settings = settings
@package_commands = {}
@package_dependencies = []
@package_directories = []
@package_repository = []
@package_downloads = []
@package_installs_service = false
@install_script = "#@support/#{name}"
@package_description = "None"
create_default_methods
end
def create_default_methods
command :install
command :remove
command :reinstall
command :installed?
end
def to_s
@name.to_s
end
def inspect
@name.to_s
end
def invoke_if_exists(sym, *args)
if @package_commands[sym]
@package_commands[sym].call(*args)
else
raise "could not find command #{sym}"
end
end
def get_binding
binding
end
def installs_service
@package_installs_service = true
end
def install_service
return unless @package_installs_service
script_name = (@install_script.split /\//).last
cp @install_script, "/etc/init.d/"
sh("chmod a+xr /etc/init.d/#{script_name}")
sh("update-rc.d #{script_name} defaults")
sh("service #{script_name} start")
end
def install_dependencies
@package_dependencies.each do |d|
lookup(d).install
end
end
def create_directories
return if (@flags.include? :apt_package or
@flags.include? :gem_package)
mkdir_p @project_directory
@package_directories.each {|d| mkdir_p d}
end
def download_repositories
client = HTTPClient.new
case @package_repository[0]
when :git
sh("git clone #{package_repository[1]} #@project_directory")
when :svn
sh("svn checkout #{package_repository[1]} #@project_directory")
end
end
def download_other_stuff
client = HTTPClient.new
@package_downloads.each do |pd|
url = pd[:url]
file = pd[:extract]
dest = pd[:to]
log "downloading package #{file}"
open("#@project_directory/#{file}", "w") do |f|
f.write(client.get_content(url))
end
sh("cd #@project_directory && tar xf #{file}")
sh("mv #@project_directory/#{file.chomp('.tar.gz')}/* #{dest}")
end
end
def remove_directories
log "removing directories for #@name"
log "rm -rf #@project_directory"
rm_rf @project_directory
@package_directories.each do |d|
log "rm -rf #{d}"
rm_rf d
end
end
def process_support_files
log "processing directory #@support/*"
Dir.glob("#@support/*").each do |file|
log "processing #{file}"
if File.file? file and /(\.*)(.erb$)/ =~ file
fname = file.scan(/(.*)(.erb$)/)[0][0]
File.open(fname,"w") do |f|
f.write(ERB.new(File.read(file)).result(get_binding))
end
end
end
end
def depends_on(*args)
args.each {|a| @package_dependencies << a}
end
def directories(*args)
args.each {|a| @package_directories << a}
end
def repository(*args)
@package_repository = args
end
def description(desc)
@package_description = desc
end
def command(name, &block)
raise "name cannot be null : #{name}" unless name
c = nil
case name
when :install
c = default_install_command(self, &block)
when :remove
c = default_remove_command(self, &block)
when :installed?
c = default_installed_predicate(self, &block)
when :reinstall
c = default_reinstall_command(self, &block)
else
c = Command.new(self, block)
end
c.name = name
@package_commands[name] = c
(class << self; self; end).class_eval do
define_method name do |*args|
@package_commands[name].call(*args)
end
end
end
def downloads(options)
@package_downloads << options
end
end
def package(name, &block)
package = Package.new(name, SETTINGS)
package.instance_eval(&block)
$packages[name] = package
end
$installed_deb_packages = `dpkg --list`
def apt_package(*args)
name = args.shift
apt_name = name
if not args.empty?
apt_name = args.shift
end
package name do
@flags << :apt_package
command :install do
sh("aptitude -y install #{apt_name}")
end
command :remove do
sh("aptitude -y remove #{apt_name}")
end
command :installed? do
log "checking if #{name} is installed"
installed = $installed_deb_packages.reject {|r| not r =~ /^ii/ or not r =~ / #{apt_name} /}
not installed.empty?
end
end
end
def gem_package(*args)
name = args.shift
if not args.empty?
gem_name = args.shift
end
package name do
@flags << :gem_package
command :install do
sh("gem install #{gem_name}")
end
command :remove do
sh("gem remove #{gem_name}")
end
command :installed? do
false
end
end
end
def py_package(name, options)
package name do
@flags << :py_package
depends_on :python
depends_on(*options[:dependencies])
downloads :url => options[:url], :extract => options[:tarball], :to => @project_directory
command :install do
sh("cd #@project_directory && python setup.py install")
end
end
end