about summary refs log tree commit homepage
path: root/lib/dtas/watchable.rb
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2015-05-19 09:14:23 +0000
committerEric Wong <e@80x24.org>2015-05-19 09:16:16 +0000
commitc02f0b8182b35df1a318418bbd0036c00be93b5c (patch)
treecf24d9e3c47ee3e3dfbe2ae697fa92b3c9f53079 /lib/dtas/watchable.rb
parent7b47191aa4c88b3daa4c980013f0047cb7ae7f6d (diff)
downloaddtas-c02f0b8182b35df1a318418bbd0036c00be93b5c.tar.gz
Since writing nested shell commands inside YAML is subject to all
sorts of strange quoting rules, encourage users to rely on external
scripts which the YAML file refers to instead.  These scripts can be
written in any reasonable scripting language capable of executing
other commands.

This allows transparently monitoring things such as `my-script.rb'
in the below example when playing my-splitfx.yml via dtas-player:

  --------------------- my-splitfx.yml -----------------------
  infile: input.flac
  command: $INDIR/my-script.rb "$INFILE"
  ...

  --------------------- my-script.rb --------------------------
  #!/usr/bin/ruby
  require 'shellwords'
  infile = ARGV.shift
  ch = %W(sox #{infile} -p).concat((ENV['TRIMFX'] || '').shellsplit)
  fx = %W(highpass 25 gain 9)
  l = ch.dup.concat(%W(remix 1v1)).concat(fx).concat(%w(contrast 30))
  r = ch.dup.concat(%W(remix 2v1)).concat(fx).concat(%w(contrast 0))
  cmd = %W(sox -M |#{l.shelljoin} |#{r.shelljoin})
  cmd.concat((ENV['SOXFMT'] || '-p').shellsplit)
  cmd.concat(%w(- stats))
  warn cmd.inspect
  exec *cmd
Diffstat (limited to 'lib/dtas/watchable.rb')
-rw-r--r--lib/dtas/watchable.rb21
1 files changed, 16 insertions, 5 deletions
diff --git a/lib/dtas/watchable.rb b/lib/dtas/watchable.rb
index cc6c018..36e8644 100644
--- a/lib/dtas/watchable.rb
+++ b/lib/dtas/watchable.rb
@@ -18,7 +18,8 @@ module DTAS::Watchable
     def readable_iter
       or_call = false
       while event = take(true) # drain the buffer
-        if (event.mask & FLAGS) != 0 && @watching[1] == event.name
+        w = @watches[event.wd] or next
+        if (event.mask & FLAGS) != 0 && w[event.name]
           or_call = true
         end
       end
@@ -31,19 +32,29 @@ module DTAS::Watchable
     end
 
     # we must watch the directory, since
-    def watch_file(path, blk)
+    def watch_files(paths, blk)
+      @watches = {} # wd -> { basename -> true }
       @on_readable = blk
-      @watching = File.split(File.expand_path(path))
-      add_watch(@watching[0], FLAGS)
+      @dir2wd = {}
+      Array(paths).each do |path|
+        watchdir, watchbase = File.split(File.expand_path(path))
+        wd = @dir2wd[watchdir] ||= add_watch(watchdir, FLAGS)
+        m = @watches[wd] ||= {}
+        m[watchbase] = true
+      end
     end
   end
 
   def watch_begin(blk)
     @ino = InotifyReadableIter.new
-    @ino.watch_file(@infile, blk)
+    @ino.watch_files(@watch_extra << @infile, blk)
     @ino
   end
 
+  def watch_extra(paths)
+    @ino.watch_extra(paths)
+  end
+
   # Closing the inotify descriptor (instead of using inotify_rm_watch)
   # is cleaner because it avoids EINVAL on race conditions in case
   # a directory is deleted: https://lkml.org/lkml/2007/7/9/3