Class: Deb::Fog::Package

Inherits:
Object
  • Object
show all
Extended by:
Utils
Includes:
Utils
Defined in:
lib/deb/fog/package.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

bucket, bucket=, debianize_op, fog, fog=, fog_escape, fog_exists?, fog_path, fog_read, fog_remove, fog_store, gpg_options, gpg_options=, is_public, is_public=, prefix, prefix=, safesystem, signing_key, signing_key=, template

Constructor Details

#initializePackage

Returns a new instance of Package.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/deb/fog/package.rb', line 83

def initialize
  @attributes = {}

  # Reference
  # http://www.debian.org/doc/manuals/maint-guide/first.en.html
  # http://wiki.debian.org/DeveloperConfiguration
  # https://github.com/jordansissel/fpm/issues/37
  if ENV.include?("DEBEMAIL") and ENV.include?("DEBFULLNAME")
    # Use DEBEMAIL and DEBFULLNAME as the default maintainer if available.
    @maintainer = "#{ENV["DEBFULLNAME"]} <#{ENV["DEBEMAIL"]}>"
  else
    # TODO(sissel): Maybe support using 'git config' for a default as well?
    # git config --get user.name, etc can be useful.
    #
    # Otherwise default to user@currenthost
    @maintainer = "<#{ENV["USER"]}@#{Socket.gethostname}>"
  end

  @name = nil
  @architecture = "native"
  @description = "no description given"
  @version = nil
  @epoch = nil
  @iteration = nil
  @url = nil
  @category = "default"
  @license = "unknown"
  @vendor = "none"
  @sha1 = nil
  @sha256 = nil
  @md5 = nil
  @size = nil
  @filename = nil
  @url_filename = nil

  @provides = []
  @conflicts = []
  @replaces = []
  @dependencies = []

  @needs_uploading = false
end

Instance Attribute Details

#architectureObject

Returns the value of attribute architecture.



19
20
21
# File 'lib/deb/fog/package.rb', line 19

def architecture
  @architecture
end

#attributesObject

Any other attributes specific to this package. This is where you’d put rpm, deb, or other specific attributes.



31
32
33
# File 'lib/deb/fog/package.rb', line 31

def attributes
  @attributes
end

#categoryObject

Returns the value of attribute category.



17
18
19
# File 'lib/deb/fog/package.rb', line 17

def category
  @category
end

#conflictsObject

Returns the value of attribute conflicts.



24
25
26
# File 'lib/deb/fog/package.rb', line 24

def conflicts
  @conflicts
end

#dependenciesObject

Returns the value of attribute dependencies.



22
23
24
# File 'lib/deb/fog/package.rb', line 22

def dependencies
  @dependencies
end

#descriptionObject

Returns the value of attribute description.



20
21
22
# File 'lib/deb/fog/package.rb', line 20

def description
  @description
end

#epochObject

Returns the value of attribute epoch.



12
13
14
# File 'lib/deb/fog/package.rb', line 12

def epoch
  @epoch
end

#excludesObject

Returns the value of attribute excludes.



26
27
28
# File 'lib/deb/fog/package.rb', line 26

def excludes
  @excludes
end

#filenameObject

Returns the value of attribute filename.



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

def filename
  @filename
end

#iterationObject

Returns the value of attribute iteration.



13
14
15
# File 'lib/deb/fog/package.rb', line 13

def iteration
  @iteration
end

#licenseObject

Returns the value of attribute license.



18
19
20
# File 'lib/deb/fog/package.rb', line 18

def license
  @license
end

#maintainerObject

Returns the value of attribute maintainer.



14
15
16
# File 'lib/deb/fog/package.rb', line 14

def maintainer
  @maintainer
end

#md5Object

Returns the value of attribute md5.



37
38
39
# File 'lib/deb/fog/package.rb', line 37

def md5
  @md5
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/deb/fog/package.rb', line 10

def name
  @name
end

#providesObject

Returns the value of attribute provides.



23
24
25
# File 'lib/deb/fog/package.rb', line 23

def provides
  @provides
end

#replacesObject

Returns the value of attribute replaces.



25
26
27
# File 'lib/deb/fog/package.rb', line 25

def replaces
  @replaces
end

#sha1Object

