Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/linux_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
require 'linux_admin/common'
require 'linux_admin/exceptions'
require 'linux_admin/command_result'
require 'linux_admin/package'
require 'linux_admin/rpm'
require 'linux_admin/deb'
require 'linux_admin/version'
require 'linux_admin/yum'

Expand Down
39 changes: 39 additions & 0 deletions lib/linux_admin/deb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# LinuxAdmin Deb Representation
#
# Copyright (C) 2013 Red Hat Inc.
# Licensed under the MIT License

class LinuxAdmin
class Deb < Package
APT_CACHE_CMD = '/usr/bin/apt-cache'

def self.from_line(apt_cache_line, in_description=false)
tag,value = apt_cache_line.split(':')
tag = tag.strip.downcase
[tag, value]
end

def self.from_string(apt_cache_string)
in_description = false
apt_cache_string.split("\n").each.with_object({}) do |line,deb|
tag,value = self.from_line(line)
if tag == 'description-en'
in_description = true
elsif tag == 'homepage'
in_description = false
end

if in_description && tag != 'description-en'
deb['description-en'] << line
else
deb[tag] = value.strip
end
end
end

def self.info(pkg)
self.from_string(run!(APT_CACHE_CMD, :params => ["show", pkg]).output)
end

end
end
18 changes: 18 additions & 0 deletions lib/linux_admin/package.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# LinuxAdmin Abstract Package Representation
#
# Copyright (C) 2013 Red Hat Inc.
# Licensed under the MIT License

class LinuxAdmin
class Package < LinuxAdmin
def self.info(pkg)
if Distro.local == Distros.redhat
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe @Fryguy has a suggestion on this one. Maybe Package should delegate the messages to a a singleton of a package manager based on the distro.

Perhaps get_info should be query... Rpm.query(pkg) seems more clear to me.

Finally, should it be PackageManager vs. Package, seems like we get package information from the package manager?
Also, what is the right package manager classes where we have rpm and deb classes here? It seems to me that deb is a package formats and not the tool we use to query for package information, so I would suggest Rpm and Maybe Apt as PackageManager classes. What are you thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so my thoughts are.

  1. Whenever I see a get_* or set_* method in Ruby, I immediately get red flags. There is almost always a better name. .query is ok, but I'd be fine with .info. Need to think about this more.
  2. Depending on what this class' purpose is, would give the names. If it is a class to deal with raw package files, then the base class should Package, with Rpm and Deb as subclasses. If it is a class to deal with package managers, then the base class should be PackageManager, with Yum and Apt as subclasses. Either way, if we take advantage of the subclassing, then there is not need for this delegator method, since each subclass will just define the method in question.

return Rpm.info(pkg)
elsif Distro.local == Distros.ubuntu
return Deb.info(pkg)
end

nil
end
end
end
23 changes: 22 additions & 1 deletion lib/linux_admin/rpm.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class LinuxAdmin
class Rpm < LinuxAdmin
class Rpm < Package
RPM_CMD = '/usr/bin/rpm'

def self.list_installed
out = run!("rpm -qa --qf \"%{NAME} %{VERSION}-%{RELEASE}\n\"").output
out.split("\n").each_with_object({}) do |line, pkg_hash|
Expand All @@ -8,6 +10,25 @@ def self.list_installed
end
end

def self.info(pkg)
params = { "-qi" => pkg}
in_description = false
out = run!(RPM_CMD, :params => params).output
out.split("\n").each.with_object({}) do |line, rpm|
tag,value = line.split(':')
tag = tag.strip
if tag == 'Description'
in_description = true
elsif in_description
rpm['description'] ||= ""
rpm['description'] << line + " "
else
tag = tag.downcase.gsub(/\s/, '_')
rpm[tag] = value.strip
end
end
end

def self.upgrade(pkg)
cmd = "rpm -U"
params = { nil => pkg }
Expand Down
54 changes: 54 additions & 0 deletions spec/deb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'spec_helper'

