lib/cheating_detection.rb in brown_noser-0.2.0 vs lib/cheating_detection.rb in brown_noser-0.2.1

- old
+ new

@@ -1,90 +1,50 @@ require 'moss_ruby' require 'fileutils' class CheatingDetection + MOSS_LOGGER_CALLBACK = ->(message=""){puts message} TEMP_DIR = 'brown_noser_cheat_detection' - def initialize(user, repo, moss_id = 000000000) + def initialize(user, repo, moss_options={}) @user = user @repo = repo - @moss_id = moss_id - end + @moss_id = moss_options.fetch(:moss_id, 000000000) - def detect - pull_details = PullBranchLister.new(@user, @repo).list - - recreate_tmp_dir - - commands = pull_details.map &command_orgnaizer() - - commands.flatten.each do |command| - prepared_command = command.call - puts prepared_command unless prepared_command.empty? - `#{prepared_command}` unless prepared_command.empty? - end - # Create the MossRuby object @moss ||= MossRuby.new(@moss_id) #replace 000000000 with your user id # Set options -- the options will already have these default values - @moss.options[:max_matches] = 10 - @moss.options[:directory_submission] = false - @moss.options[:show_num_matches] = 250 - @moss.options[:experimental_server] = false - @moss.options[:comment] = "" - @moss.options[:language] = "cc" + @moss.options[:max_matches] = moss_options.fetch(:max_matches, 10) + @moss.options[:directory_submission] = moss_options.fetch(:directory_submission, false) + @moss.options[:show_num_matches] = moss_options.fetch(:show_num_matches, 250) + @moss.options[:experimental_server] = moss_options.fetch(:experimental_server, false) + @moss.options[:comment] = moss_options.fetch(:comment, '') + @moss.options[:language] = moss_options.fetch(:comment, 'cc') + end + def detect + pull_details = PullBranchLister.new(@user, @repo).list + extractor = PullBranchFileExtractor.new(TEMP_DIR, pull_details) + extractor.extract + # Create a file hash, with the files to be processed to_check = MossRuby.empty_file_hash - #MossRuby.add_file(to_check, "#{TEMP_DIR}/**/*.h") - MossRuby.add_file(to_check, "#{TEMP_DIR}/**/*.cpp") + files_to_check = Dir.glob("#{TEMP_DIR}/**/*").select { |f| f =~ /\.(h|cpp)$/ } + MossRuby.add_file(to_check, files_to_check) # Get server to process files - url = @moss.check to_check + url = @moss.check to_check, MOSS_LOGGER_CALLBACK IO.write "brown_noser.html", "<div><h3>Cheat Detection Results</h3><br/><a href='#{url}'>#{url}</a></div>" - FileUtils.rm_rf TEMP_DIR + extractor.cleanup # Get results results = @moss.extract_results url end private - def make_folder_for_branch(user, branch) - ->(){ FileUtils.mkdir_p("#{TEMP_DIR}/#{user}_#{branch}") } - end - - def copy_file(source, dest) - ->(){ "cp \"#{source}\" \"#{dest}\"" } - end - - def extract_source_files(user, branch, dest_folder) - ->(){ - files = `git ls-tree --full-name --name-only -r #{user}/#{branch} | grep '.h$\\|.cpp$'`.split("\n") - copy_files = files.map do |file| - copy_file(file, "#{dest_folder}/#{file}").call - end - copy_files.join(" && ") - } - end - - def recreate_tmp_dir - FileUtils.rm_rf TEMP_DIR - FileUtils.mkdir TEMP_DIR - end - - def command_orgnaizer - ->(pull_context){ - [ - make_folder_for_branch(pull_context[0], pull_context[1]), - ProjectRepoSync::git_checkout("#{pull_context[0]}/#{pull_context[1]}"), - extract_source_files(pull_context[0], pull_context[1], "#{TEMP_DIR}/#{pull_context[0]}_#{pull_context[1]}") - ] - } - end - def extract_results results.each_with_index { |match, i| match.each { |file| report_match = <<-HTML <div class="match">