Class: Isolate::Sandbox
- Inherits:
-
Object
- Object
- Isolate::Sandbox
- Defined in:
- lib/isolate/sandbox.rb
Overview
An isolated environment.
Constant Summary collapse
- DEFAULT_PATH =
:nodoc:
+"tmp/isolate" # :nodoc:
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
:nodoc:.
-
#environments ⇒ Object
readonly
:nodoc:.
-
#files ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#activate(environment = nil) ⇒ Object
Activate this set of isolated entries, respecting an optional
environment
. -
#cleanup ⇒ Object
:nodoc:.
- #cleanup? ⇒ Boolean
- #disable ⇒ Object
-
#enable ⇒ Object
:nodoc:.
- #enabled? ⇒ Boolean
-
#environment(*environments, &block) ⇒ Object
(also: #env)
Restricts
gem
calls insideblock
to a set ofenvironments
. -
#gem(name, *requirements) ⇒ Object
Express a gem dependency.
-
#initialize(options = {}, &block) ⇒ Sandbox
constructor
Create a new Isolate::Sandbox instance.
-
#install(environment) ⇒ Object
:nodoc:.
-
#install? ⇒ Boolean
:nodoc:.
- #install_missing(environment) ⇒ Object
-
#load(file) ⇒ Object
:nodoc:.
-
#log(s) ⇒ Object
:nodoc:.
- #multiruby? ⇒ Boolean
- #options(options = nil) ⇒ Object
- #path ⇒ Object
- #rebuild_extensions ⇒ Object
- #remove(*extra) ⇒ Object
- #system? ⇒ Boolean
- #verbose? ⇒ Boolean
Constructor Details
#initialize(options = {}, &block) ⇒ Sandbox
Create a new Isolate::Sandbox instance. See Isolate.now! for the most common use of the API. You probably don’t want to use this constructor directly.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/isolate/sandbox.rb', line 33 def initialize = {}, &block @enabled = false @entries = [] @environments = [] @files = [] @options = path, name = .values_at :path, :name raise ArgumentError, "can't specify both name and path!" if name && path [:path] = File.("~/.gem/repos/#{name}") if name user = File. "~/.isolate/user.rb" load user if File.exist? user file, local = nil unless FalseClass === [:file] file = [:file] || Dir["{Isolate,config/isolate.rb}"].first local = "#{file}.local" if file end load file if file if block_given? files << (block.to_s[/ (\/.+):\d+/, 1] || "inline block") instance_eval(&block) end load local if local && File.exist?(local) end |
Instance Attribute Details
#entries ⇒ Object (readonly)
:nodoc:
25 26 27 |
# File 'lib/isolate/sandbox.rb', line 25 def entries @entries end |
#environments ⇒ Object (readonly)
:nodoc:
26 27 28 |
# File 'lib/isolate/sandbox.rb', line 26 def environments @environments end |
#files ⇒ Object (readonly)
:nodoc:
27 28 29 |
# File 'lib/isolate/sandbox.rb', line 27 def files @files end |
Instance Method Details
#activate(environment = nil) ⇒ Object
Activate this set of isolated entries, respecting an optional environment
. Points RubyGems to a separate repository, messes with paths, auto-installs gems (if necessary), activates everything, and removes any superfluous gem (again, if necessary). If environment
isn’t specified, ISOLATE_ENV
, RAILS_ENV
, and RACK_ENV
are checked before falling back to "development"
.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/isolate/sandbox.rb', line 73 def activate environment = nil enable unless enabled? env = (environment || Isolate.env).to_s install env if install? entries.each do |e| e.activate if e.matches? env end cleanup if cleanup? self end |
#cleanup ⇒ Object
:nodoc:
89 90 91 92 93 94 95 96 97 |
# File 'lib/isolate/sandbox.rb', line 89 def cleanup # :nodoc: gem_dir = Gem.dir global, local = Gem::Specification.partition { |s| s.base_dir != gem_dir } legit = legitimize! extra = (local - legit) + (local & global) self.remove(*extra) end |
#cleanup? ⇒ Boolean
99 100 101 |
# File 'lib/isolate/sandbox.rb', line 99 def cleanup? install? and @options.fetch(:cleanup, true) end |
#disable ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/isolate/sandbox.rb', line 103 def disable return self unless enabled? ENV.replace @old_env $LOAD_PATH.replace @old_load_path @enabled = false Isolate.refresh if block_given? then begin return yield ensure enable end end self end |
#enable ⇒ Object
:nodoc:
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/isolate/sandbox.rb', line 124 def enable # :nodoc: return self if enabled? @old_env = ENV.to_hash @old_load_path = $LOAD_PATH.dup path = self.path FileUtils.mkdir_p path ENV["GEM_HOME"] = path unless system? isolate_lib = File. "../..", __FILE__ $LOAD_PATH.reject! { |lpath| # manually deactivate pre-isolate gems... (lpath.start_with?("/") && # only full paths lpath.end_with?("/lib") && # and that end in lib lpath != isolate_lib && # that aren't us Gem.path.reject(&:empty?).any? { |gem_path| lpath.include?(gem_path) # and are included in a gem's path(?) }) } # HACK: Gotta keep isolate explicitly in the LOAD_PATH in # subshells, and the only way I can think of to do that is by # abusing RUBYOPT. unless ENV["RUBYOPT"] =~ /\s+-I\s*#{Regexp.escape isolate_lib}\b/ ENV["RUBYOPT"] = "#{ENV['RUBYOPT']} -I#{isolate_lib}" end ENV["GEM_PATH"] = path end bin = File.join path, "bin" unless ENV["PATH"].split(File::PATH_SEPARATOR).include? bin ENV["PATH"] = [bin, ENV["PATH"]].join File::PATH_SEPARATOR end ENV["ISOLATED"] = path if system? then Gem.path.unshift path # HACK: this is just wrong! Gem.path.uniq! # HACK: needed for the previous line :( end Isolate.refresh @enabled = true self end |
#enabled? ⇒ Boolean
177 178 179 |
# File 'lib/isolate/sandbox.rb', line 177 def enabled? @enabled end |
#environment(*environments, &block) ⇒ Object Also known as: env
Restricts gem
calls inside block
to a set of environments
.
183 184 185 186 187 188 189 190 |
# File 'lib/isolate/sandbox.rb', line 183 def environment *environments, &block old = @environments @environments = @environments.dup.concat environments.map { |e| e.to_s } instance_eval(&block) ensure @environments = old end |
#gem(name, *requirements) ⇒ Object
Express a gem dependency. Works pretty much like RubyGems’ gem
method, but respects environment
and doesn’t activate ‘til later.
198 199 200 201 202 203 204 |
# File 'lib/isolate/sandbox.rb', line 198 def gem name, *requirements entry = entries.find { |e| e.name == name } return entry.update(*requirements) if entry entries << entry = Entry.new(self, name, *requirements) entry end |
#install(environment) ⇒ Object
:nodoc:
206 207 208 209 210 211 |
# File 'lib/isolate/sandbox.rb', line 206 def install environment # :nodoc: install_missing environment rebuild_extensions self end |
#install? ⇒ Boolean
:nodoc:
252 253 254 |
# File 'lib/isolate/sandbox.rb', line 252 def install? # :nodoc: @options.fetch :install, true end |
#install_missing(environment) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/isolate/sandbox.rb', line 213 def install_missing environment installable = entries.select do |e| !e.specification && e.matches?(environment) end unless installable.empty? padding = Math.log10(installable.size).to_i + 1 format = "[%0#{padding}d/%s] Isolating %s (%s)." installable.each_with_index do |entry, i| log format % [i + 1, installable.size, entry.name, entry.requirement] entry.install end Gem::Specification.reset end end |
#load(file) ⇒ Object
:nodoc:
256 257 258 259 |
# File 'lib/isolate/sandbox.rb', line 256 def load file # :nodoc: files << file instance_eval IO.read(file), file, 1 end |
#log(s) ⇒ Object
:nodoc:
261 262 263 |
# File 'lib/isolate/sandbox.rb', line 261 def log s # :nodoc: $stderr.puts s if verbose? end |
#multiruby? ⇒ Boolean
265 266 267 |
# File 'lib/isolate/sandbox.rb', line 265 def multiruby? @options.fetch :multiruby, false end |
#options(options = nil) ⇒ Object
269 270 271 272 |
# File 'lib/isolate/sandbox.rb', line 269 def = nil @options.merge! if @options end |
#path ⇒ Object
274 275 276 277 278 279 280 281 282 283 |
# File 'lib/isolate/sandbox.rb', line 274 def path base = @options.fetch :path, DEFAULT_PATH if multiruby? then suffix = "#{Gem.ruby_engine}-#{RbConfig::CONFIG['ruby_version']}" base = File.join(base, suffix) unless base =~ /#{suffix}/ end File. base end |
#rebuild_extensions ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/isolate/sandbox.rb', line 231 def rebuild_extensions broken = entries.find_all { |e| e.specification && e.specification.missing_extensions? } unless broken.empty? padding = Math.log10(broken.size).to_i + 1 format = "[%0#{padding}d/%d] Building extensions for %s (ruby v%s)." broken.each_with_index do |e, i| spec = e.specification log format % [i + 1, broken.size, e.name, RUBY_VERSION] builder = Gem::Ext::Builder.new spec builder.build_extensions end Gem::Specification.reset end end |
#remove(*extra) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/isolate/sandbox.rb', line 285 def remove(*extra) unless extra.empty? padding = Math.log10(extra.size).to_i + 1 format = "[%0#{padding}d/%s] Nuking %s." extra.each_with_index do |e, i| log format % [i + 1, extra.size, e.full_name] Gem::DefaultUserInteraction.use_ui Gem::SilentUI.new do uninstaller = Gem::Uninstaller.new(e.name, :version => e.version, :ignore => true, :executables => true, :install_dir => e.base_dir) uninstaller.uninstall end end end end |
#system? ⇒ Boolean
306 307 308 |
# File 'lib/isolate/sandbox.rb', line 306 def system? @options.fetch :system, true end |
#verbose? ⇒ Boolean
310 311 312 |
# File 'lib/isolate/sandbox.rb', line 310 def verbose? @options.fetch :verbose, true end |