Class: Debci::Data::Import

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tarball) ⇒ Import

Returns a new instance of Import.



66
67
68
69
# File 'lib/debci/data.rb', line 66

def initialize(tarball)
  @root = Debci.config.data_basedir
  @tarball = tarball
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



64
65
66
# File 'lib/debci/data.rb', line 64

def root
  @root
end

#tarballObject (readonly)

Returns the value of attribute tarball.



64
65
66
# File 'lib/debci/data.rb', line 64

def tarball
  @tarball
end

Instance Method Details

#import!Object



71
72
73
74
75
76
77
78
79
80
81
82
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
# File 'lib/debci/data.rb', line 71

def import!
  pkgs = Set.new

  Dir.mktmpdir do |tmpdir|
    system('tar', 'xaf', tarball, '-C', tmpdir)
    Dir.chdir(tmpdir) do
      mapping = {}
      Dir['export/*.json'].each do |json|
        jobs = JSON.parse(File.read(json))
        jobs.each do |data|
          # load job to database
          orig_run_id = data.delete('run_id')
          job = load_job(data)

          pkgs.add(job.package)

          mapping[orig_run_id] = job.run_id
          puts"# loaded job: #{orig_run_id} -> #{job.run_id}"
          rename_files(job, orig_run_id)
        end
      end

      Dir['packages/*/*/*/*/history.json'].each do |history|
        data = JSON.parse(File.read(history))
        data.each do |entry|
          old_id = entry['run_id'].to_i
          new_id = mapping[old_id]
          if new_id
            entry['run_id'] = new_id
          end
        end
        File.open(history, 'w') do |f|
          f.write(JSON.pretty_generate(data))
        end
        puts "# rewrite #{history}"
      end
    end
    puts('# copying data files ...')
    cmd = ['rsync', '-apq', '--exclude=/export', "#{tmpdir}/", "#{root}/"]
    puts cmd.join(' ')
    system(*cmd)
  end
  update_html(pkgs.to_a)
end

#load_job(data) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'lib/debci/data.rb', line 116

def load_job(data)
  job = Debci::Job.new
  job.package = Debci::Package.find_or_create_by!(name: data.delete("package"))
  data.each do |attr, value|
    job[attr] = value if job.attributes.key?(attr)
  end
  job.save!
  job
end

#update_html(pkgs) ⇒ Object



126
127
128
129
130
131
# File 'lib/debci/data.rb', line 126

def update_html(pkgs)
  pkgs.each do |p|
    Debci::HTML.update_package(p)
  end
  Debci::HTML.update unless pkgs.empty?
end