Class: Fixi::Command::Unbag

Inherits:
Object
  • Object
show all
Defined in:
lib/fixi/command/unbag.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.arghelpObject



10
11
12
# File 'lib/fixi/command/unbag.rb', line 10

def self.arghelp
  "<input-dir> <output-dir>"
end

.detailsObject



14
15
16
17
# File 'lib/fixi/command/unbag.rb', line 14

def self.details
  "Where:\n  input-dir is the base directory of the bag.\n" +
    "  output-dir is the directory in which to import it."
end

.synopsisObject



6
7
8
# File 'lib/fixi/command/unbag.rb', line 6

def self.synopsis
  "Import files from a BagIt bag"
end

Instance Method Details

#execute(args) ⇒ Object



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
# File 'lib/fixi/command/unbag.rb', line 19

def execute args
  opts = Trollop::options args do
    banner Fixi::Command.banner "unbag"
    opt :absolute, "Show absolute paths. By default, paths are reported
      relative to the index root.".pack
  end

  # validate args and bag dir
  raise "Must specify input directory." unless args[0]
  raise "Must specify output directory." unless args[1]
  raise "Output directory already exists." if File.exists?(args[1])

  input_dir = File.expand_path(args[0])
  output_dir = File.expand_path(args[1])

  index = Fixi::Index.new(File.dirname(output_dir))
  Dir.mkdir(output_dir)

  data_dir = input_dir + "/data"
  raise "No data dir found in input dir." unless File.directory?(data_dir)

  # build hash of manifest algorithm => file path
  manifests = {}
  indexed_algs = index.algorithms.split(",")
  Dir.entries(input_dir).each do |child|
    if child =~ /^manifest-.*\.txt/
      alg = child[9..(child.length - 5)]
      if (indexed_algs.include?(alg))
        manifests[alg] = input_dir + "/" + child
      end
    end
  end

  # copy all data to dest dir, subject to includes and excludes
  Find.find(data_dir) do |abspath|
    unless abspath == data_dir
      relpath = abspath.slice(data_dir.length + 1..-1)
      destpath = output_dir + "/" + relpath
      if index.matches_any?(relpath, index.includes)
        if index.matches_any?(relpath, index.excludes)
          Find.prune
        elsif not File.directory?(abspath)
          begin
            FileUtils.cp(abspath, destpath, :preserve => true)
          rescue
            FileUtils.mkdir_p(File.dirname(destpath))
            FileUtils.cp(abspath, destpath, :preserve => true)
          end
        end
      else
        Find.prune unless File.directory?(abspath)
      end
    end
  end

  # revisit all dirs under output_dir, copying orig dir metadata
  Find.find(output_dir) do |dir|
    if File.directory?(dir) && dir != output_dir
      orig_dir = data_dir + dir[output_dir.length..-1]
      Fixi::(dir, File.stat(orig_dir))
    end
  end

  # add paths, sizes, and mtimes to db
  index.find(output_dir) do |abspath|
    relpath = index.relpath(abspath)
    index.add(relpath, false)
  end

  # add manifest entries to db
  manifests.each do |alg, manifest|
    IO.foreach(manifest) do |line|
      i = line.index(" ")
      if i 
        digest = line[0..i-1].downcase
        bagpath = line[i+6..-1].rstrip
        relpath = index.relpath(output_dir + "/" + bagpath)
        index.set_digest(relpath, alg, digest)
      end
    end
  end

  # for all new files, compute any required hashes and print A path/etc
  algs = index.algorithms.split(",")
  index.each(output_dir) do |hash|
    relpath = hash['relpath']
    abspath = index.rootpath + '/' + relpath
    puts "A #{opts[:absolute] ? abspath : relpath}"

    missing_digests = []
    algs.each do |alg|
      missing_digests << alg unless hash[alg]
    end
    if missing_digests.size > 0
      digests = Fixi::digests(missing_digests.join(","))
      hexdigests = Fixi::hexdigests(digests, abspath)
      i = 0
      missing_digests.each do |alg|
        index.set_digest(relpath, alg, hexdigests[i])
        i += 1
      end
    end
  end

end