blah blah

Ruby – Staking Out File Changes

쉘스크립트로 처리해왔던 작업들을 Ruby 로 하나씩 전환하기 위해 고민하는 중.
첫번째 문제 실마리 찾다가 건진 깜찍한 스크립트 하나.
샘플 소스에서 변경 파일명을 한번씩만 로그로 남기기 위해 약간 수정해보았다.

출처 : Staking Out File Changes

if ARGV.size < 2 puts "Usage: #{File.basename(__FILE__)} <command> [files to watch]+" exit 1 end command = ARGV.shift files = {} h = {} job_log = "list.txt" ARGV.each do |arg| Dir[arg].each { |file| files[file] = File.mtime(file) } end trap('INT') do puts "\nQuitting..." exit end File.delete(job_log) if (File::exist?(job_log)) File.open(job_log, "w") do |output| loop do sleep 1 changed_file, last_changed = files.find { |file, last_changed| File.mtime(file) > last_changed } if changed_file files[changed_file] = File.mtime(changed_file) puts "=> #{changed_file} changed, running #{command}" system(command) unless h.has_key?(changed_file) output << "#{changed_file}\n" output.flush h.store(changed_file, "U") end end end end
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s