describe LinuxAdmin::Deb do
describe "#info" do
it "returns package metadata" do
# as output w/ apt-cache show ruby on ubuntu 13.04
data = <<EOS
Package: ruby
Priority: optional
Section: interpreters
Installed-Size: 31
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: akira yamada <akira@debian.org>
Architecture: all
Source: ruby-defaults
Version: 4.9
Replaces: irb, rdoc
Provides: irb, rdoc
Depends: ruby1.9.1 (>= 1.9.3.194-1)
Suggests: ri, ruby-dev
Conflicts: irb, rdoc
Filename: pool/main/r/ruby-defaults/ruby_4.9_all.deb
Size: 4896
MD5sum: b1991f2e0eafb04f5930ed242cfe1476
SHA1: a7c55fbb83dd8382631ea771b5555d989351f840
SHA256: 84d042e0273bd2f0082dd9e7dda0246267791fd09607041a35485bfff92f38d9
Description-en: Interpreter of object-oriented scripting language Ruby (default version)
Ruby is the interpreted scripting language for quick and easy
object-oriented programming. It has many features to process text
files and to do system management tasks (as in perl). It is simple,
straight-forward, and extensible.
.
This package is a dependency package, which depends on Debian's default Ruby
version (currently v1.9.3).
Homepage: http://www.ruby-lang.org/
Description-md5: da2991b37e3991230d79ba70f9c01682
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Origin: Ubuntu
Supported: 9m
Task: kubuntu-desktop, kubuntu-full, kubuntu-active, kubuntu-active-desktop, kubuntu-active-full, kubuntu-active, edubuntu-desktop-gnome, ubuntustudio-font-meta
EOS
described_class.should_receive(:run).
with(described_class::APT_CACHE_CMD, :params => ["show", "ruby"]).
and_return(CommandResult.new(data, "", 0))
metadata = described_class.info("ruby")
metadata['package'].should == 'ruby'
metadata['priority'].should == 'optional'
metadata['section'].should == 'interpreters'
metadata['architecture'].should == 'all'
metadata['version'].should == '4.9'
metadata['origin'].should == 'Ubuntu'
end
end
end
17 changes: 17 additions & 0 deletions spec/package_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'

describe LinuxAdmin::Package do
describe "#info" do
it "dispatches to redhat lookup mechanism" do
LinuxAdmin::Distro.should_receive(:local).and_return(LinuxAdmin::Distros.redhat)
LinuxAdmin::Rpm.should_receive(:info).with('ruby')
described_class.info 'ruby'
end

it "dispatches to ubuntu lookup mechanism" do
LinuxAdmin::Distro.should_receive(:local).twice.and_return(LinuxAdmin::Distros.ubuntu)
LinuxAdmin::Deb.should_receive(:info).with('ruby')
described_class.info 'ruby'
end
end
end
48 changes: 47 additions & 1 deletion spec/rpm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,54 @@
})
end

describe "#info" do
it "returns package metadata" do
# as output w/ rpm -qi ruby on F19
data = <<EOS
Name : ruby
Version : 2.0.0.247
Release : 15.fc19
Architecture: x86_64
Install Date: Sat 19 Oct 2013 08:17:20 PM EDT
Group : Development/Languages
Size : 64473
License : (Ruby or BSD) and Public Domain
Signature : RSA/SHA256, Thu 01 Aug 2013 02:07:03 PM EDT, Key ID 07477e65fb4b18e6
Source RPM : ruby-2.0.0.247-15.fc19.src.rpm
Build Date : Wed 31 Jul 2013 08:26:49 AM EDT
Build Host : buildvm-16.phx2.fedoraproject.org
Relocations : (not relocatable)
Packager : Fedora Project
Vendor : Fedora Project
URL : http://ruby-lang.org/
Summary : An interpreter of object-oriented scripting language
Description :
Ruby is the interpreted scripting language for quick and easy
object-oriented programming. It has many features to process text
files and to do system management tasks (as in Perl). It is simple,
straight-forward, and extensible.
EOS
described_class.should_receive(:run).
with(described_class::RPM_CMD, :params => {"-qi" => "ruby"}).
and_return(CommandResult.new(data, "", 0))
metadata = described_class.info("ruby")
metadata['name'].should == 'ruby'
metadata['version'].should == '2.0.0.247'
metadata['release'].should == '15.fc19'
metadata['architecture'].should == 'x86_64'
metadata['group'].should == 'Development/Languages'
metadata['size'].should == '64473'
metadata['license'].should == '(Ruby or BSD) and Public Domain'
metadata['source_rpm'].should == 'ruby-2.0.0.247-15.fc19.src.rpm'
metadata['build_host'].should == 'buildvm-16.phx2.fedoraproject.org'
metadata['packager'].should == 'Fedora Project'
metadata['vendor'].should == 'Fedora Project'
metadata['summary'].should == 'An interpreter of object-oriented scripting language'
end
end

it ".upgrade" do
described_class.should_receive(:run).with("rpm -U", {:params=>{nil=>"abc"}}).and_return(CommandResult.new("", "", 0))
expect(described_class.upgrade("abc")).to be_true
end
end
end