Class: NSWTopo::Archive
- Inherits:
-
Object
show all
- Extended by:
- Safely
- Includes:
- Enumerable
- Defined in:
- lib/nswtopo/archive.rb
Constant Summary
collapse
- Invalid =
Class.new RuntimeError
Constants included
from Log
Log::FAILURE, Log::NEUTRAL, Log::SUCCESS, Log::UPDATE
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Safely
safely
Methods included from Log
#log_abort, #log_neutral, #log_success, #log_update, #log_warn
Constructor Details
#initialize(tar) ⇒ Archive
Returns a new instance of Archive.
7
8
9
|
# File 'lib/nswtopo/archive.rb', line 7
def initialize(tar)
@tar, @entries = tar, Hash[]
end
|
Class Method Details
.open(out_path: nil, in_path: nil, &block) ⇒ Object
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
|
# File 'lib/nswtopo/archive.rb', line 61
def self.open(out_path: nil, in_path: nil, &block)
buffer, reader = StringIO.new, in_path ? Zlib::GzipReader : StringIO
reader.open(*in_path) do |input|
begin
version = Version[input.]
raise "map file too old: created with nswtopo %s, minimum %s required" % [version, MIN_VERSION] unless version >= MIN_VERSION
raise "nswtopo too old: map file created with nswtopo %s, this version %s" % [version, VERSION] unless version <= VERSION
rescue Version::Error
raise "unrecognised map file: %s" % in_path
end if in_path && false != Config["versioning"]
Gem::Package::TarReader.new(input) do |tar|
archive = new(tar).tap(&block)
Gem::Package::TarWriter.new(buffer) do |tar|
archive.each &tar.method(:add_entry)
end if archive.changed?
end
end
Dir.mktmppath do |temp_dir|
log_update "nswtopo: saving map..."
temp_path = temp_dir / "temp.tgz"
Zlib::GzipWriter.open temp_path, Config["zlib-level"] || Zlib::BEST_SPEED do |gzip|
gzip. = VERSION.creator_string
gzip.write buffer.string
rescue Interrupt
log_update "nswtopo: interrupted, please wait..."
raise
end
safely "saving map file, please wait..." do
FileUtils.cp temp_path, out_path
rescue SystemCallError
raise "couldn't save #{out_path}"
end
log_success "map saved"
end if out_path && buffer.size.nonzero?
rescue Zlib::GzipFile::Error
raise Invalid
end
|
Instance Method Details
#changed? ⇒ Boolean
57
58
59
|
# File 'lib/nswtopo/archive.rb', line 57
def changed?
@entries.any?
end
|
#delete(filename) ⇒ Object
27
28
29
30
31
32
33
|
# File 'lib/nswtopo/archive.rb', line 27
def delete(filename)
find do |entry|
entry.full_name == filename
end&.tap do
@entries[filename] = nil
end
end
|
#each(&block) ⇒ Object
17
18
19
20
21
22
23
24
25
|
# File 'lib/nswtopo/archive.rb', line 17
def each(&block)
@tar.rewind
@tar.each do |entry|
yield entry unless @entries.key? entry.full_name
end
@entries.each do |filename, entry|
yield entry if entry
end
end
|
#mtime(filename) ⇒ Object
41
42
43
44
45
46
47
|
# File 'lib/nswtopo/archive.rb', line 41
def mtime(filename)
find do |entry|
entry.full_name == filename
end&.then do |entry|
Time.at entry..mtime
end
end
|
#read(filename) ⇒ Object
35
36
37
38
39
|
# File 'lib/nswtopo/archive.rb', line 35
def read(filename)
find do |entry|
entry.full_name == filename
end&.read
end
|
#uptodate?(depender, *dependees) ⇒ Boolean
49
50
51
52
53
54
55
|
# File 'lib/nswtopo/archive.rb', line 49
def uptodate?(depender, *dependees)
return unless mtime(depender)
dependees.all? do |dependee|
mtimes = [depender, dependee].map(&method(:mtime))
mtimes.all? && mtimes.inject(&:>=)
end
end
|
#write(filename, content) ⇒ Object
11
12
13
14
15
|
# File 'lib/nswtopo/archive.rb', line 11
def write(filename, content)
io = StringIO.new content
= Gem::Package::TarHeader.new name: filename, size: io.size, prefix: "", mode: 0o0644, mtime: Time.now
@entries[filename] = Gem::Package::TarReader::Entry.new , io
end
|