Returns the value of attribute sha1.



35
36
37
# File 'lib/deb/fog/package.rb', line 35

def sha1
  @sha1
end

#sha256Object

Returns the value of attribute sha256.



36
37
38
# File 'lib/deb/fog/package.rb', line 36

def sha256
  @sha256
end

#sizeObject

Returns the value of attribute size.



38
39
40
# File 'lib/deb/fog/package.rb', line 38

def size
  @size
end

#urlObject

Returns the value of attribute url.



16
17
18
# File 'lib/deb/fog/package.rb', line 16

def url
  @url
end

#url_filenameObject

hashes



34
35
36
# File 'lib/deb/fog/package.rb', line 34

def url_filename
  @url_filename
end

#vendorObject

Returns the value of attribute vendor.



15
16
17
# File 'lib/deb/fog/package.rb', line 15

def vendor
  @vendor
end

#versionObject

Returns the value of attribute version.



11
12
13
# File 'lib/deb/fog/package.rb', line 11

def version
  @version
end

Class Method Details

.extract_control(package) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/deb/fog/package.rb', line 59

def extract_control(package)
  if system("which dpkg &> /dev/null")
    `dpkg -f #{package}`
  else
    # ar fails to find the control.tar.gz tarball within the .deb
    # on Mac OS. Try using ar to list the control file, if found,
    # use ar to extract, otherwise attempt with tar which works on OS X.
    extract_control_tarball_cmd = "ar p #{package} control.tar.gz"

    begin
      safesystem("ar t #{package} control.tar.gz &> /dev/null")
    rescue SafeSystemError
      warn "Failed to find control data in .deb with ar, trying tar."
      extract_control_tarball_cmd = "tar zxf #{package} --to-stdout control.tar.gz"
    end

    Dir.mktmpdir do |path|
      safesystem("#{extract_control_tarball_cmd} | tar -zxf - -C #{path}")
      File.read(File.join(path, "control"))
    end
  end
end

.parse_file(package) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/deb/fog/package.rb', line 45

def parse_file(package)
  p = self.new
  p.extract_info(extract_control(package))
  p.apply_file_info(package)
  p.filename = package
  p
end

.parse_string(s) ⇒ Object



53
54
55
56
57
# File 'lib/deb/fog/package.rb', line 53

def parse_string(s)
  p = self.new
  p.extract_info(s)
  p
end

Instance Method Details

#apply_file_info(file) ⇒ Object

def extract_info



280
281
282
283
284
285
# File 'lib/deb/fog/package.rb', line 280

def apply_file_info(file)
  self.size = File.size(file)
  self.sha1 = Digest::SHA1.file(file).hexdigest
  self.sha256 = Digest::SHA2.file(file).hexdigest
  self.md5 = Digest::MD5.file(file).hexdigest
end

#extract_info(control) ⇒ Object

from fpm



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/deb/fog/package.rb', line 228

