Class: AutomateIt::ShellManager::Portable
- Inherits:
-
BaseDriver
- Object
- Common
- Plugin::Base
- Plugin::Driver
- BaseDriver
- AutomateIt::ShellManager::Portable
- Defined in:
- lib/automateit/shell_manager/portable.rb
Overview
ShellManager::Portable
Pure-Ruby, portable driver for ShellManager provides Unix-like shell commands for manipulating files and executing commands.
It does not provide commands for:
-
#which
-
#which!
Constant Summary collapse
- FILE_MASK =
0100000
- DIRECTORY_MASK =
040000
Constants inherited from Plugin::Driver
Plugin::Driver::BASE_DRIVER_NAME
Constants included from Constants
Constants::HELPERS_DIR, Constants::INSTALL_DIR, Constants::PERROR, Constants::PEXEC, Constants::PNOTE, Constants::WARNING_BOILERPLATE
Instance Attribute Summary
Attributes inherited from Plugin::Driver
Attributes inherited from Common
Instance Method Summary collapse
-
#backup(*sources) ⇒ Object
See ShellManager#backup.
-
#cd(dir, opts = {}, &block) ⇒ Object
See ShellManager#cd.
-
#chmod(mode, targets, opts = {}) ⇒ Object
See ShellManager#chmod.
-
#chmod_R(mode, targets, opts = {}) ⇒ Object
See ShellManager#chmod_R.
-
#chown(user, group, targets, opts = {}) ⇒ Object
See ShellManager#chown.
-
#chown_R(user, group, targets, opts = {}) ⇒ Object
See ShellManager#chown_R.
-
#chperm(targets, opts = {}) ⇒ Object
See ShellManager#chperm.
-
#cp(sources, target, opts = {}) ⇒ Object
See ShellManager#cp.
-
#cp_R(*args) ⇒ Object
See ShellManager#cp_R.
-
#cp_r(sources, target, opts = {}) ⇒ Object
See ShellManager#cp_r.
-
#install(source, target, mode = nil) ⇒ Object
See ShellManager#install.
-
#mkdir(dirs, opts = {}, &block) ⇒ Object
See ShellManager#mkdir.
-
#mkdir_p(dirs, opts = {}, &block) ⇒ Object
See ShellManager#mkdir_p.
-
#mktemp(name = nil, &block) ⇒ Object
See ShellManager#mktemp.
-
#mktempdir(name = nil, &block) ⇒ Object
See ShellManager#mktempdir.
-
#mktempdircd(name = nil, &block) ⇒ Object
See ShellManager#mktempdircd.
-
#mv(sources, target) ⇒ Object
See ShellManager#mv.
- #provides_mode? ⇒ Boolean
- #provides_ownership? ⇒ Boolean
-
#pwd ⇒ Object
See ShellManager#pwd.
-
#rm(targets, opts = {}) ⇒ Object
See ShellManager#rm.
-
#rm_r(targets, opts = {}) ⇒ Object
See ShellManager#rm_r.
-
#rm_rf(targets, opts = {}) ⇒ Object
See ShellManager#rm_rf.
-
#rmdir(dirs) ⇒ Object
See ShellManager#rmdir.
-
#sh(*commands) ⇒ Object
See ShellManager#sh.
-
#suitability(method, *args) ⇒ Object
:nodoc:.
-
#touch(targets, opts = {}) ⇒ Object
See ShellManager#touch.
-
#umask(mode = nil, &block) ⇒ Object
See ShellManager#umask.
Methods inherited from BaseDriver
Methods inherited from Plugin::Driver
abstract_driver, #available?, base_driver, base_driver?, depends_on, inherited, manager_token, #setup
Methods inherited from Plugin::Base
Methods inherited from Common
#initialize, #log, #nitpick, #noop, #noop=, #noop?, #preview, #preview=, #preview?, #preview_for, #setup, #superuser?, #writing, #writing=, #writing?
Constructor Details
This class inherits a constructor from AutomateIt::Common
Instance Method Details
#backup(*sources) ⇒ Object
See ShellManager#backup
32 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 64 65 66 67 68 69 |
# File 'lib/automateit/shell_manager/portable.rb', line 32 def backup(*sources) sources, opts = args_and_opts(*sources) targets = [] for source in sources is_dir = File.directory?(source) tempster_opts = { :verbose => false, :noop => noop?, :delete => false, :dir => File.dirname(source), :prefix => "%s.%s" % [File.basename(source), Time.now.to_i], :suffix => ".bak", :kind => is_dir ? :directory : :file, } target = ::Tempster.tempster(tempster_opts) log.silence(opts[:quiet] ? Logger::WARN : log.level) do if is_dir cp_opts = {} cp_opts[:recursive] = true if is_dir cp_opts[:preserve] = :try source_children = _directory_contents(source) #puts "sc: %s" % source_children.inspect interpreter.cp_r(source_children, target, cp_opts) else interpreter.cp(source, target) end end targets << target end return sources.size == 1 ? targets.first : targets end |
#cd(dir, opts = {}, &block) ⇒ Object
See ShellManager#cd
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/automateit/shell_manager/portable.rb', line 124 def cd(dir, opts={}, &block) if block log.enqueue(:info, PEXEC+(block ? "pushd" : "cd")+" "+dir) begin if writing? or File.directory?(dir) FileUtils.cd(dir, &block) else block.call(true) end rescue Exception => e raise e ensure log.dequeue(:info, PEXEC+"popd # => #{pwd}") end else FileUtils.cd(dir) if writing? end return dir end |
#chmod(mode, targets, opts = {}) ⇒ Object
See ShellManager#chmod
453 454 455 |
# File 'lib/automateit/shell_manager/portable.rb', line 453 def chmod(mode, targets, opts={}) chperm(targets, {:mode => mode}.merge(opts)) end |
#chmod_R(mode, targets, opts = {}) ⇒ Object
See ShellManager#chmod_R
458 459 460 |
# File 'lib/automateit/shell_manager/portable.rb', line 458 def chmod_R(mode, targets, opts={}) chmod(mode, targets, {:recursive => true}.merge(opts)) end |
#chown(user, group, targets, opts = {}) ⇒ Object
See ShellManager#chown
463 464 465 |
# File 'lib/automateit/shell_manager/portable.rb', line 463 def chown(user, group, targets, opts={}) chperm(targets, {:user => user, :group => group}.merge(opts)) end |
#chown_R(user, group, targets, opts = {}) ⇒ Object
See ShellManager#chown_R
468 469 470 |
# File 'lib/automateit/shell_manager/portable.rb', line 468 def chown_R(user, group, targets, opts={}) chown(user, group, targets, {:recursive => true}.merge(opts)) end |
#chperm(targets, opts = {}) ⇒ Object
See ShellManager#chperm
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/automateit/shell_manager/portable.rb', line 369 def chperm(targets, opts={}) _replace_owner_with_user(opts) user = \ if opts[:user] opts[:user] = opts[:user].to_s if opts[:user].is_a?(Symbol) if opts[:user].is_a?(String) begin Etc.getpwnam(opts[:user]).uid rescue ArgumentError :not_present end else opts[:user] end end group = \ if opts[:group] opts[:group] = opts[:group].to_s if opts[:group].is_a?(Symbol) if opts[:group].is_a?(String) begin Etc.getgrnam(opts[:group]).gid rescue ArgumentError :not_present end else opts[:group] end end modified_entries = [] modified_ownership = false = false Find.find(*targets) do |path| modified = false stat = writing? || File.exists?(path) ? File.stat(path) : nil if opts[:mode] # TODO ShellManager::Portable#chperm -- process chmod symbolic strings, e.g., [ugoa...][[+-=][rwxXstugo...]...][,...] mode = opts[:mode] | (stat.directory? ? DIRECTORY_MASK : FILE_MASK) if stat unless stat and (mode ^ stat.mode).zero? modified = true = true File.chmod(mode, path) if writing? end end if user and (not stat or user != stat.uid) modified = true modified_ownership = true File.chown(user, nil, path) if writing? end if group and (not stat or group != stat.gid) modified = true modified_ownership = true File.chown(nil, group, path) if writing? end modified_entries << path if modified Find.prune if not opts[:recursive] and File.directory?(path) end return false if modified_entries.empty? display_entries = opts[:details] ? modified_entries : targets display_entries = [display_entries].flatten if msg = "chmod" msg << " -R" if opts[:recursive] msg << " 0%o" % opts[:mode] if opts[:mode] msg << " " << display_entries.join(' ') log.info(PEXEC+msg) end if modified_ownership msg = "chown" msg << " -R" if opts[:recursive] msg << " %s" % opts[:user] if opts[:user] msg << ":" if opts[:user] and opts[:group] msg << "%s" % opts[:group] if opts[:group] msg << " " << display_entries.join(' ') log.info(PEXEC+msg) end return targets.is_a?(String) ? modified_entries.first : modified_entries end |
#cp(sources, target, opts = {}) ⇒ Object
See ShellManager#cp
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/automateit/shell_manager/portable.rb', line 210 def cp(sources, target, opts={}) # TODO ShellManager::Portable#cp -- rather funky, needs a code review fu_opts = _fileutils_opts for opt in [:noop, :verbose] opt = opt.to_sym fu_opts[opt] = opts[opt] if opts[opt] end fu_opts_with_preserve = fu_opts.clone fu_opts_with_preserve[:preserve] = \ if opts[:preserve] == :try fsim = File::Stat.instance_methods (fsim.include?("uid") and fsim.include?("gid") and fsim.include?("mode") and fsim.include?("atime")) else opts[:preserve] end changed = [] sources_a = [sources].flatten sources_a.each do |parent| Find.find(parent) do |child| source_fn = File.directory?(child) ? child+"/" : child target_dir = File.directory?(target) target_fn = peer_for(source_fn, target) log.debug(PNOTE+"comparing %s => %s" % [source_fn, target_fn]) source_st = File.stat(source_fn) is_copy = false begin begin target_st = File.stat(target_fn) unless target_dir # Is the file obviously different? if source_st.file? for kind in %w(size mtime) next if kind == "mtime" and ! opts[:preserve] unless source_st.send(kind) == target_st.send(kind) log.debug(PNOTE+"%s not same %s" % [target_fn, kind]) raise EOFError.new end end unless FileUtils.identical?(source_fn, target_fn) log.debug(PNOTE+"%s not identical" % target_fn) raise EOFError.new end end # File just needs to be altered if opts[:preserve] unless source_st.mode == target_st.mode changed << child log.debug(PNOTE+"%s not same mode" % target_fn) chmod(source_st.mode, target_fn, fu_opts) end unless source_st.uid == target_st.uid and source_st.gid == target_st.gid changed << child log.debug(PNOTE+"%s not same uid/gid" % target_fn) chown(source_st.uid, source_st.gid, target_fn, fu_opts) end end end rescue EOFError changed << child is_copy = true end rescue Errno::ENOENT changed << child log.debug(PNOTE+"%s not present" % target_fn) is_copy = true end if is_copy log.info(PEXEC+"cp%s %s %s" % [opts[:recursive] ? ' -r' : '', source_fn, target_fn]) ## puts "fo %s" % fu_opts.inspect ## puts "fowp %s" % fu_opts_with_preserve.inspect FileUtils.cp_r(source_fn, target_fn, fu_opts_with_preserve) end end end result = \ if changed.empty? false else if sources_a.size == 1 changed.first else changed.uniq end end return result end |
#cp_R(*args) ⇒ Object
See ShellManager#cp_R
311 312 313 |
# File 'lib/automateit/shell_manager/portable.rb', line 311 def cp_R(*args) cp_r(*args) end |
#cp_r(sources, target, opts = {}) ⇒ Object
See ShellManager#cp_r
306 307 308 |
# File 'lib/automateit/shell_manager/portable.rb', line 306 def cp_r(sources, target, opts={}) cp(sources, target, {:recursive => true}.merge(opts)) end |
#install(source, target, mode = nil) ⇒ Object
See ShellManager#install
194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/automateit/shell_manager/portable.rb', line 194 def install(source, target, mode=nil) cp_rv = nil chmod_rv = nil log.silence(Logger::WARN) do cp_rv = cp(source, target) chmod_rv = chmod(mode, peer_for(source, target)) if mode end return false unless cp_rv or chmod_rv log.info(PEXEC+"install%s %s %s" % [mode ? ' -m 0%o' % mode : '', source, target]) return source end |
#mkdir(dirs, opts = {}, &block) ⇒ Object
See ShellManager#mkdir
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 176 177 |
# File 'lib/automateit/shell_manager/portable.rb', line 150 def mkdir(dirs, opts={}, &block) _replace_owner_with_user(opts) kind = opts[:parents] ? :mkdir_p : :mkdir missing = [dirs].flatten.select{|dir| ! File.directory?(dir)} result = false if missing.empty? and not block chperm(opts) if opts[:user] or opts[:group] or opts[:mode] return result end unless missing.empty? cmd = kind.to_s.gsub(/_/, ' -') log.info(PEXEC+"#{cmd} #{missing.join(" ")}") result = [FileUtils.send(kind, missing, _fileutils_opts)].flatten result = result.first if [dirs].flatten.size == 1 end if block if missing.size > 1 raise ArgumentError.new( "can only use a block if you mkdir a single directory") end dir = [dirs].flatten.first cd(dir) do block.call(result) end end chperm(opts) if opts[:user] or opts[:group] or opts[:mode] return missing end |
#mkdir_p(dirs, opts = {}, &block) ⇒ Object
See ShellManager#mkdir_p
180 181 182 |
# File 'lib/automateit/shell_manager/portable.rb', line 180 def mkdir_p(dirs, opts={}, &block) mkdir(dirs, {:parents => true}.merge(opts), &block) end |
#mktemp(name = nil, &block) ⇒ Object
See ShellManager#mktemp
88 89 90 |
# File 'lib/automateit/shell_manager/portable.rb', line 88 def mktemp(name=nil, &block) _mktemp_helper(:mktemp, name, &block) end |
#mktempdir(name = nil, &block) ⇒ Object
See ShellManager#mktempdir
93 94 95 |
# File 'lib/automateit/shell_manager/portable.rb', line 93 def mktempdir(name=nil, &block) _mktemp_helper(:mktempdir, name, &block) end |
#mktempdircd(name = nil, &block) ⇒ Object
See ShellManager#mktempdircd
98 99 100 |
# File 'lib/automateit/shell_manager/portable.rb', line 98 def mktempdircd(name=nil, &block) _mktemp_helper(:mktempdircd, name, &block) end |
#mv(sources, target) ⇒ Object
See ShellManager#mv
316 317 318 319 320 321 322 |
# File 'lib/automateit/shell_manager/portable.rb', line 316 def mv(sources, target) present = [sources].flatten.select{|entry| self._present?(entry)} return false if present.empty? present = present.first if present.size == 1 FileUtils.mv(present, target, _fileutils_opts) && present return present end |
#provides_mode? ⇒ Boolean
21 22 23 |
# File 'lib/automateit/shell_manager/portable.rb', line 21 def provides_mode? ! broken? end |
#provides_ownership? ⇒ Boolean
25 26 27 |
# File 'lib/automateit/shell_manager/portable.rb', line 25 def provides_ownership? ! broken? end |
#pwd ⇒ Object
See ShellManager#pwd
145 146 147 |
# File 'lib/automateit/shell_manager/portable.rb', line 145 def pwd() return FileUtils.pwd() end |
#rm(targets, opts = {}) ⇒ Object
See ShellManager#rm
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/automateit/shell_manager/portable.rb', line 325 def rm(targets, opts={}) kind = \ if opts[:recursive] and opts[:force] :rm_rf elsif opts[:recursive] :rm_r else :rm end present = [targets].flatten.select{|entry| self._present?(entry)} return false if present.empty? msg = "rm" if opts[:recursive] and opts[:force] msg << " -rf" elsif opts[:recursive] msg << " -r" elsif opts[:force] msg << " -f" end msg << " " << present.join(' ') log.info(PEXEC+msg) present = present.first if present.size == 0 FileUtils.send(kind, present, _fileutils_opts) return present end |
#rm_r(targets, opts = {}) ⇒ Object
See ShellManager#rm_r
356 357 358 |
# File 'lib/automateit/shell_manager/portable.rb', line 356 def rm_r(targets, opts={}) rm(targets, {:recursive => true}.merge(opts)) end |
#rm_rf(targets, opts = {}) ⇒ Object
See ShellManager#rm_rf
361 362 363 |
# File 'lib/automateit/shell_manager/portable.rb', line 361 def rm_rf(targets, opts={}) rm(targets, {:recursive => true, :force => true}.merge(opts)) end |
#rmdir(dirs) ⇒ Object
See ShellManager#rmdir
185 186 187 188 189 190 191 |
# File 'lib/automateit/shell_manager/portable.rb', line 185 def rmdir(dirs) present = [dirs].flatten.select{|dir| File.directory?(dir)} return false if present.empty? log.info(PEXEC+"rmdir #{String === dirs ? dirs : dirs.join(' ')}") FileUtils.rmdir(present, _fileutils_opts) return present end |
#sh(*commands) ⇒ Object
See ShellManager#sh
72 73 74 75 76 |
# File 'lib/automateit/shell_manager/portable.rb', line 72 def sh(*commands) args, opts = args_and_opts(*commands) log.info(PEXEC+"#{args.join(' ')}") return writing? ? system(*args) : true end |
#suitability(method, *args) ⇒ Object
:nodoc:
12 13 14 |
# File 'lib/automateit/shell_manager/portable.rb', line 12 def suitability(method, *args) # :nodoc: return available? ? 1 : 0 end |
#touch(targets, opts = {}) ⇒ Object
See ShellManager#touch
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/automateit/shell_manager/portable.rb', line 473 def touch(targets, opts={}) like = opts.delete(:like) stamp = opts.delete(:stamp) quiet = opts.delete(:quiet) == true ? true : false time = \ if stamp stamp elsif like begin File.stat(like).mtime rescue Errno::ENOENT => e if preview? Time.now else raise e end end else Time.now end unless quiet msg = "touch" msg << " --reference %s" % like if like msg << " --stamp %s" % stamp if stamp msg << " " << [targets].flatten.join(" ") log.info(PEXEC+msg) end results = [] for target in [targets].flatten begin stat = File.stat(target) next if stat.mtime.to_i == time.to_i rescue Errno::ENOENT File.open(target, "a"){} unless preview? end File.utime(time, time, target) unless preview? results << target end return false if results.empty? return targets.is_a?(String) ? results.first : results end |
#umask(mode = nil, &block) ⇒ Object
See ShellManager#umask
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/automateit/shell_manager/portable.rb', line 103 def umask(mode=nil, &block) if mode old = File::umask File::umask(mode) if block begin block.call rescue Exception => e raise e ensure File::umask(old) end end else File::umask end end |