Commit 00e2a17dbce7b3ebd2f17209d4b813ef6d11687e

Thomas de Grivel 2023-02-24T19:04:40

wip rbpkg_ci

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
diff --git a/.gitignore b/.gitignore
index b251085..eaea6ed 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
 /bin/
+/ci/
 /include/
 /lib/
 /src/
diff --git a/bin/rbpkg b/bin/rbpkg
index 260ff70..5731ded 100755
--- a/bin/rbpkg
+++ b/bin/rbpkg
@@ -114,7 +114,7 @@ begin
   rbpkg
   Rbpkg::Log.ok_all
 rescue => error
-  verbose(-1, "#{error.class.name}: #{error.message}")
   verbose(-1, error.backtrace.reverse.join("\n"))
-  Rbpkg::Log.ko_all
+  verbose(-1, "#{error.class.name}: #{error.message}")
+  exit 1
 end
diff --git a/bin/rbpkg_ci b/bin/rbpkg_ci
old mode 100644
new mode 100755
index 99a17c3..bdd2b3a
--- a/bin/rbpkg_ci
+++ b/bin/rbpkg_ci
@@ -1,6 +1,5 @@
 #!/usr/bin/env ruby
-load "#{File.dirname(File.dirname(__FILE__))}/lib/rbpkg/log.rb"
-load "#{File.dirname(File.dirname(__FILE__))}/lib/rbpkg/ci.rb"
+load "#{File.dirname(File.dirname(__FILE__))}/lib/rbpkg.rb"
 
 $ARGS = ARGV
 
@@ -11,23 +10,23 @@ def shift
   arg
 end
 
-verbose = 2   # print command output
+verbose_level = 2   # print command output
 case $ARGS[0]
 when "-v"
-  verbose = 3 # print all messages
+  verbose_level = 3 # print all messages
   shift
 when "-q"
-  verbose = 1 # print commands
+  verbose_level = 1 # print commands
   shift
 when "-qq"
-  verbose = 0 # print nothing
+  verbose_level = 0 # print nothing
   shift
 end
 
-Rbpkg.init("rbpkg_ci", verbose)
+Rbpkg.init("rbpkg_ci", verbose_level)
 
 def usage()
-  STDERR.puts "Usage: #{File.basename($0)} REPO ..."
+  STDERR.puts "Usage: #{File.basename($0)} REPO BRANCH COMMIT"
   exit 1
 end
 
@@ -38,10 +37,18 @@ def rbpkg_ci
   when "--help"
     usage
   end
-  Rbpkg.CI.tag
-  $ARGS.each do |name|
-    Rbpkg.Repos.repo(name).ci
-  end
+  repo = shift
+  branch = shift
+  commit = shift
+  usage unless $ARGS == []
+  Rbpkg::Repos.repo(repo).ci(branch, commit)
 end
 
-rbpkg_ci
+begin
+  rbpkg_ci
+  Rbpkg::Log.ok_all
+rescue => error
+  verbose(-1, error.backtrace.reverse.join("\n"))
+  verbose(-1, "#{error.class.name}: #{error.message}")
+  exit 1
+end
diff --git a/lib/rbpkg.rb b/lib/rbpkg.rb
index 9499b8c..8c47031 100644
--- a/lib/rbpkg.rb
+++ b/lib/rbpkg.rb
@@ -31,6 +31,10 @@ module Rbpkg
     end
   end
 
+  def self.ci_dir
+    "#{dir}/ci"
+  end
+
   def self.clean_all(repos)
     repos.each do |name|
       Repos.repo(name).clean_all()
diff --git a/lib/rbpkg/log.rb b/lib/rbpkg/log.rb
index 727c887..fda3a6a 100644
--- a/lib/rbpkg/log.rb
+++ b/lib/rbpkg/log.rb
@@ -1,87 +1,129 @@
+require 'base64'
+
 module Rbpkg
-  module Log
+  class Log
+    attr_reader :name, :path, :path_html
+    attr_accessor :status
 
