Class: Deb::S3::Package

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

access_policy, access_policy=, bucket, bucket=, debianize_op, encryption, encryption=, gpg_options, gpg_options=, prefix, prefix=, s3, s3=, s3_escape, s3_exists?, s3_path, s3_read, s3_remove, s3_store, safesystem, signing_key, signing_key=, template

Constructor Details

#initializePackage

Returns a new instance of Package.



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
125
126
127
128
129
130
131
132
# File 'lib/deb/s3/package.rb', line 96

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

  @dependencies = []
end

Instance Attribute Details

#architectureObject

Returns the value of attribute architecture.



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

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/s3/package.rb', line 31

def attributes
  @attributes
end

#categoryObject

Returns the value of attribute category.



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

def category
  @category
end

#dependenciesObject

Returns the value of attribute dependencies.



27
28
29
# File 'lib/deb/s3/package.rb', line 27

def dependencies
  @dependencies
end

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#epochObject

Returns the value of attribute epoch.



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

def epoch
  @epoch
end

#filenameObject

Returns the value of attribute filename.



39
40
41
# File 'lib/deb/s3/package.rb', line 39

def filename
  @filename
end

#iterationObject

Returns the value of attribute iteration.



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

def iteration
  @iteration
end

#licenseObject

Returns the value of attribute license.



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

def license
  @license
end

#maintainerObject

Returns the value of attribute maintainer.



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

def maintainer
  @maintainer
end

#md5Object

Returns the value of attribute md5.



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

def md5
  @md5
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#sha1Object

hashes



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

def sha1
  @sha1
end

#sha256Object

Returns the value of attribute sha256.



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

def sha256
  @sha256
end

#sizeObject

Returns the value of attribute size.



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

def size
  @size
end

#urlObject

Returns the value of attribute url.



21
22
23
# File 'lib/deb/s3/package.rb', line 21

def url
  @url
end

#vendorObject

Returns the value of attribute vendor.



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

def vendor
  @vendor
end

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.extract_control(package) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/deb/s3/package.rb', line 58

def extract_control(package)
  if system("which dpkg > /dev/null 2>&1")
    output, status = Open3.capture2("dpkg", "-f", package)
    output
  else
	# use ar to determine control file name (control.ext)
    package_files = `ar t #{package}`
    control_file = package_files.split("\n").select do |file|
      file.start_with?("control.")
    end.first
    if control_file === "control.tar.gz"
	  compression = "z"
    elsif control_file === "control.tar.zst"
      compression = "I zstd"
	else
	  compression = "J"
    end

    # 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_file}"

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

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

.parse_file(package) ⇒ Object



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

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



52
53
54
55
56
# File 'lib/deb/s3/package.rb', line 52

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

Instance Method Details

#apply_file_info(file) ⇒ Object

def extract_info



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

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



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
# File 'lib/deb/s3/package.rb', line 231

def extract_info(control)
  fields = parse_control(control)

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

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

  # Packages manifest fields
  self.url_filename = fields.delete('Filename')
  self.sha1 = fields.delete('SHA1')
  self.sha256 = fields.delete('SHA256')
  self.md5 = fields.delete('MD5sum')
  self.size = fields.delete('Size')
  self.description = fields.delete('Description')

  #self.config_files = config_files

  self.dependencies += Array(parse_depends(fields.delete('Depends')))

  self.attributes[:deb_recommends] = fields.delete('Recommends')
  self.attributes[:deb_suggests]   = fields.delete('Suggests')
  self.attributes[:deb_enhances]   = fields.delete('Enhances')
  self.attributes[:deb_pre_depends] = fields.delete('Pre-Depends')

  self.attributes[:deb_breaks]    = fields.delete('Breaks')
  self.attributes[:deb_conflicts] = fields.delete('Conflicts')
  self.attributes[:deb_provides]  = fields.delete('Provides')
  self.attributes[:deb_replaces]  = fields.delete('Replaces')

  self.attributes[:deb_field] = Hash[fields.map { |k, v|
    [k.sub(/\AX[BCS]{0,3}-/, ''), v]
  }]
end

#fix_dependency(dep) ⇒ Object

from fpm



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
226
227
228
# File 'lib/deb/s3/package.rb', line 178

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

#full_versionObject



134
135
136
137
# File 'lib/deb/s3/package.rb', line 134

def full_version
  return nil if [epoch, version, iteration].all?(&:nil?)
  [[epoch, version].compact.join(":"), iteration].compact.join("-")
end

#generate(codename) ⇒ Object



147
148
149
# File 'lib/deb/s3/package.rb', line 147

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

#parse_control(control) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/deb/s3/package.rb', line 286

def parse_control(control)
  field = nil
  value = ""
  {}.tap do |fields|
    control.each_line do |line|
      if line =~ /^(\s+)(\S.*)$/
        indent, rest = $1, $2
        # Continuation
        if indent.size == 1 && rest == "."
          value << "\n"
          rest = ""
        elsif value.size > 0
          value << "\n"
        end
        value << rest
      elsif line =~ /^([-\w]+):(.*)$/
        fields[field] = value if field
        field, value = $1, $2.strip
      end
    end
    fields[field] = value if field
  end
end

#parse_depends(data) ⇒ Object

from fpm



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

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(codename) ⇒ Object



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

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

#url_filename=(f) ⇒ Object



139
140
141
# File 'lib/deb/s3/package.rb', line 139

def url_filename=(f)
  @url_filename = f
end