Class: Labrea

Inherits:
Object
  • Object
show all
Defined in:
lib/labrea.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, install_dir, opts) ⇒ Labrea

Class initialization



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/labrea.rb', line 9

def initialize(filename, install_dir, opts)
  @filename   = filename
  @install_dir  = install_dir
  @exclude    = exclude
  @working_dir  = Dir.pwd()
  @changeset    = Array.new
  defaults = {
    :exclude => [],
    :checksum_file => "checksum.json"
  }
  opts = defaults.merge(opts)
  @exclude = opts[:exclude]
  @checksum_file = opts[:checksum_file]
end

Instance Attribute Details

#checksum_fileObject

Returns the value of attribute checksum_file.



25
26
27
# File 'lib/labrea.rb', line 25

def checksum_file
  @checksum_file
end

#excludeObject

Returns the value of attribute exclude.



24
25
26
# File 'lib/labrea.rb', line 24

def exclude
  @exclude
end

Instance Method Details

#checksum_fqnObject



145
146
147
148
149
150
151
# File 'lib/labrea.rb', line 145

def checksum_fqn
  if @checksum_file =~ /^#{File::SEPARATOR}/
    @checksum_file
  else
    File.join(@install_dir,@checksum_file)
  end
end

#extract_file(file, testmode) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/labrea.rb', line 122

def extract_file(file, testmode)
  Dir.chdir(@working_dir) do |path|
    @changeset << file.to_s

    case filetype(@filename)
    when :tgz
  `tar -xzf #{@filename} -C #{@install_dir} #{file}` unless testmode
    when :zip
  `unzip -q #{@filename} -d #{@install_dir} #{file}` unless testmode
    when :bz2
  `tar -xjf #{@filename} -C #{@install_dir} #{file}` unless testmode
    end
  end
end

#extract_files(testmode) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/labrea.rb', line 106

def extract_files(testmode)
  Dir.chdir(@working_dir) do |path|

    case filetype(@filename)
    when :tgz
  output = `tar -xvzf #{@filename} -C #{@install_dir} 2>/dev/null` unless testmode
    when :zip
  `unzip #{@filename} -d #{@install_dir}` unless testmode
      output = `zipinfo -1 #{@filename}`
    when :bz2
  output = `tar -xvjf #{@filename} -C #{@install_dir} 2>/dev/null` unless testmode
    end
    @changeset |= output.split("\n") if output
  end
end

#filetype(filename) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/labrea.rb', line 86

def filetype(filename)
  if filename.match('.*\.tgz')
    return :tgz
  end

  if filename.match('.*\.tar\.gz')
    return :tgz
  end

  if filename.match('.*\.zip')
    return :zip
  end

  if filename.match('.*\.bz2')
    return :bz2
  end

  return :unknown
end

#install(testmode = false) ⇒ Object

Installation of binary archive



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
# File 'lib/labrea.rb', line 28

def install(testmode=false)
  @changeset.clear
  extract_files(testmode)

  # Generate checksums and add to hash
  checksums = Hash.new
  if File.exists?(@install_dir)
    Dir.chdir(@install_dir) do |path|
      @changeset.each do |file|
        checksums[file] = sha1sum(file) unless @exclude.include? file
      end
    end 
  # Dir.glob("**/*.*") do |file|
  #   @changeset << file.to_s
  #   if File.file?(file)
  #     if [email protected]? file
  #       checksums[file] = sha1sum(file)
  #     end
  #   end
  # end
  #  end
  end
  
  # Write out checksum file
  File.open(checksum_fqn, "w+") do |file|
    file.puts JSON.generate(checksums)
  end unless testmode

  return @changeset
end

#sha1sum(file) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/labrea.rb', line 137

def sha1sum(file)
    if File.file?(file)
  return Digest::SHA1.hexdigest File.read(file)
    end

    return false
end

#update(testmode = false) ⇒ Object

Update of binary archive



60
61
# File 'lib/labrea.rb', line 60

def update(testmode=false)
end

#verify(testmode = false) ⇒ Object

Verification of binary archive



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/labrea.rb', line 64

def verify(testmode=false)
  @changeset.clear

  # Read checksums from file
  checksums = Hash.new
  File.open(checksum_fqn, "r") do |file|
    checksums = JSON.load(file)
  end

  checksums.each_pair do |k,v|
    if !@exclude.include? k
      Dir.chdir(@install_dir) do |path|
 if sha1sum(k) != v
   extract_file(k, testmode)
 end
      end
    end
  end

  return @changeset
end