forked from tipr/bagit
/
bag.rb
99 lines (82 loc) · 2.7 KB
/
bag.rb
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
require 'bagit/fetch'
require 'bagit/file'
require 'bagit/info'
require 'bagit/manifest'
require 'bagit/string'
require 'bagit/valid'
module BagIt
# Represents the state of a bag on a filesystem
class Bag
attr_reader :bag_dir
include Validity # Validity functionality
include Info # bagit & bag info functionality
include Manifest # manifest related functionality
include Fetch # fetch related functionality
# Make a new Bag based at path
def initialize(path)
@bag_dir = path
# make the dir structure if it doesn't exist
FileUtils::mkdir bag_dir unless File.directory? bag_dir
FileUtils::mkdir data_dir unless File.directory? data_dir
# write some tag info if its not there
unless File.exist? bagit_txt_file
write_bagit("BagIt-Version" => SPEC_VERSION, "Tag-File-Character-Encoding" => "UTF-8")
end
unless File.exist? bag_info_txt_file
write_bag_info('Bag-Software-Agent' => "BagIt Ruby Gem (http://bagit.rubyforge.org)")
end
end
# Return the path to the data directory
def data_dir
File.join @bag_dir, 'data'
end
# Return the paths to each bag file relative to bag_dir
def bag_files
Dir[File.join(data_dir, '**', '*')].select { |f| File.file? f }
end
# Return the paths to each tag file relative to bag_dir
def tag_files
Dir[File.join(@bag_dir, '*')].select { |f| File.file? f }
end
# Add a bag file
def add_file(base_path, src_path=nil)
path = File.join(data_dir, base_path)
raise "Bag file exists: #{base_path}" if File.exist? path
FileUtils::mkdir_p File.dirname(path)
if src_path.nil?
open(path, 'w') { |io| yield io }
else
FileUtils::cp src_path, path
end
end
# Remove a bag file
def remove_file(base_path)
path = File.join(data_dir, base_path)
raise "Bag file does not exist: #{base_path}" unless File.exist? path
FileUtils::rm path
end
# Retrieve the IO handle for a file in the bag
def get(base_path)
path = File.join(data_dir, base_path)
return nil unless File.exist?(path)
File.open(path)
end
# Test if this bag is empty (no files)
def empty?
self.bag_files.empty?
end
# Get all bag file paths relative to the data dir
def paths
self.bag_files.collect { |f| f.sub(data_dir + '/', '') }
end
# Remove all empty directory trees from the bag
def gc!
Dir.entries(data_dir).each do |f|
unless %w{.. .}.include? f
abs_path = File.join data_dir, f
File.clean abs_path
end
end
end
end
end