-
Notifications
You must be signed in to change notification settings - Fork 2
/
feed_updater
317 lines (294 loc) · 10.1 KB
/
feed_updater
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env ruby
#--
# Copyright (c) 2005 Robert Aman
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
if RUBY_PLATFORM =~ /mswin/
puts "FeedUpdater is not supported on Windows due to a lack of fork()."
exit
end
# :stopdoc:
# ROFLCOPTERS?
# This mainly exists because of the approximately 1 billion different
# ways to deploy this script.
class FileStalker
def self.hunt(paths)
for path in paths
if File.exists?(File.expand_path(path))
return File.expand_path(path)
elsif File.exists?(File.expand_path(
File.dirname(__FILE__) + path))
return File.expand_path(File.dirname(__FILE__) + path)
elsif File.exists?(File.expand_path(
File.dirname(__FILE__) + "/" + path))
return File.expand_path(File.dirname(__FILE__) + "/" + path)
end
end
return nil
end
end
# :startdoc:
$:.unshift(FileStalker.hunt([
"/../vendor/feedupdater/lib",
"vendor/feedupdater/lib",
"../lib"]))
$:.uniq!
require 'yaml'
require 'feed_updater'
if ARGV[0].nil?
# Exit quickly if no command line arguments given.
feed_updater_version = FeedTools::FEED_UPDATER_VERSION::STRING
feed_tools_version = "<error: not loaded>"
if defined?(FeedTools::FEED_TOOLS_VERSION::STRING)
feed_tools_version = FeedTools::FEED_TOOLS_VERSION::STRING
elsif defined?(FEED_TOOLS_VERSION)
feed_tools_version = FEED_TOOLS_VERSION
end
puts ""
puts "FeedUpdater #{feed_updater_version} / FeedTools #{feed_tools_version}"
puts "USAGE: feed_updater <subcommand>"
puts ""
puts "Subcommands:"
puts " start"
puts " This will start the daemon if it hasn't already been started."
puts ""
puts " stop"
puts " This will stop the daemon if it's already running."
puts ""
puts " restart"
puts " This will restart the daemon."
puts ""
puts " install"
puts " If the current directory is a Rails installation, this will"
puts " install FeedUpdater into the vendor folder and add a new script"
puts " to the scripts/ directory for running FeedUpdater. Rerunning"
puts " the install subcommand will update the files against the latest"
puts " version of the FeedUpdater gem file you have installed. Rails"
puts " is NOT required to use FeedUpdater."
puts ""
exit
elsif ARGV[0] == "install"
rails_root = nil
install_vendor_directory = nil
install_config_directory = nil
install_script_directory = nil
current_directory = File.expand_path(".")
loop do
entries = Dir.entries(current_directory)
if entries.include?("vendor") &&
entries.include?("config") &&
entries.include?("script")
rails_root = current_directory
break
else
break if current_directory == "/"
current_directory = File.expand_path(current_directory + "/..")
end
end
if rails_root.nil?
puts "You must run the install command when the current directory is"
puts "a Rails application."
else
install_vendor_directory = File.expand_path(rails_root + "/vendor")
install_config_directory = File.expand_path(rails_root + "/config")
install_script_directory = File.expand_path(rails_root + "/script")
# Install FeedUpdater gem to application vendor directory
system("rm -rf #{install_vendor_directory}/feedupdater")
unpack_string = `gem unpack feedupdater`
feed_updater_version =
unpack_string.scan(/feedupdater\-(.*)\'/).flatten[0]
if feed_updater_version.nil? || feed_updater_version == ""
puts "You need to have the feedupdater gem installed for the install"
puts "subcommand to run correctly."
exit
end
puts "Installing FeedUpdater #{feed_updater_version} " +
"to vendor directory..."
system("cp -rp feedupdater-#{feed_updater_version} " +
"#{install_vendor_directory}")
system("rm -rf feedupdater-#{feed_updater_version}")
system("mv #{install_vendor_directory}/" +
"feedupdater-#{feed_updater_version} " +
"#{install_vendor_directory}/feedupdater")
# Install FeedTools gem to application vendor directory
system("rm -rf #{install_vendor_directory}/feedtools")
unpack_string = `gem unpack feedtools`
feed_tools_version =
unpack_string.scan(/feedtools\-(.*)\'/).flatten[0]
if feed_tools_version.nil? || feed_tools_version == ""
puts "You need to have the feedtools gem installed for the install"
puts "subcommand to run correctly."
exit
end
puts "Installing FeedTools #{feed_tools_version} to vendor directory..."
system("cp -rp feedtools-#{feed_tools_version} " +
"#{install_vendor_directory}")
system("rm -rf feedtools-#{feed_tools_version}")
system("mv #{install_vendor_directory}/" +
"feedtools-#{feed_tools_version} " +
"#{install_vendor_directory}/feedtools")
# Copying the default config file to the config directory
if !File.exists?(File.expand_path(install_config_directory +
"/feed_updater.yml"))
puts "Installing default config file..."
config_file =
File.expand_path(File.dirname(__FILE__) + '/../config/feed_updater.yml')
system("cp -p #{config_file} #{install_config_directory}")
else
puts "Config file already exists, skipping..."
end
# Copying this executable to the script directory
puts "Copying executable to script directory..."
system("rm -rf #{install_script_directory}/feed_updater")
system("cp -p #{__FILE__} #{install_script_directory}")
system("chmod +x #{install_script_directory}/feed_updater")
puts "Use script/feed_updater to run FeedUpdater."
end
exit
end
config_file = FileStalker.hunt([
"../config/feed_updater.yml",
"./feed_updater.yml"
])
# Reportedly, in some cases, the example config is found instead.
if config_file =~ /example/
config_file = FileStalker.hunt([
"../../../config/feed_updater.yml",
"../config/feed_updater.yml",
"./feed_updater.yml"
])
end
if config_file.nil?
puts "Could not locate feed_updater.yml config file."
exit
end
config_hash = YAML.load(File.open(config_file, "r").read)
load_script_path = config_hash["load_script"]
if !load_script_path.nil?
result = FileStalker.hunt([
load_script_path,
File.dirname(config_file) + "/" + load_script_path,
File.dirname(config_file) + load_script_path,
File.dirname(__FILE__) + "/../" + load_script_path,
File.dirname(__FILE__) + "/.." + load_script_path
])
if result.nil?
puts "Could not locate #{load_script_path.inspect} file."
exit
else
load_script_path = result
end
end
pid_file_path = config_hash["pid_file_path"]
if !pid_file_path.nil?
result = FileStalker.hunt([
pid_file_path,
File.dirname(config_file) + "/" + pid_file_path,
File.dirname(config_file) + pid_file_path,
File.dirname(__FILE__) + "/../" + pid_file_path,
File.dirname(__FILE__) + "/.." + pid_file_path,
"../" + pid_file_path
])
pid_file_path = result if !result.nil?
else
result = FileStalker.hunt([
File.dirname(__FILE__) + "/../config",
"./config",
File.dirname(__FILE__) + "/config"
])
pid_file_path = result if !result.nil?
end
log_file_path = config_hash["log_file_path"]
if !log_file_path.nil?
result = FileStalker.hunt([
log_file_path,
File.dirname(config_file) + "/" + log_file_path,
File.dirname(config_file) + log_file_path,
File.dirname(__FILE__) + "/../" + log_file_path,
File.dirname(__FILE__) + "/.." + log_file_path,
"../" + log_file_path
])
log_file_path = result if !result.nil?
else
result = FileStalker.hunt([
File.dirname(__FILE__) + "/../log",
"./log",
File.dirname(__FILE__) + "/log",
File.dirname(__FILE__) + "/../logs",
"./logs",
File.dirname(__FILE__) + "/logs"
])
log_file_path = result if !result.nil?
end
script_class = nil
existing_subclasses = []
ObjectSpace.each_object do |instance|
if instance.class.name == "Class" &&
instance.ancestors.include?(FeedTools::FeedUpdater)
existing_subclasses << instance
end
end
load(load_script_path)
loaded_subclasses = []
ObjectSpace.each_object do |instance|
if instance.class.name == "Class" &&
instance.ancestors.include?(FeedTools::FeedUpdater)
loaded_subclasses << instance
end
end
script_classes = (loaded_subclasses - existing_subclasses)
if script_classes.size == 0
puts "There do not appear to be any subclasses of FeedTools::FeedUpdater."
exit
elsif script_classes.size > 1
puts "There appear to be multiple subclasses of FeedTools::FeedUpdater."
exit
else
script_class = script_classes[0]
end
updater = nil
if load_script_path.nil?
updater = FeedTools::FeedUpdater.new
FeedTools::FeedUpdater.on_update do |feed, seconds|
updater.logger.info("Loaded '#{feed.href}'.")
updater.logger.info("=> Updated (#{feed.title}) in #{seconds} seconds.")
end
FeedTools::FeedUpdater.on_error do |href, error|
updater.logger.info("Error updating '#{href}':")
updater.logger.info(error)
end
else
updater = script_class.new
end
for key in config_hash.keys
key_sym = key.to_s.to_sym
updater.updater_options[key_sym] = config_hash[key]
end
updater.pid_file_dir = pid_file_path
updater.log_file_dir = log_file_path
case ARGV[0]
when "start"
updater.start()
when "stop"
updater.stop()
when "restart"
updater.restart()
end