Class: Akabei::Repository

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/akabei/repository.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Repository

Returns a new instance of Repository.



12
13
14
15
16
# File 'lib/akabei/repository.rb', line 12

def initialize(opts = {})
  @db = {}
  @include_files = opts[:include_files] || false
  @signer = opts[:signer]
end

Instance Attribute Details

#include_filesObject

Returns the value of attribute include_files.



10
11
12
# File 'lib/akabei/repository.rb', line 10

def include_files
  @include_files
end

#signerObject

Returns the value of attribute signer.



10
11
12
# File 'lib/akabei/repository.rb', line 10

def signer
  @signer
end

Class Method Details

.load(path, opts = {}) ⇒ Object



55
56
57
58
59
# File 'lib/akabei/repository.rb', line 55

def self.load(path, opts = {})
  new(opts).tap do |repo|
    repo.load(path)
  end
end

Instance Method Details

#==(other) ⇒ Object



22
23
24
25
26
27
# File 'lib/akabei/repository.rb', line 22

def ==(other)
  other.is_a?(self.class) &&
    signer == other.signer &&
    include_files == other.include_files &&
    @db == other.instance_variable_get(:@db)
end

#add(package) ⇒ Object



75
76
77
# File 'lib/akabei/repository.rb', line 75

def add(package)
  @db[package.name] = package.to_entry
end

#create_db(topdir, archive) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/akabei/repository.rb', line 124

def create_db(topdir, archive)
  @db.keys.sort.each do |pkgname|
    pkg_entry = @db[pkgname]
    archive.new_entry do |entry|
      entry.pathname = "#{pkg_entry.db_name}/"
      entry.copy_stat(topdir.join(entry.pathname).to_s)
      archive.write_header(entry)
    end
    %w[desc depends files].each do |fname|
      pathname = "#{pkg_entry.db_name}/#{fname}"
      path = topdir.join(pathname)
      if path.readable?
        archive.new_entry do |entry|
          entry.pathname = pathname
          entry.copy_stat(path.to_s)
          archive.write_header(entry)
          archive.write_data(path.read)
        end
      end
    end
  end
end

#load(path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/akabei/repository.rb', line 29

def load(path)
  path = Pathname.new(path)
  return unless path.readable?
  verify!(path)
  db = {}
  ArchiveUtils.each_entry(path) do |entry, archive|
    db_name, key = *entry.pathname.split('/', 2)
    if key.include?('/')
      raise Error.new("Malformed repository database: #{path}: #{entry.pathname}")
    end
    db[db_name] ||= PackageEntry.new
    case key
    when ''
      # Ignore
    when 'desc', 'depends', 'files'
      load_entries(db[db_name], archive.read_data)
    else
      raise Error.new("Unknown repository database key: #{key}")
    end
  end
  db.each_value do |entry|
    @db[entry.name] = entry
  end
  nil
end

#load_entries(entry, data) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/akabei/repository.rb', line 61

def load_entries(entry, data)
  key = nil
  data.each_line do |line|
    line.strip!
    if m = line.match(/\A%([A-Z0-9]+)%\z/)
      key = m[1].downcase
    elsif line.empty?
      key = nil
    else
      entry.add(key, line)
    end
  end
end

#remove(pkgname) ⇒ Object



79
80
81
# File 'lib/akabei/repository.rb', line 79

def remove(pkgname)
  @db.delete(pkgname)
end

#save(path) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/akabei/repository.rb', line 92

def save(path)
  Archive::Writer.open_filename(path.to_s, Archive::COMPRESSION_GZIP, Archive::FORMAT_TAR) do |archive|
    Dir.mktmpdir do |dir|
      dir = Pathname.new(dir)
      store_tree(dir)
      create_db(dir, archive)
    end
  end
  if signer
    signer.detach_sign(path)
  end
  nil
end

#store_tree(topdir) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/akabei/repository.rb', line 106

def store_tree(topdir)
  @db.each_value do |pkg_entry|
    pkgdir = topdir.join(pkg_entry.db_name)
    pkgdir.mkpath
    pkgdir.join('desc').open('w') do |f|
      pkg_entry.write_desc(f)
    end
    pkgdir.join('depends').open('w') do |f|
      pkg_entry.write_depends(f)
    end
    if @include_files
      pkgdir.join('files').open('w') do |f|
        pkg_entry.write_files(f)
      end
    end
  end
end

#verify!(path) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/akabei/repository.rb', line 83

def verify!(path)
  if signer && File.readable?("#{path}.sig")
    signer.verify!(path)
    true
  else
    false
  end
end