Class: Slackware::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/slackware/package.rb

Direct Known Subclasses

PackageBundle, Script

Constant Summary collapse

RE_FILE_LIST =
/^FILE LIST:/
RE_COMPRESSED_PACKAGE_SIZE =
/^COMPRESSED PACKAGE SIZE:\s+(.*)$/
RE_UNCOMPRESSED_PACKAGE_SIZE =
/^UNCOMPRESSED PACKAGE SIZE:\s+(.*)$/
RE_PACKAGE_LOCATION =
/^PACKAGE LOCATION:\s+(.*)$/
RE_PACKAGE_DESCRIPTION =
/^PACKAGE DESCRIPTION:\s+(.*)$/
FMT_UPGRADE_TIME =
"%F %H:%M:%S"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ Package

attr_accessor :time, :owned_files



42
43
44
# File 'lib/slackware/package.rb', line 42

def initialize(name = nil)
  self.name = name
end

Instance Attribute Details

#archObject

Returns the value of attribute arch.



40
41
42
# File 'lib/slackware/package.rb', line 40

def arch
  @arch
end

#buildObject

Returns the value of attribute build.



40
41
42
# File 'lib/slackware/package.rb', line 40

def build
  @build
end

#fileObject

Returns the value of attribute file.



40
41
42
# File 'lib/slackware/package.rb', line 40

def file
  @file
end

#nameObject

Returns the value of attribute name.



40
41
42
# File 'lib/slackware/package.rb', line 40

def name
  @name
end

#pathObject

Fill in the path information



214
215
216
# File 'lib/slackware/package.rb', line 214

def path
  @path
end

#tagObject

Returns the value of attribute tag.



40
41
42
# File 'lib/slackware/package.rb', line 40

def tag
  @tag
end

#tag_sepObject

Returns the value of attribute tag_sep.



40
41
42
# File 'lib/slackware/package.rb', line 40

def tag_sep
  @tag_sep
end

#upgrade_timeObject

Returns the value of attribute upgrade_time.



40
41
42
# File 'lib/slackware/package.rb', line 40

def upgrade_time
  @upgrade_time
end

#versionObject

Returns the value of attribute version.



40
41
42
# File 'lib/slackware/package.rb', line 40

def version
  @version
end

Class Method Details

.parse(name) ⇒ Object

Package.parse class method



75
76
77
78
79
# File 'lib/slackware/package.rb', line 75

def self::parse(name)
  p = self.new()
  p.parse(name)
  return p
end

Instance Method Details

#_owned_filesObject

Accessor for the FILE LIST from the package file unless the :owned_files symbol is populated



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/slackware/package.rb', line 171

def _owned_files
  files = []
  File.open(self.path + '/' + self.fullname) do |f|
    loop do
      break if f.eof?
      line = f.readline()
    begin
        if line.force_encoding("US-ASCII") =~ RE_FILE_LIST
          f.seek(2, IO::SEEK_CUR)
          break
        end
      rescue ArgumentError
        # ArgumentError: invalid byte sequence in US-ASCII
        # so dumb, i wish i could determine a better solution for this
        true
      end
    end
    begin
      files = f.readlines().map {|line| line.rstrip.force_encoding("US-ASCII") }
    rescue ArgumentError
      Log.instance.debug("Slackware::Package") {
        "encoding in : " + self.path + '/' + self.fullname
      }
    end
  end
  return files
end

#compressed_sizeObject

Accessor for the COMPRESSED PACKAGE SIZE from the package file



148
149
150
151
152
153
154
155
156
157
# File 'lib/slackware/package.rb', line 148

def compressed_size
  return @compressed_size unless @compressed_size.nil?

  f = File.open(self.path + '/' + self.fullname)
  loop do
    if (f.readline =~ RE_COMPRESSED_PACKAGE_SIZE)
      return @compressed_size = $1
    end
  end
end

#compressed_size=(size) ⇒ Object

Setter for the COMPRESSED PACKAGE SIZE, in the event you are parsing a repo file



160
161
162
# File 'lib/slackware/package.rb', line 160

def compressed_size=(size)
  @compressed_size = size
end

#fullnameObject

Reassemble the package name as it would be in file form



82
83
84
85
86
87
88
89
# File 'lib/slackware/package.rb', line 82

def fullname
  if (self.upgrade_time)
    time = self.upgrade_time.strftime("%F,%H:%M:%S")
    return [self.name, self.version, self.arch, [self.build, self.tag].join(self.tag_sep), "upgraded", time].join("-")
  else
    return [self.name, self.version, self.arch, [self.build, self.tag].join(self.tag_sep)].join("-")
  end
end

#inspectObject



