~awstools-dev/ubuntu/maverick/ec2-ami-tools/maverick

« back to all changes in this revision

Viewing changes to lib/ec2/platform/linux/tar.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2008-10-14 08:35:25 UTC
  • Revision ID: james.westby@ubuntu.com-20081014083525-c0n69wr7r7aqfb8w
Tags: 1.3-26357-0ubuntu2
* New upstream version.
* Update the debian copyright file.
* Added quilt patch system to make it easier to maintain. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2008 Amazon.com, Inc. or its affiliates.  All Rights
 
2
# Reserved.  Licensed under the Amazon Software License (the
 
3
# "License").  You may not use this file except in compliance with the
 
4
# License. A copy of the License is located at
 
5
# http://aws.amazon.com/asl or in the "license" file accompanying this
 
6
# file.  This file is distributed on an "AS IS" BASIS, WITHOUT
 
7
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
 
8
# the License for the specific language governing permissions and
 
9
# limitations under the License.
 
10
 
 
11
require 'ec2/platform/linux/constants'
 
12
module EC2
 
13
  module Platform
 
14
    module Linux
 
15
      class Tar
 
16
        class Command
 
17
          EXECUTABLE=EC2::Platform::Linux::Constants::Utility::TAR
 
18
          def initialize(e = EXECUTABLE)
 
19
            @files   = []
 
20
            @options = []
 
21
            @executable = e
 
22
          end
 
23
          
 
24
          def version;        @options << '--version';   self; end
 
25
          def verbose;        @options << '-v';          self; end
 
26
          def create;         @options << '-c';          self; end
 
27
          def bzip2;          @options << '-j';          self; end
 
28
          def diff;           @options << '-d';          self; end
 
29
          def gzip;           @options << '-z';          self; end
 
30
          def extract;        @options << '-x';          self; end
 
31
          def update;         @options << '-u';          self; end
 
32
          def sparse;         @options << '-S';          self; end
 
33
          def dereference;    @options << '-h';          self; end
 
34
          
 
35
          def archive(filename)
 
36
            filename = '-' if filename.nil? 
 
37
            @options << "-f #{filename}"
 
38
            self
 
39
          end
 
40
          
 
41
          def chdir(dir)
 
42
            @options << "-C #{dir}" unless dir.nil?
 
43
            self
 
44
          end
 
45
          
 
46
          def add(filename, dir = nil)
 
47
            item = dir.nil? ? filename : "-C #{dir} #{filename}"
 
48
            @files << item
 
49
            self
 
50
          end
 
51
          def expand
 
52
            "#{@executable} #{@options.join(' ')} #{@files.join(' ')}".strip            
 
53
          end
 
54
        end
 
55
        class Version
 
56
          RECOMMENDED  = 'tar 1.15'
 
57
          REGEX = /(?:tar).*?(\d+)\.(\d+)\.?(\d*)/
 
58
          attr_reader :values
 
59
          attr_reader :string
 
60
          
 
61
          def initialize(str=nil)
 
62
            @string = str
 
63
            @string = default if str.nil? or str.empty?
 
64
            @values = Version.parse @string
 
65
          end
 
66
          def default
 
67
            s = `#{Command.new.version.expand}`.strip
 
68
            s = nil unless $? == 0
 
69
            s
 
70
          end
 
71
          def string= (str)
 
72
            @string = str
 
73
            @values = Version.parse @string
 
74
          end
 
75
          def >= (other)
 
76
            return nil if @values.nil?
 
77
            if other.nil? or not other.is_a? Version
 
78
              raise ArgumentError, "Cannot compare with invalid version #{other}"
 
79
            end
 
80
            @values.zip(other.values).each do |mine, others|
 
81
              return false if mine < others
 
82
              return true if mine > others
 
83
            end
 
84
            return true
 
85
          end          
 
86
          def usable?
 
87
             self >= Version.new(Version::RECOMMENDED)
 
88
          end
 
89
          def self.parse(str)
 
90
            match = REGEX.match(str)
 
91
            return nil if match.nil?
 
92
            begin
 
93
              items = match.captures.collect do |cap|
 
94
                cap.sub!(/^0*/, "")
 
95
                case cap
 
96
                when ""
 
97
                  num = 0
 
98
                else
 
99
                  num = Integer(cap)
 
100
                end
 
101
              end
 
102
            rescue ArgumentError
 
103
              return nil
 
104
            end
 
105
            items
 
106
          end          
 
107
          def self.current
 
108
            Version.new
 
109
          end
 
110
        end
 
111
      end
 
112
    end
 
113
  end
 
114
end