Module: OnceOnly::Check

Defined in:
lib/once-only/check.rb

Class Method Summary collapse

Class Method Details

.calc_checksum(buf) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/once-only/check.rb', line 67

def Check::calc_checksum(buf)
  if $ruby_sha1
    Sha1::sha1(buf)
  else
    Digest::SHA1.hexdigest(buf)
  end
end

.calc_file_checksums(list, precalc) ⇒ Object

Calculate the checksums for each file in the list and return a list of array - each row containing the Hash type (MD5), the value and the (relative) file path.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/once-only/check.rb', line 53

def Check::calc_file_checksums list, precalc
  list.map { |fn|
    # First see if fn is in the precalculated list
    fqn = File.expand_path(fn)
    if precalc[fqn] and File.mtime(fqn) < precalc[fqn][:time]
      $stderr.print "Precalculated ",fn,"\n"
      rec = precalc[fqn]
      [rec[:type],rec[:hash],fqn]
    else
      ['MD5'] + `/usr/bin/md5sum #{fqn}`.split
    end
  }
end

.check_files_exist(list) ⇒ Object



17
18
19
20
21
# File 'lib/once-only/check.rb', line 17

def Check::check_files_exist list
  list.each { |fn| 
    Check::exit_error("File #{fn} does not exist!") if not File.exist?(fn)
  }
end

.drop_dir_option(list) ⇒ Object

Drop -d argument from list



105
106
107
108
109
110
111
112
# File 'lib/once-only/check.rb', line 105

def Check::drop_dir_option(list)
  is_part_of_arg = lambda { |p1, p2|
    (p1 == '-d' or p2 == '-d')
  }
  a = [ list[0] ]
  list.each_cons(2) { |pair| a << pair[1] if not is_part_of_arg.call(pair[0],pair[1])}
  a
end

.drop_pbs_option(list) ⇒ Object

Drop –pbs and optional argument from list



95
96
97
98
99
100
101
102
# File 'lib/once-only/check.rb', line 95

def Check::drop_pbs_option(list)
  is_part_of_pbs_arg = lambda { |p1, p2|
    (p1 == '--pbs' and p2 =~ /\s+/) or p2 == '--pbs'
  }
  a = [ list[0] ]
  list.each_cons(2) { |pair| a << pair[1] if not is_part_of_pbs_arg.call(pair[0],pair[1])}
  a
end

.filter_file_list(list, regex) ⇒ Object

filter out all names accoding to filters



24
25
26
# File 'lib/once-only/check.rb', line 24

def Check::filter_file_list list, regex
  list.map { |name| ( name =~ /#{regex}/ ? nil : name ) }.compact
end

.filter_file_list_glob(list, glob) ⇒ Object

filter out all names accoding to glob (this is not an efficient implementation, as the glob runs for every listed file!)



30
31
32
# File 'lib/once-only/check.rb', line 30

def Check::filter_file_list_glob list, glob
  list.map { |name| ( Dir.glob(glob).index(name) ? nil : name ) }.compact
end

.get_file_list(list) ⇒ Object

filter out all arguments that reflect existing files



13
14
15
# File 'lib/once-only/check.rb', line 13

def Check::get_file_list list
  list.map { |arg| get_existing_filename(arg) }.compact
end

.make_once_filename(checksums, prefix = 'once-only') ⇒ Object

Create a file name out of the content of checksums



76
77
78
79
# File 'lib/once-only/check.rb', line 76

def Check::make_once_filename checksums, prefix = 'once-only'
  buf = checksums.map { |entry| entry }.join("\n")
  prefix + '-' + calc_checksum(buf) + '.txt'
end

.precalculated_checksums(files) ⇒ Object

Return a hash of files with their hash type, hash value and check time



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/once-only/check.rb', line 35

def Check::precalculated_checksums(files)
  precalc = {}
  files.each do | fn |
    dir = File.dirname(fn)
    raise "Precalculated hash file should have .md5 extension!" if fn !~ /\.md5$/
    t = File.mtime(fn)
    File.open(fn).each { |s|
      a = s.split
      checkfn = File.expand_path(a[1],dir)
      precalc[checkfn] = { type: 'MD5', hash: a[0], time: t }
    }
  end
  precalc
end

.requote(list) ⇒ Object

Put quotes around regexs and globs



88
89
90
91
92
# File 'lib/once-only/check.rb', line 88

def Check::requote list
  a = [ list[0] ]
  list.each_cons(2) { |pair| a << (['--skip-glob','--skip-regex'].index(pair[0]) ? "'#{pair[1]}'" : pair[1]) }
  a
end

.write_file(fn, checksums) ⇒ Object



81
82
83
84
85
# File 'lib/once-only/check.rb', line 81

def Check::write_file fn, checksums
  File.open(fn,'w') { |f|
    checksums.each { |items| f.print items[0],"\t",items[1],"\t",items[2],"\n" }
  }
end