Method: MJ::Tools::SubProcess::ClassMethods#adjust_environment

Defined in:
lib/mj/tools/subprocess.rb

#adjust_environment(wd = nil, env = nil, lang = "C") ⇒ Object

Helper method to adjust LANG to ā€œCā€



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/mj/tools/subprocess.rb', line 50

def adjust_environment( wd=nil, env=nil, lang="C" )
    begin
        # Go to the working directory if given
        if wd
            cwd = Dir.getwd
            Dir.chdir(wd) if not $noop
            logger.debug( "Changed working directory from #{cwd} to #{wd}" )
        end

        # Set the environment the user wants
        oldenv = Hash.new
        if env
            env.each do |var, value|
                oldenv[var] = ENV[var]
                if value.nil? or value == ""
                    next if ENV.has_key?( value )
                    ENV[var] = nil
                    logger.verbose  "Removing #{var} from environment"
                else
                    logger.verbose  "#{var} = #{value}"
                    ENV[var] = value
                end
            end
        end
        # Save old LANG setting and switch to 'C'
        oldlang = ENV['LANG']
        ENV['LANG'] = lang
        yield
        # Reset the old LANG setting
        ENV['LANG'] = oldlang
        # Reset our changes to ENV
        oldenv.each do |var, value|
            ENV[var] = value
        end

    ensure
        # Reset the current working directory
        if wd
            logger.debug( "Changed working directory back to #{cwd}" )
            Dir.chdir(cwd) if not $noop
        end

    end

end