Class: NzbGetPP::PostProcessor
- Inherits:
-
Object
- Object
- NzbGetPP::PostProcessor
show all
- Includes:
- Shellout
- Defined in:
- lib/nzbgetpp.rb
Defined Under Namespace
Classes: ConfigError, Error, InflationError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Shellout
#shellout
Constructor Details
#initialize(path, nzb, par, category) ⇒ PostProcessor
Returns a new instance of PostProcessor.
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/nzbgetpp.rb', line 29
def initialize(path,
nzb,
par,
category)
self.path = path
self.nzb = nzb
self.par = par
self.category = category
clear_callbacks()
load_config()
self.log = mk_log(config.log_level, config.log_fn)
end
|
Instance Attribute Details
#category ⇒ Object
Returns the value of attribute category.
22
23
24
|
# File 'lib/nzbgetpp.rb', line 22
def category
@category
end
|
#config ⇒ Object
Returns the value of attribute config.
23
24
25
|
# File 'lib/nzbgetpp.rb', line 23
def config
@config
end
|
#log ⇒ Object
Returns the value of attribute log.
24
25
26
|
# File 'lib/nzbgetpp.rb', line 24
def log
@log
end
|
#nzb ⇒ Object
Returns the value of attribute nzb.
25
26
27
|
# File 'lib/nzbgetpp.rb', line 25
def nzb
@nzb
end
|
#par ⇒ Object
Returns the value of attribute par.
26
27
28
|
# File 'lib/nzbgetpp.rb', line 26
def par
@par
end
|
#path ⇒ Object
Returns the value of attribute path.
27
28
29
|
# File 'lib/nzbgetpp.rb', line 27
def path
@path
end
|
Instance Method Details
#clear_callbacks ⇒ Object
60
61
62
|
# File 'lib/nzbgetpp.rb', line 60
def clear_callbacks
@callbacks = Array.new()
end
|
52
53
54
|
# File 'lib/nzbgetpp.rb', line 52
def configure
yield(@config)
end
|
233
234
235
236
237
238
239
240
241
242
243
244
|
# File 'lib/nzbgetpp.rb', line 233
def figure_config_fn
fn = "config.rb"
custom = File.join(ENV["HOME"], ".nzbgetpp", fn)
return custom if File.exist? custom
default = File.expand_path(File.join(File.dirname(__FILE__), "../support", fn))
return default if File.exist? default
raise RuntimeError,
"Unable to find config in #{[custom, default].inspect}"
end
|
#inflate_rar_files ⇒ Object
We shellout to unrar as a demonstration of our lack of faith for Ruby-based unraring to handle large files efficiently, fast or even at all.
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
|
# File 'lib/nzbgetpp.rb', line 114
def inflate_rar_files
log.info("Inflating rar files")
unrar_cmd = mk_unrar_cmd
log.debug(unrar_cmd)
FileUtils.mkdir_p(scratch_directory)
child_pid, stdout, stderr = shellout(unrar_cmd)
until Process.wait(child_pid, Process::WNOHANG)
readable, _ = IO.select([stdout, stderr], [], [], 1)
next if readable.nil?
readable.each do |io|
next if io.closed? or io.eof?
line = io.readline
case io
when stdout
log.detail(line)
else
log.error(line)
end
end
end
if $?.success?
log.info("Inflation succeeded")
else
raise(InflationError, child_status.inspect)
end
end
|
#install_callback(name, &block) ⇒ Object
56
57
58
|
# File 'lib/nzbgetpp.rb', line 56
def install_callback(name, &block)
@callbacks.push([name, block])
end
|
#load_config ⇒ Object
44
45
46
47
48
49
50
|
# File 'lib/nzbgetpp.rb', line 44
def load_config()
@config = OpenStruct.new()
config_fn = figure_config_fn()
ruby = File.read(config_fn)
Kernel.eval(ruby, binding(), config_fn, 0)
@config
end
|
#merge_directory_tree(source, destination) ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/nzbgetpp.rb', line 146
def merge_directory_tree(source, destination)
Dir.glob(File.join(source, "**/*")).each do |f|
next if File.directory?(f)
rel_dir = File.dirname(f).gsub(/^#{Regexp.escape(source.to_s)}\/?/, "")
target_dir = File.join(destination,
rel_dir)
unless File.exist?(target_dir)
log.debug("Making %s" % target_dir)
FileUtils.mkdir_p(target_dir)
end
log.debug("Will move #{f} to #{target_dir}")
FileUtils.mv(f, target_dir)
end
end
|
#mk_log(log_level, log_fn) ⇒ Object
199
200
201
|
# File 'lib/nzbgetpp.rb', line 199
def mk_log(log_level, log_fn)
Log.new(log_level, log_fn)
end
|
#mk_unrar_cmd ⇒ Object
203
204
205
206
207
208
209
210
|
# File 'lib/nzbgetpp.rb', line 203
def mk_unrar_cmd
[
config.unrar_bin,
config.unrar_flags.join(" "),
"\"#{unrar_targets.to_s}\"",
"\"#{scratch_directory.to_s}\"",
].join(" ")
end
|
#postprocess! ⇒ Object
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
|
# File 'lib/nzbgetpp.rb', line 74
def postprocess!
unless nzb.completed?
log.info("Called prematurely - this nzb ain't ready for me")
return true
end
unless Dir[path.join("*.rar").to_s].empty?
inflate_rar_files
else
log.info("Nothing to inflate")
end
remove_unneeded_files(path)
merge_directory_tree(path, scratch_directory)
remove_download_directory(path)
remove_unneeded_files(scratch_directory)
log.info("Moving to final resting place")
storage_dir = storage_directory.dirname
unless storage_dir.exist?
log.debug("Storage dir #{storage_dir} will be created")
FileUtils.mkdir_p(storage_dir.to_s)
end
FileUtils.mv(scratch_directory, storage_directory)
run_callbacks()
log.info("Postprocessing complete")
log.close_log_fd()
return true
rescue PostProcessor::Error => e
log.error("#{e.class.name}: #{e.message}")
e.backtrace.each do |line|
log.detail(line)
end
raise(e)
end
|
#remove_download_directory(where) ⇒ Object
167
168
169
|
# File 'lib/nzbgetpp.rb', line 167
def remove_download_directory(where)
FileUtils.rm_r(where)
end
|
#remove_unneeded_files(where) ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/nzbgetpp.rb', line 171
def remove_unneeded_files(where)
log.info("Cleaning up unnecessary files in %s" %
where.to_s)
config.discard_files.each do |glob|
log.debug("Will remove %s #=> %s" %
[
where.join(glob),
Dir.glob(where.join(glob)).inspect()
])
Dir.glob(where.join(glob)).each do |file|
FileUtils.rm_f(file)
end
end
Dir.glob(where.join("*")) do |glob|
remove_unneeded_files(Pathname.new(glob)) if File.directory? glob
end
end
|
#run_callbacks ⇒ Object
64
65
66
67
68
69
70
71
72
|
# File 'lib/nzbgetpp.rb', line 64
def run_callbacks
log.info("Running callbacks")
log.detail("Callbacks #=> #{@callbacks.inspect()}")
@callbacks.each do |name, block|
log.debug("Running callback #{name}")
block.call()
end
end
|
#scratch_directory ⇒ Object
217
218
219
220
221
222
223
|
# File 'lib/nzbgetpp.rb', line 217
def scratch_directory
@scratch_directory ||= Pathname.new(File.join(*[
config.scratch_directory,
self.category,
path.basename,
].compact)).expand_path
end
|
#storage_directory ⇒ Object
225
226
227
228
229
230
231
|
# File 'lib/nzbgetpp.rb', line 225
def storage_directory
@storage_directory ||= Pathname.new(File.join(*[
config.storage_directory,
self.category,
path.basename,
].compact)).expand_path
end
|
#unrar_targets ⇒ Object
212
213
214
215
|
# File 'lib/nzbgetpp.rb', line 212
def unrar_targets
@unrar_targets ||= Pathname.new(File.join(path,
config.unrar_targets)).expand_path
end
|