Files
dunlop-core/app/services/dunlop/file_grep.rb
T

36 lines
1.3 KiB
Ruby

module Dunlop::FileGrep
def self.egrep( input_filename, output_filename, regexp )
%x[egrep '#{regexp}' #{Shellwords.escape(input_filename)} > #{Shellwords.escape(output_filename)}]
end
def self.egrep_v( input_filename, output_filename, regexp )
%x[egrep -v '#{regexp}' #{Shellwords.escape(input_filename)} > #{Shellwords.escape(output_filename)}]
end
def self.select(input_filename, regexp_string)
filter input_filename, regexp_string, filter_method: :egrep
end
def self.filter(input_filename, regexp_string, filter_method: :egrep)
local_working_file = Tempfile.new("file_grep", WORKING_PATH)
#egrep(input_filename, local_working_file.path, regexp_string)
public_send(filter_method, input_filename, local_working_file.path, regexp_string)
FileUtils.cp(local_working_file.path, input_filename)
FileUtils.chmod(0644, input_filename)
ensure
local_working_file.unlink
end
def self.reject(input_filename, regexp_string)
filter input_filename, regexp_string, filter_method: :egrep_v
#local_working_file = Tempfile.new("file_grep", WORKING_PATH)
#egrep_v(input_filename, local_working_file.path, regexp_string)
#FileUtils.cp(local_working_file.path, input_filename)
#FileUtils.chmod(0644, input_filename)
#ensure
#local_working_file.unlink
end
end