Class: Tpkg::OS::MacOSX

Inherits:
Tpkg::OS show all
Defined in:
lib/tpkg/os/macosx.rb

Overview

tpkg package management system License: MIT (www.opensource.org/licenses/mit-license.php)

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tpkg::OS

#arch, create, #cron_dot_d_directory, #fqdn, #init_links, #os, #os_name, register_implementation, #remove_native_stub_pkg, #stub_native_pkg, #sudo_default?, #sys_v_init_links

Constructor Details

#initialize(options = {}) ⇒ MacOSX

Returns a new instance of MacOSX.



11
12
13
14
# File 'lib/tpkg/os/macosx.rb', line 11

def initialize(options={})
  @portcmd = options[:portcmd] || options[:testcmd] || '/opt/local/bin/port'
  super
end

Class Method Details

.supported?Boolean

Returns:

  • (Boolean)


5
6
7
8
# File 'lib/tpkg/os/macosx.rb', line 5

def self.supported?
  Facter.loadfacts
  Facter['operatingsystem'].value == 'Darwin'
end

Instance Method Details

#available_native_packages(pkgname) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
# File 'lib/tpkg/os/macosx.rb', line 16

def available_native_packages(pkgname)
  native_packages = []
  if File.exist?(@portcmd)
    # Ports can also have an "epoch" number, but that appears to be
    # invisible from the command line
    # http://guide.macports.org/#reference.keywords
    cmd = "#{@portcmd} installed #{pkgname}"
    puts "available_native_packages running '#{cmd}'" if @debug
    IO.popen(cmd) do |pipe|
      pipe.each_line do |line|
        next if line =~ /The following ports are currently installed/
        next if line =~ /None of the specified ports are installed/
        next if line !~ /\(active\)/
        name, portversion = line.split(' ')
        portversion.sub!(/^@/, '')
        # Remove variant names
        portversion.sub!(/\+.*/, '')
        version, package_version = portversion.split('_', 2)
        pkg = Tpkg.pkg_for_native_package(name, version, package_version, :native_installed)
        native_packages << pkg
      end
    end
    if !$?.success?
      raise "available_native_packages error running port"
    end
    cmd = "#{@portcmd} list #{pkgname}"
    puts "available_native_packages running '#{cmd}'" if @debug
    IO.popen(cmd) do |pipe|
      pipe.each_line do |line|
        name, version = line.split(' ')
        version.sub!(/^@/, '')
        package_version = nil
        pkg = Tpkg.pkg_for_native_package(name, version, package_version, :native_available)
        native_packages << pkg
      end
    end
    if !$?.success?
      raise "available_native_packages error running port"
    end
  else
    # Fink, Homebrew support would be nice
    raise "No supported native package tool available on #{os}"
  end
  native_packages
end

#install_native_package(pkg) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tpkg/os/macosx.rb', line 78

def install_native_package(pkg)
  pkgname = native_pkg_to_install_string(pkg)
  if File.exist?(@portcmd)
    cmd = "#{@portcmd} install #{pkgname}"
    puts "Running '#{cmd}' to install native package" if @debug
    system(cmd)
  else
    # Fink, Homebrew support would be nice
    raise "No supported native package tool available on #{os}"
  end
end

#native_pkg_to_install_string(pkg) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tpkg/os/macosx.rb', line 61

def native_pkg_to_install_string(pkg)
  pkgname = nil
  if File.exist?(@portcmd)
    pkgname = pkg[:metadata][:name]
    # MacPorts doesn't support installing a specific version (AFAIK)
    if pkg[:metadata][:version]
      warn "Ignoring version with MacPorts"
    end
    if pkg[:metadata][:package_version]
      warn "Ignoring package version with MacPorts"
    end
  else
    # Fink, Homebrew support would be nice
    raise "No supported native package tool available on #{os}"
  end
  pkgname
end

#os_versionObject



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/tpkg/os/macosx.rb', line 101

def os_version
  if !@os_version
    if Facter['macosx_productversion'] &&
       Facter['macosx_productversion'].value &&
       !Facter['macosx_productversion'].value.empty?
      macver = Facter['macosx_productversion'].value
      # Extract 10.5 from 10.5.6, for example
      @os_version = macver.split('.')[0,2].join('.')
    end
  end
  super
end

#upgrade_native_package(pkg) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tpkg/os/macosx.rb', line 89

def upgrade_native_package(pkg)
  pkgname = native_pkg_to_install_string(pkg)
  if File.exist?(@portcmd)
    cmd = "#{@portcmd} upgrade #{pkgname}"
    puts "Running '#{cmd}' to upgrade native package" if @debug
    system(cmd)
  else
    # Fink, Homebrew support would be nice
    raise "No supported native package tool available on #{os}"
  end
end