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.



69
70
71
72
# File 'lib/debci/data.rb', line 69

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

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

#tarballObject (readonly)

Returns the value of attribute tarball.



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

def tarball
  @tarball
end

Instance Method Details

#import!Object



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
115
116
117
# File 'lib/debci/data.rb', line 74

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



119
120
121
122
123
124
125
126
127
# File 'lib/debci/data.rb', line 119

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



129
130
131
132
133
134
# File 'lib/debci/data.rb', line 129

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