def extract_info(control)
  parse = lambda do |field|
    value = control[/^#{field}: .*/]
    if value.nil?
      return nil
    else
      return value.split(": ",2).last
    end
  end

  # Parse 'epoch:version-iteration' in the version string
  version_re = /^(?:([0-9]+):)?(.+?)(?:-(.*))?$/
  m = version_re.match(parse.call("Version"))
  if !m
    raise "Unsupported version string '#{parse.call("Version")}'"
  end
  self.epoch, self.version, self.iteration = m.captures

  self.architecture = parse.call("Architecture")
  self.category = parse.call("Section")
  self.license = parse.call("License") || self.license
  self.maintainer = parse.call("Maintainer")
  self.name = parse.call("Package")
  self.url = parse.call("Homepage")
  self.vendor = parse.call("Vendor") || self.vendor
  self.attributes[:deb_priority] = parse.call("Priority")
  self.attributes[:deb_origin] = parse.call("Origin")
  self.attributes[:deb_installed_size] = parse.call("Installed-Size")

  # Packages manifest fields
  self.url_filename = parse.call("Filename")
  self.sha1 = parse.call("SHA1")
  self.sha256 = parse.call("SHA256")
  self.md5 = parse.call("MD5sum")
  self.size = parse.call("Size")

  # The description field is a special flower, parse it that way.
  # The description is the first line as a normal Description field, but also continues
  # on future lines indented by one space, until the end of the file. Blank
  # lines are marked as ' .'
  description = control[/^Description: .*[^\Z]/m]
  description = description.gsub(/^[^(Description|\s)].*$/, "").split(": ", 2).last
  self.description = description.gsub(/^ /, "").gsub(/^\.$/, "")

  #self.config_files = config_files

  self.dependencies += Array(parse_depends(parse.call("Depends")))
  self.conflicts += Array(parse_depends(parse.call("Conflicts")))
  self.provides += Array(parse_depends(parse.call("Provides")))
  self.replaces += Array(parse_depends(parse.call("Replaces")))
end

#fix_dependency(dep) ⇒ Object

from fpm



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/deb/fog/package.rb', line 175

def fix_dependency(dep)
  # Deb dependencies are: NAME (OP VERSION), like "zsh (> 3.0)"
  # Convert anything that looks like 'NAME OP VERSION' to this format.
  if dep =~ /[\(,\|]/
    # Don't "fix" ones that could appear well formed already.
  else
    # Convert ones that appear to be 'name op version'
    name, op, version = dep.split(/ +/)
    if !version.nil?
      # Convert strings 'foo >= bar' to 'foo (>= bar)'
      dep = "#{name} (#{debianize_op(op)} #{version})"
    end
  end

  name_re = /^[^ \(]+/
  name = dep[name_re]
  if name =~ /[A-Z]/
    dep = dep.gsub(name_re) { |n| n.downcase }
  end

  if dep.include?("_")
    dep = dep.gsub("_", "-")
  end

  # Convert gem ~> X.Y.Z to '>= X.Y.Z' and << X.Y+1.0
  if dep =~ /\(~>/
    name, version = dep.gsub(/[()~>]/, "").split(/ +/)[0..1]
    nextversion = version.split(".").collect { |v| v.to_i }
    l = nextversion.length
    nextversion[l-2] += 1
    nextversion[l-1] = 0
    nextversion = nextversion.join(".")
    return ["#{name} (>= #{version})", "#{name} (<< #{nextversion})"]
  elsif (m = dep.match(/(\S+)\s+\(!= (.+)\)/))
    # Append this to conflicts
    self.conflicts += [dep.gsub(/!=/,"=")]
    return []
  elsif (m = dep.match(/(\S+)\s+\(= (.+)\)/)) and
      self.attributes[:deb_ignore_iteration_in_dependencies?]
    # Convert 'foo (= x)' to 'foo (>= x)' and 'foo (<< x+1)'
    # but only when flag --ignore-iteration-in-dependencies is passed.
    name, version = m[1..2]
    nextversion = version.split('.').collect { |v| v.to_i }
    nextversion[-1] += 1
    nextversion = nextversion.join(".")
    return ["#{name} (>= #{version})", "#{name} (<< #{nextversion})"]
  else
    # otherwise the dep is probably fine
    return dep.rstrip
  end
end

#generateObject



144
145
146
# File 'lib/deb/fog/package.rb', line 144

def generate
  template("package.erb").result(binding)
end

#needs_uploading?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/deb/fog/package.rb', line 140

def needs_uploading?
  @needs_uploading
end

#parse_depends(data) ⇒ Object

from fpm



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/deb/fog/package.rb', line 149

def parse_depends(data)
  return [] if data.nil? or data.empty?
  # parse dependencies. Debian dependencies come in one of two forms:
  # * name
  # * name (op version)
  # They are all on one line, separated by ", "

  dep_re = /^([^ ]+)(?: \(([>=<]+) ([^)]+)\))?$/
  return data.split(/, */).collect do |dep|
    m = dep_re.match(dep)
    if m
      name, op, version = m.captures
      # this is the proper form of dependency
      if op && version && op != "" && version != ""
        "#{name} (#{op} #{version})".strip
      else
        name.strip
      end
    else
      # Assume normal form dependency, "name op version".
      dep
    end
  end
end

#url_filename_encodedObject



136
137
138
# File 'lib/deb/fog/package.rb', line 136

def url_filename_encoded
  @url_filename || "pool/#{self.name[0]}/#{self.name[0..1]}/#{fog_escape(File.basename(self.filename))}"
end