235
236
237
238
239
240
241
242
243
244
245
# File 'lib/slackware/package.rb', line 235

def inspect
  "#<%s:0x%x name=\"%s\" version=\"%s\" arch=\"%s\" build=%s tag=\"%s\">" % [
    self.class.name,
    self.object_id,
    self.name,
    self.version,
    self.arch,
    self.build,
    self.tag
  ]
end

#owned_filesObject

Set the file list in the package object in memory



165
166
167
# File 'lib/slackware/package.rb', line 165

def owned_files
  @owned_files ||= _owned_files()
end

#package_descriptionObject

Accessor for the PACKAGE DESCRIPTION from the package file



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/slackware/package.rb', line 92

def package_description
  return @package_description unless @package_description.nil?

  f = File.open(path() + '/' + self.fullname)
  loop do
    if (f.readline =~ RE_PACKAGE_DESCRIPTION)
      @package_description = f.take_while {|l|
        not(l =~ RE_FILE_LIST)
      }.map {|l|
        l.sub(/^#{self.name}:\s?/, '').chomp
      }
      return @package_description
    end
  end
end

#package_description=(desc) ⇒ Object

Setter for the PACKAGE DESCRIPTION, in the event you are parsing a repo file



109
110
111
# File 'lib/slackware/package.rb', line 109

def package_description=(desc)
  @package_description = desc
end

#package_locationObject

Accessor for the PACKAGE LOCATION from the package file



114
115
116
117
118
119
120
121
122
123
# File 'lib/slackware/package.rb', line 114

def package_location
  return @package_location unless @package_location.nil?

  f = File.open(self.path + '/' + self.fullname)
  loop do
    if (f.readline =~ RE_PACKAGE_LOCATION)
      return @package_location = $1
    end
  end
end

#package_location=(path) ⇒ Object

Setter for the PACKAGE LOCATION, in the event you are parsing a repo file



126
127
128
# File 'lib/slackware/package.rb', line 126

def package_location=(path)
  @package_location = path
end

#parse(name) ⇒ Object

pkg.parse instance method for parsing the package information



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/slackware/package.rb', line 47

def parse(name)
  if name.include?("/")
    self.path = File.dirname(name)
    name = File.basename(name)
  end
  if (name =~ RE_REMOVED_NAMES)
    name = $1
    self.upgrade_time = MyTime.strptime($2 + ' ' + $3, fmt=FMT_UPGRADE_TIME)
  end
  arr = name.split('-')
  build = arr.pop
  if (build.include?("_"))
    self.tag_sep = "_"
    self.build = build.split(self.tag_sep)[0]
    self.tag = build.split(self.tag_sep)[1..-1].join(self.tag_sep)
  elsif (build =~ RE_BUILD_TAG)
    self.build = $1
    self.tag = $2
  else
    self.build = build
    self.tag = ""
  end
  self.arch = arr.pop
  self.version = arr.pop
  self.name = arr.join('-')
end

#timeObject

populates and returns self.time



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/slackware/package.rb', line 200

def time
  if (@time.nil? && self.path)
    if (File.exist?(self.path + "/" + self.fullname))
      @time = File.mtime(self.path + "/" + self.fullname)
    end
  elsif (not(self.path) && (@time.nil?))
    if (File.exist?(Paths::installed_packages() + "/" + self.fullname))
      @time = File.mtime(Paths::installed_packages() + "/" + self.fullname)
    end
  end
  return @time
end

#to_hObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/slackware/package.rb', line 218

def to_h
  {
    "name" => @name,
    "version" => @version,
    "arch" => @arch,
    "build" => @build,
    "tag" => @tag,
    "tag_sep" => @tag_sep,
    "upgrade_time" => @upgrade_time,
    "compressed_size" => compressed_size(),
    "uncompressed_size" => uncompressed_size(),
    "path" => path(),
    "time" => time(),
    "owned_files" => owned_files(),
  }
end

#uncompressed_sizeObject

Accessor for the UNCOMPRESSED PACKAGE SIZE from the package file



131
132
133
134
135
136
137
138
139
140
# File 'lib/slackware/package.rb', line 131

def uncompressed_size
  return @uncompressed_size unless @uncompressed_size.nil?

  f = File.open(self.path + '/' + self.fullname)
  loop do
    if (f.readline =~ RE_UNCOMPRESSED_PACKAGE_SIZE)
      return @uncompressed_size = $1
    end
  end
end

#uncompressed_size=(size) ⇒ Object

Setter for the UNCOMPRESSED PACKAGE SIZE, in the event you are parsing a repo file



143
144
145
# File 'lib/slackware/package.rb', line 143

def uncompressed_size=(size)
  @uncompressed_size = size
end