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.



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

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

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



61
62
63
# File 'lib/debci/data.rb', line 61

def root
  @root
end

#tarballObject (readonly)

Returns the value of attribute tarball.



61
62
63
# File 'lib/debci/data.rb', line 61

def tarball
  @tarball
end

Instance Method Details

#import!Object



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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/debci/data.rb', line 68

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.write(history, JSON.pretty_generate(data))
        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



111
112
113
114
115
116
117
118
119
# File 'lib/debci/data.rb', line 111

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



121
122
123
# File 'lib/debci/data.rb', line 121

def update_html(pkgs)
  Debci::HTML.update unless pkgs.empty?
end