Class: Debci::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/debci/repository.rb

Overview

This class implements the backend access to the debci data files. Normally you should access the data through objects of the Debci::Package class, which you can obtain by calling this class' find_package method.

>> repository = Debci::Repository.new
>> package = repository.find_package('mypackage')

Defined Under Namespace

Classes: PackageNotFound

Instance Method Summary (collapse)

Constructor Details

- (Repository) initialize(path = nil)

:nodoc:



17
18
19
20
21
# File 'lib/debci/repository.rb', line 17

def initialize(path=nil) # :nodoc:
  path ||= Debci.config.data_basedir
  @path = path
  @data_dirs = Dir.glob(File.join(path, '*-*')).reject { |d| d =~ /\.old$/ }
end

Instance Method Details

- (Object) architectures

Returns an Array of suites known to this debci instance



29
30
31
# File 'lib/debci/repository.rb', line 29

def architectures
  @architectures ||= @data_dirs.map { |d| File.basename(d).split('-').last }.uniq.sort
end

- (Object) architectures_for(package)

Returns an Array of architectures for which there is data for package.



45
46
47
48
# File 'lib/debci/repository.rb', line 45

def architectures_for(package)
  package = String(package)
  data_dirs_for(package).map { |d| File.basename(d).split('-').last }.uniq
end

- (Object) find_package(name)

Returns a single package by its names.

Raises a Debci::PackageNotFound is there is no package with that name.



57
58
59
60
61
62
63
# File 'lib/debci/repository.rb', line 57

def find_package(name)
  if !packages.include?(name)
    raise PackageNotFound.new(name)
  end

  Debci::Package.new(name, self)
end

- (Object) news_for(package, n = 10)

Backend implementation for Debci::Package#news



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/debci/repository.rb', line 94

def news_for(package, n=10)
  suites = '{' + self.suites.join(',') + '}'
  architectures = '{' + self.architectures.join(',') + '}'
  history = Dir.glob(File.join(data_dir(suites, architectures, package), '[0-9]*.json')).sort_by { |f| File.basename(f) }

  news = []

  while !history.empty?
    file = history.pop
    suite_arch = File.basename(File.expand_path(File.dirname(file) + '/../../..'))
    suite, architecture = suite_arch.split('-')
    status = load_status(file, suite, architecture)
    if status.newsworthy?
      news << status
    end
    if news.size >= n
      break
    end
  end

  news
end

- (Object) packages

Returns a Set of packages known to this debci instance



34
35
36
# File 'lib/debci/repository.rb', line 34

def packages
  @packages ||= @data_dirs.map { |d| Dir.glob(File.join(d, 'packages/*/*')) }.flatten.map { |d| File.basename(d) }.to_set
end

- (Object) search(query)

Searches packages by name.

Returns an Array of Debci::Package objects. On an exact match, will return an Array with a single element. Otherwise all packages that match the query (which is converted into a regular expression) are returned.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/debci/repository.rb', line 70

def search(query)
  # first try exact match
  match = packages.select { |p| p == query }

  # then try regexp match
  if match.empty?
    re = Regexp.new(query)
    match = packages.select { |p| p =~ re }
  end

  match.map { |p| Debci::Package.new(p, self)}
end

- (Object) status_for(package)

Backend implementation for Debci::Package#status



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

def status_for(package)
  architectures.map do |arch|
    suites.map do |suite|
      status_file = File.join(data_dir(suite, arch, package), 'latest.json')
      load_status(status_file, suite, arch)
    end
  end
end

- (Object) suites

Returns an Array of suites known to this debci instance



24
25
26
# File 'lib/debci/repository.rb', line 24

def suites
  @suites ||= @data_dirs.map { |d| File.basename(d).split('-').first }.uniq.sort
end

- (Object) suites_for(package)

Returns an Array of suites for which there is data for package.



39
40
41
42
# File 'lib/debci/repository.rb', line 39

def suites_for(package)
  package = String(package)
  data_dirs_for(package).map { |d| File.basename(d).split('-').first }.uniq
end