package_manager 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,4 +17,4 @@ if ARGV.size < 1
17
17
  end
18
18
 
19
19
  pkg = PackageManager::Base.load
20
- puts pkg.find_available( ARGV )
20
+ puts pkg.find_available( ARGV[ 0 ] )
@@ -17,4 +17,4 @@ if ARGV.size < 1
17
17
  end
18
18
 
19
19
  pkg = PackageManager::Base.load
20
- puts pkg.find_installed( ARGV )
20
+ puts pkg.find_installed( ARGV[ 0 ] )
data/bin/pkg-install CHANGED
@@ -17,4 +17,4 @@ if ARGV.size < 1
17
17
  end
18
18
 
19
19
  pkg = PackageManager::Base.load
20
- puts pkg.install( ARGV )
20
+ puts pkg.install( ARGV[ 0 ] )
data/bin/pkg-install-file CHANGED
@@ -10,5 +10,11 @@ pkg-install-file: Install a package from a file
10
10
  Usage: #{ $0 } [options]
11
11
  EOT
12
12
 
13
+ if ARGV.size < 1
14
+ $stderr.puts 'No file name supplied' if ARGV.size < 1
15
+ $stderr.puts usage
16
+ exit 1
17
+ end
18
+
13
19
  pkg = PackageManager::Base.load
14
- puts pkg.install_file( ARGV )
20
+ puts pkg.install_file( ARGV[ 0 ] )
@@ -17,4 +17,4 @@ if ARGV.size < 1
17
17
  end
18
18
 
19
19
  pkg = PackageManager::Base.load
20
- puts pkg.list_contents( ARGV )
20
+ puts pkg.list_contents( ARGV[ 0 ] )
data/bin/pkg-provides ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ROOT_PATH = File.dirname( File.dirname( File.expand_path( __FILE__ ) ) )
4
+ LIB_PATH = File.join( ROOT_PATH, 'lib' )
5
+ $:.unshift( LIB_PATH )
6
+ require 'package_manager'
7
+
8
+ usage = <<EOT
9
+ pkg-provides: Find the package a file comes from
10
+ Usage: #{ $0 } [options]
11
+ EOT
12
+
13
+ if ARGV.size < 1
14
+ $stderr.puts 'No file name supplied' if ARGV.size < 1
15
+ $stderr.puts usage
16
+ exit 1
17
+ end
18
+
19
+ pkg = PackageManager::Base.load
20
+ puts pkg.provides( ARGV )
data/bin/pkg-uninstall ADDED
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ROOT_PATH = File.dirname( File.dirname( File.expand_path( __FILE__ ) ) )
4
+ LIB_PATH = File.join( ROOT_PATH, 'lib' )
5
+ $:.unshift( LIB_PATH )
6
+ require 'package_manager'
7
+
8
+ usage = <<EOT
9
+ pkg-uninstall: Uninstall the named package
10
+ Usage: #{ $0 } [options]
11
+ EOT
12
+
13
+ if ARGV.size < 1
14
+ $stderr.puts 'No package name supplied'
15
+ $stderr.puts usage
16
+ exit 1
17
+ end
18
+
19
+ pkg = PackageManager::Base.load
20
+ puts pkg.uninstall( ARGV[ 0 ] )
@@ -1,13 +1,12 @@
1
1
  require 'rubygems' if RUBY_VERSION < '1.9'
2
2
  require 'package_manager/base'
3
- require 'package_manager/port'
4
3
 
5
4
  module PackageManager
6
5
 
7
6
  module VERSION #:nodoc:
8
7
  MAJOR = 0
9
8
  MINOR = 0
10
- TINY = 2
9
+ TINY = 3
11
10
 
12
11
  STRING = [ MAJOR, MINOR, TINY ].join('.')
13
12
  end
@@ -1,3 +1,5 @@
1
+ require 'package_manager/dpkg'
2
+
1
3
  module PackageManager
2
4
 