-    def self.add(name)
-      raise "already logging to #{name.inspect}" if @@logs.include?(name)
-      raise "log file exists: #{name.inspect}" if File.exist?(path(name))
-      @@logs << name
-      verbose(3, "Logging to #{path(name)}")
-    end
-
-    def self.ansi_to_html(string)
-      substitution = {
-        "&" => "&amp;",
-        "<" => "&lt;",
-        ">" => "&gt;",
-        "\33[0;31m" => "<span style=\"color: red;\">",
-        "\33[0;34m" => "<span style=\"color: blue;\">",
-        "\33[0;35m" => "<span style=\"color: purple;\">",
-        "\33[0m" => "</span>"
-      }
-      s = string.clone
-      substitution.each do |k, v|
-        s.gsub!(k, v)
+    def initialize(name)
+      @name = name
+      @path = "#{Rbpkg.log_dir}/#{name}_#{Rbpkg.date}_#{$$}.log"
+      @path_html = "#{path}.html"
+      @status = :running
+      raise "log file exists: #{@path.inspect}" if File.exist?(@path)
+      raise "log file exists: #{@path_html.inspect}" if File.exist?(@path_html)
+    end
+
+    def img
+      path = "#{File.dirname(File.dirname(File.dirname(__FILE__)))}/share/icon/64x64/status_#{status}.png"
+      file = File.read(path)
+      base64 = Base64.encode64(file)
+      "data:image/png;base64,#{base64}"
+    end
+
+    def put_layout
+      tmp = path_html + '.tmp'
+      log = File.read(path)
+      html = <<EOF
+<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <meta charset="utf-8"/>
+    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
+    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
+    <title>#{path.to_html}</title>
+    <link rel="stylesheet" href="/_assets/app.css">
+    <script defer type="text/javascript" src="/_assets/app.js"></script>
+    <link rel="icon" type="image/png" sizes="64x64" href="${IMG}">
+  </head>
+  <body>
+    <div class="ci-header">
+      <a href="./"><i class="fas fa-asterisk"></i> All logs</a>
+    </div>
+    <h1>
+      <img src="#{img}" class="status-#{status.to_s.to_html}"/>
+      #{path.to_html}
+    </h1>
+    <pre><code>#{log.ansi_to_html}</code></pre>
+  </body>
+</html>
+EOF
+      File.write(tmp, html)
+      verbose(3, "Writing #{path_html}")
+      FileUtils.mv(tmp, path_html)
+    end
+
+    def puts(string)
+      File.open(path, "a") do |output|
+        output.puts string
       end
-      s
+    end
+
+    def self.add(name)
+      raise "already logging to #{name.inspect}" if @@log[name]
+      log = new(name)
+      @@log[name] = log
+      verbose(3, "Logging to #{log.path}")
     end
 
     def self.init(name)
       @@name = name
-      @@logs = []
+      @@log = {}
       add(name)
       at_exit { ko_all }
     end
 
     def self.ko(name)
-      puts_by_name(name, status_message_ko)
-      remove(name)
+      log = remove(name)
+      log.status = :ko
+      log.puts(status_message_ko)
+      log.put_layout
     end
 
     def self.ko_all
-      if @@logs != []
+      if @@log != {}
+        @@log.each do |name, log|
+          log.status = :ko
+        end
         verbose(-1, status_message_ko)
-        @@logs = []
+        remove_all
       end
     end
 
     def self.ok(name)
-      puts_by_name(name, status_message_ok)
-      remove(name)
+      log = remove(name)
+      log.status = :ok
+      log.puts(status_message_ok)
+      log.put_layout
     end
 
     def self.ok_all
-      if @@logs != []
+      if @@log != []
+        @@log.each do |name, log|
+          log.status = :ok
+        end
         verbose(3, status_message_ok)
-        @@logs = []
+        remove_all
       end
     end
 
-    def self.path(name)
-      "#{Rbpkg.log_dir}/#{name}_#{Rbpkg.date}_#{$$}.log"
-    end
-
-    def self.path_html(name)
-      "#{path(name)}.html"
-    end
-
     def self.puts(string)
-      @@logs.each do |name|
-        puts_by_name(name, string)
+      @@log.each do |name, log|
+        log.puts(string)
       end
     end
 
-    def self.puts_by_name(name, string)
-      File.open(path(name), "a") do |output|
-        output.puts string
-      end
-      File.open(path_html(name), "a") do |output|
-        output.puts ansi_to_html(string)
-      end
+    def self.remove(name)
+      log = @@log[name]
+      raise "not logging to #{name.inspect}" unless log
+      @@log.delete(name)
+      log
     end
 
-    def self.remove(name)
-      raise "not logging to #{name.inspect}" unless @@logs.include?(name)
-      @@logs.delete(name)
+    def self.remove_all
+      logs = @@log
+      @@log = {}
+      logs
     end
 
     def self.status_message(status)
@@ -95,5 +137,34 @@ module Rbpkg
     def self.status_message_ok
       status_message("OK")
     end
+  end
 end
+
+class String
+  def ansi_to_html
+    substitution = {
+      "\33[0;31m" => "<span style=\"color: red;\">",
+      "\33[0;34m" => "<span style=\"color: blue;\">",
+      "\33[0;35m" => "<span style=\"color: purple;\">",
+      "\33[0m" => "</span>"
+    }
+    s = to_html
+    substitution.each do |k, v|
+      s.gsub!(k, v)
+    end
+    s
+  end
+
+  def to_html
+    substitution = {
+      "&" => "&amp;",
+      "<" => "&lt;",
+      ">" => "&gt;"
+    }
+    s = clone
+    substitution.each do |k, v|
+      s.gsub!(k, v)
+    end
+    s
+  end
 end
