module OnceOnly::Check

Public Class Methods

calc_checksum(buf) click to toggle source
# 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) click to toggle source

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.

# 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) click to toggle source
# 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) click to toggle source

Drop -d argument from list

# 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) click to toggle source

Drop –pbs and optional argument from list

# 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) click to toggle source

filter out all names accoding to filters

# 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) click to toggle source

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

# 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) click to toggle source

filter out all arguments that reflect existing files

# 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') click to toggle source

Create a file name out of the content of checksums

# 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) click to toggle source

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

# 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) click to toggle source

Put quotes around regexs and globs

# 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) click to toggle source
# 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

Protected Class Methods

exit_error(msg, errval=1) click to toggle source
# File lib/once-only/check.rb, line 125
def Check::exit_error msg, errval=1
  $stderr.print "\nERROR: ",msg
  $stderr.print " (once-only returned error #{errval})!\n"
  exit errval
end
get_existing_filename(arg) click to toggle source
# File lib/once-only/check.rb, line 117
def Check::get_existing_filename arg
  return arg if File.exist?(arg)
  # sometimes arguments are formed as -in=file
  (option,filename) = arg.split(/=/)
  return filename if filename and File.exist?(filename)
  nil
end