From d2f75170a6c1548b85335c7e84a000ed67a0158b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 7 Sep 2013 21:25:17 +0000 Subject: implement environment variable expansion This will allow users to more-easily edit configs and feel like a real shell. We no longer mistakenly expand nil env variables to "" anymore, either. --- lib/dtas/process.rb | 28 +++++++++++++++++++++++++--- lib/dtas/source/sox.rb | 2 +- 2 files changed, 26 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/dtas/process.rb b/lib/dtas/process.rb index b90b318..a3c1c04 100644 --- a/lib/dtas/process.rb +++ b/lib/dtas/process.rb @@ -19,12 +19,33 @@ module DTAS::Process # :nodoc: end while true end + # expand common shell constructs based on environment variables + # this is order-dependent, but Ruby 1.9+ hashes are already order-dependent + def env_expand(env, opts) + env = env.dup + if false == opts.delete(:expand) + env.each do |key, val| + Numeric === val and env[key] = val.to_s + end + else + env.each do |key, val| + case val + when Numeric # stringify numeric values to simplify users' lives + env[key] = val.to_s + when /[\`\$]/ # perform variable/command expansion + tmp = env.dup + tmp.delete(key) + val = qx(tmp, "echo #{val}", expand: false) + env[key] = val.chomp + end + end + end + end + # for long-running processes (sox/play/ecasound filters) def dtas_spawn(env, cmd, opts) opts = { close_others: true, pgroup: true }.merge!(opts) - - # stringify env, integer values are easier to type unquoted as strings - env.each { |k,v| env[k] = v.to_s } + env = env_expand(env, opts) pid = begin Process.spawn(env, cmd, opts) @@ -53,6 +74,7 @@ module DTAS::Process # :nodoc: re.binmode opts[:err] = we end + env = env_expand(env, opts) pid = begin Process.spawn(env, *cmd, opts) rescue Errno::EINTR # Ruby bug? diff --git a/lib/dtas/source/sox.rb b/lib/dtas/source/sox.rb index e26f54b..484a0ec 100644 --- a/lib/dtas/source/sox.rb +++ b/lib/dtas/source/sox.rb @@ -41,7 +41,7 @@ class DTAS::Source::Sox # :nodoc: def try(infile, offset = nil) err = "" cmd = %W(soxi -s #{infile}) - s = qx(@env, cmd, err_str: err, no_raise: true) + s = qx(@env.dup, cmd, err_str: err, no_raise: true) return if err =~ /soxi FAIL formats:/ self.class.try_to_fail_harder(infile, s, cmd) or return source_file_dup(infile, offset) -- cgit v1.2.3-24-ge0c7