diff --git a/lib/rbpkg/repo.rb b/lib/rbpkg/repo.rb
index ddcd125..a18e29a 100644
--- a/lib/rbpkg/repo.rb
+++ b/lib/rbpkg/repo.rb
@@ -32,13 +32,47 @@ class Rbpkg::Repo
   end
 
   def checkout(branch)
-    verbose 3, "repo(#{name.inspect}).checkout"
+    verbose(3, "repo(#{name.inspect}).checkout")
     sh_branch=sh_quote(branch)
     cmd! "cd #{sh_quote(src_dir!)} && git fetch #{sh_quote(remote)} #{sh_branch} && git checkout #{sh_branch} -- && git submodule update"
   end
 
+  def ci(ref, commit)
+    verbose(3, "repo(#{name.inspect}).ci(#{commit.inspect})")
+    Rbpkg::Log.add(ci_log_name_ref(ref))
+    Rbpkg::Log.add(ci_log_name_commit(commit))
+    sh_name = sh_quote(name)
+    sh_profile = sh_quote(ci_dir + '/etc/profile')
+    if File.directory?(ci_dir)
+      Rbpkg::Log.add("rbpkg_ci.#{name}.upgrade")
+      if cmd ". #{sh_profile} && rbpkg checkout #{sh_quote(commit)} #{sh_name} && rbpkg upgrade #{sh_name}"
+        Rbpkg::Log.ok("rbpkg_ci.#{name}.upgrade")
+      else
+        Rbpkg::Log.ko("rbpkg_ci.#{name}.upgrade")
+      end
+    end
+    Rbpkg::Log.add("rbpkg_ci.#{name}.install")
+    cmd! "rbpkg_bootstrap -f #{sh_quote(ci_dir)}"
+    cmd! ". #{sh_profile} && rbpkg clone #{sh_name} && rbpkg checkout #{sh_quote(commit)} #{sh_name} && rbpkg install #{sh_name}"
+    Rbpkg::Log.ok("rbpkg_ci.#{name}.install")
+    Rbpkg::Log.ok(ci_log_name_commit(commit))
+    Rbpkg::Log.ok(ci_log_name_ref(ref))
+  end
+
+  def ci_dir
+    @ci_dir || @ci_dir = "#{Rbpkg.ci_dir}/#{name}"
+  end
+
+  def ci_log_name_commit(commit)
+    "rbpkg_ci.#{name}.commit_#{commit}"
+  end
+
+  def ci_log_name_ref(ref)
+    "rbpkg_ci.#{name}.ref_#{ref}"
+  end
+
   def clean_all
-    verbose 3, "repo(#{name.inspect}).clean_all"
+    verbose(3, "repo(#{name.inspect}).clean_all")
     clean_fake
     clean_build
     clean_configure
@@ -331,6 +365,11 @@ class Rbpkg::Repo
     cmd! "cd #{sh_quote(src_dir!)} && git pull && git submodule update"
   end
 
+  def pull_possible?
+    File.directory?(src_dir) &&
+      system("git -C #{sh_quote(src_dir)} symbolic-ref HEAD >/dev/null 2>&1")
+  end
+
   @required_by = nil
   def required_by
     @required_by || @required_by =
@@ -430,7 +469,7 @@ class Rbpkg::Repo
 
   def upgrade
     verbose 3, "repo(#{name.inspect}).upgrade"
-    pull
+    pull if pull_possible?
     return if installed_version == version
     package
     uninstall
diff --git a/share/icon/64x64/ci.png b/share/icon/64x64/ci.png
new file mode 100644
index 0000000..9b8a020
Binary files /dev/null and b/share/icon/64x64/ci.png differ
diff --git a/share/icon/64x64/status_ko.png b/share/icon/64x64/status_ko.png
new file mode 100644
index 0000000..a0d60d3
Binary files /dev/null and b/share/icon/64x64/status_ko.png differ
diff --git a/share/icon/64x64/status_ok.png b/share/icon/64x64/status_ok.png
new file mode 100644
index 0000000..a189b4c
Binary files /dev/null and b/share/icon/64x64/status_ok.png differ
diff --git a/share/icon/64x64/status_running.png b/share/icon/64x64/status_running.png
new file mode 100644
index 0000000..6f4e27e
Binary files /dev/null and b/share/icon/64x64/status_running.png differ
diff --git a/share/icon/64x64/status_unknown.png b/share/icon/64x64/status_unknown.png
new file mode 100644
index 0000000..62703fe
Binary files /dev/null and b/share/icon/64x64/status_unknown.png differ