Class: Deb::S3::Manifest

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/deb/s3/manifest.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

#initializeManifest

Returns a new instance of Manifest.



22
23
24
25
26
27
28
29
30
31
# File 'lib/deb/s3/manifest.rb', line 22

def initialize
  @packages = []
  @packages_to_be_upload = []
  @component = nil
  @architecture = nil
  @files = {}
  @cache_control = ""
  @fail_if_exists = false
  @skip_package_upload = false
end

Instance Attribute Details

#architectureObject

Returns the value of attribute architecture.



13
14
15
# File 'lib/deb/s3/manifest.rb', line 13

def architecture
  @architecture
end

#cache_controlObject

Returns the value of attribute cache_control.



12
13
14
# File 'lib/deb/s3/manifest.rb', line 12

def cache_control
  @cache_control
end

#codenameObject

Returns the value of attribute codename.



10
11
12
# File 'lib/deb/s3/manifest.rb', line 10

def codename
  @codename
end

#componentObject

Returns the value of attribute component.



11
12
13
# File 'lib/deb/s3/manifest.rb', line 11

def component
  @component
end

#fail_if_existsObject

Returns the value of attribute fail_if_exists.



14
15
16
# File 'lib/deb/s3/manifest.rb', line 14

def fail_if_exists
  @fail_if_exists
end

#filesObject

Returns the value of attribute files.



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

def files
  @files
end

#packagesObject (readonly)

Returns the value of attribute packages.



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

def packages
  @packages
end

#packages_to_be_uploadObject (readonly)

Returns the value of attribute packages_to_be_upload.



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

def packages_to_be_upload
  @packages_to_be_upload
end

#skip_package_uploadObject

Returns the value of attribute skip_package_upload.



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

def skip_package_upload
  @skip_package_upload
end

Class Method Details

.parse_packages(str) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/deb/s3/manifest.rb', line 50

def parse_packages(str)
  m = self.new
  str.split("\n\n").each do |s|
    next if s.chomp.empty?
    m.packages << Deb::S3::Package.parse_string(s)
  end
  m
end

.retrieve(codename, component, architecture, cache_control, fail_if_exists, skip_package_upload = false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/deb/s3/manifest.rb', line 34

def retrieve(codename, component, architecture, cache_control, fail_if_exists, skip_package_upload=false)
  m = if s = Deb::S3::Utils.s3_read("dists/#{codename}/#{component}/binary-#{architecture}/Packages")
    self.parse_packages(s)
  else
    self.new
  end

  m.codename = codename
  m.component = component
  m.architecture = architecture
  m.cache_control = cache_control
  m.fail_if_exists = fail_if_exists
  m.skip_package_upload = skip_package_upload
  m
end

Instance Method Details

#add(pkg, preserve_versions, needs_uploading = true) ⇒ Object



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

def add(pkg, preserve_versions, needs_uploading=true)
  if self.fail_if_exists
    packages.each { |p|
      next unless p.name == pkg.name && \
                  p.full_version == pkg.full_version && \
                  File.basename(p.url_filename(@codename)) != \
                  File.basename(pkg.url_filename(@codename))
      raise AlreadyExistsError,
            "package #{pkg.name}_#{pkg.full_version} already exists " \
            "with different filename (#{p.url_filename(@codename)})"
    }
  end
  if preserve_versions
    packages.delete_if { |p| p.name == pkg.name && p.full_version == pkg.full_version }
  else
    packages.delete_if { |p| p.name == pkg.name }
  end
  packages << pkg
  packages_to_be_upload << pkg if needs_uploading
  pkg
end

#delete_package(pkg, versions = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/deb/s3/manifest.rb', line 82

def delete_package(pkg, versions=nil)
  deleted = []
  new_packages = @packages.select { |p|
      # Include packages we didn't name
      if p.name != pkg
         p
      # Also include the packages not matching a specified version
      elsif (!versions.nil? and p.name == pkg and !versions.include?(p.version) and !versions.include?("#{p.version}-#{p.iteration}") and !versions.include?(p.full_version))
          p
      end
  }
  deleted = @packages - new_packages
  @packages = new_packages
  deleted
end

#generateObject



98
99
100
# File 'lib/deb/s3/manifest.rb', line 98

def generate
  @packages.collect { |pkg| pkg.generate(@codename) }.join("\n")
end

#hashfile(path) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/deb/s3/manifest.rb', line 136

def hashfile(path)
  {
    :size   => File.size(path),
    :sha1   => Digest::SHA1.file(path).hexdigest,
    :sha256 => Digest::SHA2.file(path).hexdigest,
    :md5    => Digest::MD5.file(path).hexdigest
  }
end

#write_to_s3 {|f| ... } ⇒ Object

Yields:

  • (f)


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
133
134
# File 'lib/deb/s3/manifest.rb', line 102

def write_to_s3
  manifest = self.generate

  unless self.skip_package_upload
    # store any packages that need to be stored
    @packages_to_be_upload.each do |pkg|
      yield pkg.url_filename(@codename) if block_given?
      s3_store(pkg.filename, pkg.url_filename(@codename), 'application/octet-stream; charset=binary', self.cache_control, self.fail_if_exists)
    end
  end

  # generate the Packages file
  pkgs_temp = Tempfile.new("Packages")
  pkgs_temp.write manifest
  pkgs_temp.close
  f = "dists/#{@codename}/#{@component}/binary-#{@architecture}/Packages"
  yield f if block_given?
  s3_store(pkgs_temp.path, f, 'text/plain; charset=utf-8', self.cache_control)
  @files["#{@component}/binary-#{@architecture}/Packages"] = hashfile(pkgs_temp.path)
  pkgs_temp.unlink

  # generate the Packages.gz file
  gztemp = Tempfile.new("Packages.gz")
  gztemp.close
  Zlib::GzipWriter.open(gztemp.path) { |gz| gz.write manifest }
  f = "dists/#{@codename}/#{@component}/binary-#{@architecture}/Packages.gz"
  yield f if block_given?
  s3_store(gztemp.path, f, 'application/x-gzip; charset=binary', self.cache_control)
  @files["#{@component}/binary-#{@architecture}/Packages.gz"] = hashfile(gztemp.path)
  gztemp.unlink

  nil
end