class Gemnasium::DependencyFiles

Constants

SUPPORTED_DEPENDENCY_FILES

Public Class Methods

get_content_to_upload(project_path, files_path) click to toggle source

Get the content to upload to Gemnasium.

@param files_path [Array] an array containing the path of the files @return [Array] array of hashes containing file name, file sha and file content

# File lib/gemnasium/dependency_files.rb, line 24
def self.get_content_to_upload(project_path, files_path)
  files_path.inject([]) do |arr, file_path|
    arr << { filename: file_path, sha: calculate_sha1(file_path), content: File.open("#{project_path}/#{file_path}") {|io| io.read} }
  end
end
get_sha1s_hash(project_path) click to toggle source

Get a Hash of sha1s for each file corresponding to the regex

@param regexp [Regexp] the regular expression of requested files @return [Hash] the hash associating each file path with its SHA1 hash

# File lib/gemnasium/dependency_files.rb, line 12
def self.get_sha1s_hash(project_path)
  Dir.chdir(project_path)
  Dir.glob("**/**").grep(SUPPORTED_DEPENDENCY_FILES).inject({}) do |h, file_path|
    h[file_path] = calculate_sha1("#{project_path}/#{file_path}") unless is_ignored?(file_path)
    h
  end
end

Private Class Methods

calculate_sha1(file_path) click to toggle source

Calculate hash of a file in the same way git does

@param file_path [String] path of the file @return [String] SHA1 of the file

# File lib/gemnasium/dependency_files.rb, line 36
def self.calculate_sha1(file_path)
  mem_buf = File.open(file_path) {|io| io.read}
  size = mem_buf.size
  header = "blob #{size}\0" # type[space]size[null byte]

  Digest::SHA1.hexdigest(header + mem_buf)
end
is_ignored?(file_path) click to toggle source

Test if the file is ignored by the configuration file

@param file_path [String] path of the file to test @return [Boolean] true if the file is ignored

# File lib/gemnasium/dependency_files.rb, line 48
def self.is_ignored?(file_path)
  Gemnasium.config.ignored_paths.each do |ignored_path|
    return true if file_path =~ ignored_path
  end
  false
end