Class: Babushka::PkgHelper

Inherits:
Object show all
Extended by:
LogHelpers, ShellHelpers
Defined in:
lib/babushka/pkg_helper.rb

Class Method Summary collapse

Methods included from ShellHelpers

cmd_dir, current_username, log_shell, login_shell, raw_shell, shell, shell!, shell?, shell_cmd, sudo, which

Methods included from LogHelpers

debug, deprecated!, log, log_block, log_error, log_ok, log_stderr, log_warn, removed!

Class Method Details

.all_manager_keysObject



13
14
15
# File 'lib/babushka/pkg_helper.rb', line 13

def all_manager_keys
  [:apt, :pacman, :brew, :macports, :yum, :binpkgsrc, :binports]
end

.bin_pathObject



48
49
50
# File 'lib/babushka/pkg_helper.rb', line 48

def bin_path
  prefix / 'bin'
end

.cmd_in_path?(cmd_name) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
# File 'lib/babushka/pkg_helper.rb', line 52

def cmd_in_path? cmd_name
  if (_cmd_dir = cmd_dir(cmd_name)).nil?
    log_error "The '#{cmd_name}' command is not available. You probably need to add #{bin_path} to your PATH."
  else
    _cmd_dir.starts_with?(prefix)
  end
end

.handle_install!(pkgs, opts = nil) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/babushka/pkg_helper.rb', line 35

def handle_install! pkgs, opts = nil
  if [*pkgs].empty?
    log "Nothing to install on #{manager_key}-based systems."
  else
    update_pkg_lists_if_required
    install! pkgs, opts
  end
end

.has?(pkg, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
# File 'lib/babushka/pkg_helper.rb', line 21

def has? pkg, opts = {}
  pkg = Babushka.VersionOf(pkg)
  has_pkg?(pkg).tap {|matching_version|
    matching_pkg = Babushka.VersionOf(pkg.name, (matching_version if matching_version.is_a?(VersionStr)))
    unless opts[:log] == false
      log "system #{matching_version ? "has" : "doesn't have"} #{matching_pkg} #{pkg_type}", :as => (:ok if matching_version)
    end
  }
end

.install!(pkgs, opts = nil) ⇒ Object



31
32
33
# File 'lib/babushka/pkg_helper.rb', line 31

def install! pkgs, opts = nil
  install_pkgs! [*pkgs].map {|pkg| Babushka.VersionOf(pkg) }, opts
end

.manager_depObject



9
10
11
# File 'lib/babushka/pkg_helper.rb', line 9

def manager_dep
  'package manager'.with(pkg_binary)
end

.pkg_binaryObject



7
# File 'lib/babushka/pkg_helper.rb', line 7

def pkg_binary; pkg_cmd end

.prefixObject



44
45
46
# File 'lib/babushka/pkg_helper.rb', line 44

def prefix
  cmd_dir(pkg_binary).p.dir
end

.present?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/babushka/pkg_helper.rb', line 17

def present?
  which pkg_binary
end

.should_sudo?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/babushka/pkg_helper.rb', line 60

def should_sudo?
  !File.writable?(bin_path)
end

.update_pkg_lists(message = "Updating #{manager_dep.capitalize} package lists") ⇒ Object



78
79
80
# File 'lib/babushka/pkg_helper.rb', line 78

def update_pkg_lists message = "Updating #{manager_dep.capitalize} package lists"
  log_shell message, pkg_update_command, :sudo => should_sudo?
end

.update_pkg_lists_if_requiredObject



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/babushka/pkg_helper.rb', line 64

def update_pkg_lists_if_required
  if pkg_update_timeout.nil?
    true # not required
  else
    list_age = Time.now - pkg_list_dir.mtime
    if list_age > pkg_update_timeout
      update_pkg_lists "#{manager_dep.capitalize} package lists are #{list_age.round.xsecs} old. Updating"
    else
      debug "#{manager_dep.capitalize} package lists are #{list_age.round.xsecs} old (up to date)."
      true # up to date
    end
  end
end