Commit cc0f76b47b2fd654efa3f42bd79e638dfe946d36

Thomas de Grivel 2023-02-28T11:24:31

ci_mux

diff --git a/lib/rbpkg.rb b/lib/rbpkg.rb
index 76eed8e..845c3c4 100644
--- a/lib/rbpkg.rb
+++ b/lib/rbpkg.rb
@@ -55,16 +55,17 @@ module Rbpkg
 
   def self.ci_mux
     verbose(3, "Rbpkg.ci_mux")
-    status = {}
-    ci_mux_host_status(status)
-    ci_mux_status(status)
-    log = {}
+    status = ci_mux_status_hosts
+    final_status = ci_mux_status(status)
+    log = ci_mux_log_hosts(status, final_status)
+    ci_mux_log(log)
+  end
+
+  def self.ci_mux_log(log, final_status)
     Dir.chdir("#{Rbpkg.ci_dir}/log") do
-      ci_mux_log(log)
-      log.each do |name, l|
+      log.each do |name, sublogs|
         File.open("#{name}.html", "w") do |output|
-          status_file = "#{Rbpkg.ci_dir}/status/#{name}.status"
-          s = File.read(status_file).strip
+          s = final_status[name]
           img = status_img(s)
           output.puts <<-EOF
 <!DOCTYPE html>
@@ -88,15 +89,13 @@ module Rbpkg
     </h1>
     <ul class="list-group">
 EOF
-          Dir["*/*/#{name}.log.html"].each do |subdir_file|
-            status_file = "#{Rbpkg.ci_dir}/status/#{subdir_file.gsub(/.log.html$/, ".status")}"
-            s = File.read(status_file).strip
-            img = status_img(s)
-            subdir_file_html = subdir_file.to_html
+          sublogs.each do |d, sublog|
+            sublog.img = status_img(sublog.status)
+            sublog_html = sublog.html.to_html
             output.puts <<-EOF
       <li class="list-group-item">
-        <img src="#{img}" class="status-#{s.to_html}"/>
-        <a href="#{subdir_file_html}">#{subdir_file_html}</a>
+        <img src="#{img}" class="status-#{sublog.status.to_s.to_html}"/>
+        <a href="#{sublog_html}">#{sublog_html}</a>
       </li>
 EOF
           end
@@ -110,54 +109,75 @@ EOF
     end
   end
 
-  def self.ci_mux_log(logs)
-    Dir["*/*"].each do |d|
-      Dir.chdir(d) do
-        Dir["*/*.log"].each do |subdir_file|
-          name = File.basename(subdir_file, ".log")
-          file = "#{name}.log"
-          if ! log[name] || ! FileUtils.uptodate?(file, [subdir_file])
-            FileUtils.cp(subdir_file, file)
-            FileUtils.cp("#{subdir_file}.html", "#{file}.html")
-            log[name] = true
+  def self.ci_mux_log_hosts(status, final_status)
+    log = {}
+    Dir.chdir("#{Rbpkg.ci_dir}/log") do
+      Dir["*/*"].each do |d|
+        Dir.chdir(d) do
+          Dir["*/*.log"].each do |subdir_file|
+            name = File.basename(subdir_file, ".log")
+            d_name = "#{d}/#{name}"
+            file = "#{name}.log"
+            if ! File.exist?(file) || File.mtime(file) <= File.mtime(subdir_file)
+              FileUtils.cp(subdir_file, file)
+              subdir_file_html = "#{subdir_file}.html"
+              FileUtils.cp(subdir_file_html, "#{file}.html")
+              log_item = {log: subdir_file,
+                          html: subdir_file_html,
+                          status: status[name][d_name]}
+              if ! log[name]
+                log[name] = {}
+              end
+              log[name][d_name] = log_item
+            end
           end
         end
       end
     end
   end
 
-  def self.ci_mux_host_status(status)
+  def self.ci_mux_status(status)
+    final_status = {}
+    Dir.chdir("#{ci_dir}/status") do
+      status.each do |name, s|
+        sh_name = sh_quote(name)
+        final_status[name] = if s.find(:running)
+                               :running
+                             elsif s.find(:ko)
+                               :ko
+                             else
+                               :ok
+                             end
+        File.write("#{name}.status", final_status[name])
+      end
+    end
+    final_status
+  end
+
+  def self.ci_mux_status_hosts
+    status = {}
     Dir.chdir("#{ci_dir}/status") do
       Dir["*/*"].each do |d|
         Dir.chdir(d) do
           Dir["*/*.status"].each do |subdir_file|
+            s = File.read(subdir_file).strip.to_sym
+            raise "invalid status" unless [:running, :ko, :ok].include?(s)
             name = File.basename(subdir_file, ".status")
             puts "status[#{name.inspect}]"
+            d_name = "#{d}/#{name}"
             file = "#{name}.status"
-            if ! FileUtils.uptodate?(file, [subdir_file])
+            if ! File.exist(file) || File.mtime(file) <= File.mtime(subdir_file)
               cmd! "cp -a #{sh_quote(subdir_file)} #{sh_quote(file)}"
-              status[name] = true
+              if ! status[name]
+                status[name] = {}
+              end
+              status[name][d_name] = s
             end
           end
         end
       end
     end
-  end
-
-  def self.ci_mux_status(status)
-    Dir.chdir("#{ci_dir}/status") do
-      status.each do |name, s|
-        sh_name = sh_quote(name)
-        s = if system("grep -q running */*/#{sh_name}.status")
-              "running\n"
-            elsif system("grep -q ko */*/#{sh_name}.status")
-              "ko\n"
-            else
-              "ok\n"
-            end
-        File.write("#{name}.status", s)
-      end
-    end
+    status
   end
 
   def self.ci_remote(repo, branch, commit, host)