2015-05-07 00:54:07 +03:00
|
|
|
#!ruby
|
|
|
|
|
2019-07-15 07:15:13 +03:00
|
|
|
require_relative 'lib/vpath'
|
2015-05-07 00:54:07 +03:00
|
|
|
|
|
|
|
class Checksum
|
|
|
|
def initialize(vpath)
|
|
|
|
@vpath = vpath
|
|
|
|
end
|
|
|
|
|
2015-05-07 10:09:43 +03:00
|
|
|
attr_reader :source, :target
|
|
|
|
|
2015-05-07 00:54:07 +03:00
|
|
|
def source=(source)
|
|
|
|
@source = source
|
|
|
|
@checksum = File.basename(source, ".*") + ".chksum"
|
|
|
|
end
|
|
|
|
|
2015-05-07 10:09:43 +03:00
|
|
|
def target=(target)
|
|
|
|
@target = target
|
|
|
|
end
|
|
|
|
|
2015-05-07 00:54:07 +03:00
|
|
|
def update?
|
|
|
|
src = @vpath.read(@source)
|
|
|
|
@len = src.length
|
|
|
|
@sum = src.sum
|
2015-05-07 10:09:43 +03:00
|
|
|
return false unless @vpath.search(File.method(:exist?), @target)
|
2015-05-07 00:54:07 +03:00
|
|
|
begin
|
|
|
|
data = @vpath.read(@checksum)
|
|
|
|
rescue
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return false unless data[/src="([0-9a-z_.-]+)",/, 1] == @source
|
|
|
|
return false unless @len == data[/\blen=(\d+)/, 1].to_i
|
|
|
|
return false unless @sum == data[/\bchecksum=(\d+)/, 1].to_i
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update!
|
2022-11-21 10:20:17 +03:00
|
|
|
File.binwrite(@checksum, "src=\"#{@source}\", len=#{@len}, checksum=#{@sum}")
|
2015-05-07 00:54:07 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
return true if update?
|
|
|
|
update! if ret = yield(self)
|
|
|
|
ret
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy(name)
|
|
|
|
@vpath.open(name, "rb") {|f|
|
|
|
|
IO.copy_stream(f, name)
|
|
|
|
}
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-05-07 10:09:43 +03:00
|
|
|
def make(*args)
|
|
|
|
system(@make, *args)
|
2015-05-07 00:54:07 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def def_options(opt = (require 'optparse'; OptionParser.new))
|
|
|
|
@vpath.def_options(opt)
|
|
|
|
opt.on("--make=PATH") {|v| @make = v}
|
|
|
|
opt
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.update(argv)
|
|
|
|
k = new(VPath.new)
|
2015-05-07 10:09:43 +03:00
|
|
|
k.source, k.target, *argv = k.def_options.parse(*argv)
|
2018-05-18 03:40:06 +03:00
|
|
|
k.update {|_| yield(_, *argv)}
|
2015-05-07 00:54:07 +03:00
|
|
|
end
|
|
|
|
end
|