3
5
  class Apt < Dpkg
@@ -20,6 +22,10 @@ module PackageManager
20
22
  'apt-get install'
21
23
  end
22
24
 
25
+ def uninstall_command
26
+ 'apt-get remove'
27
+ end
28
+
23
29
  end
24
30
 
25
31
  end
@@ -1,47 +1,129 @@
1
+ require 'rubygems'
2
+
3
+ # Pre-declare Base class, so we can load derived classes for class factory
4
+ module PackageManager
5
+ class Base; end
6
+ end
7
+
8
+ require 'package_manager/apt'
9
+ require 'package_manager/dpkg'
10
+ require 'package_manager/fink'
11
+ require 'package_manager/port'
12
+ require 'package_manager/rpm'
13
+ require 'package_manager/yum'
14
+
1
15
  module PackageManager
2
16
 
3
17
  class Base
4
18
 
5
19
  attr_reader :name
6
20
 
7
- def self.load
8
- PackageManager::Port.new
21
+ KNOWN_PACKAGE_MANAGERS = { 'apt' => Apt,
22
+ 'dpkg' => Dpkg,
23
+ 'fink' => Fink,
24
+ 'port' => Port,
25
+ 'rpm' => Rpm,
26
+ 'yum' => Yum }
27
+
28
+ def self.load( package_manager = guess_package_manager )
29
+ raise 'Unknown package manager' if KNOWN_PACKAGE_MANAGERS[ package_manager ].nil?
30
+ KNOWN_PACKAGE_MANAGERS[ package_manager ].new
31
+ end
32
+
33
+ # Returns an array of matches
34
+ def find_available( package_match )
35
+ run_command( find_available_command, package_match ).split("\n")
36
+ end
37
+
38
+ # Returns an array of matches
39
+ def find_installed( package_match )
40
+ run_command( find_installed_command, package_match ).split("\n").uniq
9
41
  end
10
42
 
11
- def find_available( argv )
12
- raise "You should supply a search pattern" if argv.size < 1
13
- command = find_available_command
14
- run_command( command, argv[ 0 ] )
43
+ # Returns an array of file names
44
+ def list_contents( package )
45
+ run_command( list_contents_command, package ).split("\n")
15
46
  end
16
47
 
17
- def find_installed( argv )
18
- raise "You should supply a search pattern" if argv.size < 1
19
- command = find_installed_command
20
- run_command( command, argv[ 0 ] )
48
+ # Returns true if the package installed successfully
49
+ def install( package )
50
+ run_command( install_command, package )
51
+ $?.exitstatus == 0
21
52
  end
22
53
 
23
- def list_contents( argv )
24
- raise "You should supply a package name" if argv.size < 1
25
- command = list_contents_command
26
- run_command( command, argv[ 0 ] )
54
+ # Returns true if the package installed successfully
55
+ def install_file( file )
56
+ raise 'The file '#{ file }' does not exist' if ! File.exist?( file )
57
+ run_command( install_file_command, file )
58
+ $?.exitstatus == 0
27
59
  end
28
60
 
29
- def install( argv )
30
- raise "You should supply a package name" if argv.size < 1
31
- command = install_command
32
- run_command( command, argv[ 0 ] )
61
+ # Returns true if the package uninstalled successfully
62
+ def uninstall( package )
63
+ run_command( uninstall_command, package )
64
+ $?.exitstatus == 0
33
65
  end
34
66
 
35
- def install_file( argv )
36
- raise "You should supply a package name" if argv.size < 1
37
- raise "The file '#{ ARGV[ 0 ] }' does not exist" if ! File.exist?( argv[ 0 ] )
38
- run_command( install_file_command, argv[ 0 ] )
67
+ # Returns the package name if found, or an empty string
68
+ def provides( file )
69
+ raise 'The file '#{ file }' does not exist' if ! File.exist?( file )
70
+ run_command( provides_command, file )
39
71
  end
40
72
 
41
73
  private
42
74
 
43
75
  def run_command( command, *args )
44
- `#{ command } #{ args.join( ' ' ) }`
76
+ `#{ command } #{ args.join( ' ' ) }`.chomp
77
+ end
78
+
79
+ def self.guess_package_manager
80
+ case operating_system
81
+ when 'linux'
82
+ linux_package_manager
83
+ when 'darwin'
84
+ darwin_package_manager
85
+ else
86
+ raise 'Unhandled operating system'
87
+ end
88
+ end
89
+
90
+ def self.operating_system
91
+ platforms = Gem.platforms
92
+ raise 'Unexpected response from Gem.platforms' unless platforms.respond_to?( :size )
93
+ raise 'Expected Array of 2 values' unless platforms.size == 2
94
+ raise 'Unexpected value returned by Gem.platforms' unless platforms[ 1 ].respond_to?( :os )
95
+ platforms[ 1 ].os
96
+ end
97
+
98
+ def self.linux_package_manager
99
+ case
100
+ when program_installed?( 'apt-get' )
101
+ 'apt'
102
+ when program_installed?( 'yum' )
103
+ 'yum'
104
+ when program_installed?( 'rpm' )
105
+ 'rpm'
106
+ else
107
+ raise "Unknown or missing package manager system"
108
+ end
109
+ end
110
+
111
+ # TODO: Add brew
112
+ def self.darwin_package_manager
113
+ # Be opinionated and prefer Mac Ports if both it and Fink are installed
114
+ case
115
+ when program_installed?( 'port' )
116
+ 'port'
117
+ when program_installed?( 'apt-get' )
118
+ 'fink'
119
+ else
120
+ raise "Unknown or missing package manager system"
121
+ end
122
+ end
123
+
124
+ def self.program_installed?( program )
125
+ `which '#{ program }'`
126
+ $?.exitstatus == 0
45
127
  end
46
128
 
47
129
  end
@@ -16,6 +16,10 @@ module PackageManager
16
16
  'dpkg -i'
17
17
  end
18
18
 
19
+ def provides_command
20
+ 'dpkg-query -S'
21
+ end
22
+
19
23
  end
20
24
 
21
25
  end
@@ -20,6 +20,10 @@ module PackageManager
20
20
  'rpm -ihv'
21
21
  end
22
22
 
23
+ def provides_command
24
+ 'rpm -qf'
25
+ end
26
+
23
27
  end
24
28
 
25
29
  end
@@ -12,6 +12,10 @@ module PackageManager
12
12
  'yum install'
13
13
  end
14
14
 
15
+ def uninstall_command
16
+ 'yum remove'
17
+ end
18
+
15
19
  end
16
20
 
17
21
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: package_manager
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joe Yates
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-08 00:00:00 +01:00
18
+ date: 2010-09-10 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -42,6 +42,8 @@ executables:
42
42
  - pkg-install
43
43
  - pkg-install-file
44
44
  - pkg-list-contents
45
+ - pkg-provides
46
+ - pkg-uninstall
45
47
  extensions: []
46
48
 
47
49
  extra_rdoc_files:
@@ -51,6 +53,8 @@ extra_rdoc_files:
51
53
  - bin/pkg-install
52
54
  - bin/pkg-install-file
53
55
  - bin/pkg-list-contents
56
+ - bin/pkg-provides
57
+ - bin/pkg-uninstall
54
58
  - lib/package_manager/apt.rb
55
59
  - lib/package_manager/base.rb
56
60
  - lib/package_manager/dpkg.rb
@@ -67,6 +71,8 @@ files:
67
71
  - bin/pkg-install
68
72
  - bin/pkg-install-file
69
73
  - bin/pkg-list-contents
74
+ - bin/pkg-provides
75
+ - bin/pkg-uninstall
70
76
  - lib/package_manager/apt.rb
71
77
  - lib/package_manager/base.rb
72
78
  - lib/package_manager/dpkg.rb