Hog Hog2025.1-4
create_badges.tcl
Go to the documentation of this file.
1 #!/usr/bin/env tclsh
2 # Copyright 2018-2025 The University of Birmingham
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 # @file
17 # Create and uploads GitLab badges for chosen projects
18 
19 
20 proc generate_prj_badge {prj_name ver color file} {
21  set font_size 11.0
22  set max_characters 20.0
23  puts [string length $prj_name]
24  if { [expr {[string length $prj_name] > $max_characters}] } {
25  set scaling_factor [expr { $max_characters / [string length $prj_name] } ]
26  puts $font_size
27  set font_size [expr {ceil($scaling_factor*$font_size)}]
28  puts $scaling_factor
29  puts $font_size
30  }
31 
32  set svg_content "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
33 <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"250\" height=\"20\">
34  <linearGradient id=\"b\" x2=\"0\" y2=\"100%\">
35  <stop offset=\"0\" stop-color=\"#bbb\" stop-opacity=\".1\"/>
36  <stop offset=\"1\" stop-opacity=\".1\"/>
37  </linearGradient>
38  <mask id=\"hog_prj_badge\">
39  <rect width=\"250\" height=\"20\" rx=\"10\" fill=\"#fff\"/>
40  </mask>
41  <g mask=\"url(#hog_prj_badge)\">
42  <path fill=\"$color\" d=\"M0 0h250v20H0z\"/>
43  <path fill=\"#262626\" d=\"M160 0h90v20H160z\"/>
44  <path fill=\"#262626\" d=\"M250,20 a1,1 0 0,0 0,-16\"/>
45  <path fill=\"url(#b)\" d=\"M0 0h250v20H0z\"/>
46  </g>
47  <g fill=\"#fff\" text-anchor=\"middle\" font-family=\"DejaVu Sans,Verdana,Geneva,sans-serif\" font-size=\"$font_size\">
48  <text x=\"80\" y=\"14\">$prj_name</text>
49  </g>
50  <g fill=\"#fff\" text-anchor=\"middle\" font-family=\"DejaVu Sans,Verdana,Geneva,sans-serif\" font-size=\"11\">
51  <text x=\"205\" y=\"14\">$ver</text>
52  </g>
53 </svg>"
54 
55  if {[catch {
56  set fh [open $file w]
57  puts $fh $svg_content
58  close $fh
59  } error_msg]} {
60  error "Failed to write to file: $error_msg"
61  }
62 }
63 
64 proc generate_res_badge {res res_value color file} {
65  set svg_content "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
66 <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"120\" height=\"20\">
67  <linearGradient id=\"b\" x2=\"0\" y2=\"100%\">
68  <stop offset=\"0\" stop-color=\"#bbb\" stop-opacity=\".1\"/>
69  <stop offset=\"1\" stop-opacity=\".1\"/>
70  </linearGradient>
71  <mask id=\"hog_res_badge\">
72  <rect width=\"120\" height=\"20\" rx=\"3\" fill=\"#fff\"/>
73  </mask>
74  <g mask=\"url(#hog_res_badge)\">
75  <path fill=\"#555\" d=\"M0 0h60v20H0z\"/>
76  <path fill=\"$color\" d=\"M60 0h60v20H60z\"/>
77  <path fill=\"url(#b)\" d=\"M0 0h120v20H0z\"/>
78  </g>
79  <g fill=\"#fff\" text-anchor=\"middle\" font-family=\"DejaVu Sans,Verdana,Geneva,sans-serif\" font-size=\"11\">
80  <text x=\"30\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">$res</text>
81  <text x=\"30\" y=\"14\">$res</text>
82  </g>
83  <g fill=\"#fff\" text-anchor=\"middle\" font-family=\"DejaVu Sans,Verdana,Geneva,sans-serif\" font-size=\"11\">
84  <text x=\"90\" y=\"15\" fill=\"#010101\" fill-opacity=\".3\">$res_value</text>
85  <text x=\"90\" y=\"14\">$res_value</text>
86  </g>
87 </svg>"
88 
89  if {[catch {
90  set fh [open $file w]
91  puts $fh $svg_content
92  close $fh
93  } error_msg]} {
94  error "Failed to write to file: $error_msg"
95  }
96 
97 }
98 
99 set OldPath [pwd]
100 set TclPath [file dirname [info script]]/..
101 set repo_path [file normalize "$TclPath/../.."]
102 source $TclPath/hog.tcl
103 
104 set usage "- CI script that creates GitLab badges with utilisation and timing results for a chosen Hog project \n USAGE: $::argv0 <push token> <Gitlab api url> <Gitlab project id> <Gitlab project url> <GitLab Server URL> <Hog project> <ext_path>"
105 
106 if { [llength $argv] < 7 } {
107  Msg Info [cmdline::usage $usage]
108  cd $OldPath
109  return
110 }
111 
112 set result [catch {package require json} JsonFound]
113 if {"$result" != "0"} {
114  Msg CriticalWarning "Cannot find JSON package equal or higher than 1.0.\n $JsonFound\n Exiting"
115  return -1
116 }
117 
118 set push_token [lindex $argv 0]
119 set api_url [lindex $argv 1]
120 set project_id [lindex $argv 2]
121 set project_url [lindex $argv 3]
122 set gitlab_url [lindex $argv 4]
123 set project [lindex $argv 5]
124 set ext_path [lindex $argv 6]
125 
126 set resources [dict create "LUTs" "LUTs" "Registers" "FFs" "Block" "BRAM" "URAM" "URAM" "DSPs" "DSPs"]
127 set ver [ GetProjectVersion $repo_path/Top/$project $repo_path $ext_path 0 ]
128 
129 set accumulated ""
130 set current_badges [dict create]
131 set page 0
132 
133 Msg Info "Retrieving current badges..."
134 while {1} {
135  lassign [ExecuteRet curl --header "PRIVATE-TOKEN: $push_token" "$api_url/projects/${project_id}/badges?page=$page" --request GET] ret content
136  set content_dict [json::json2dict $content]
137  if {[llength $content_dict] > 0} {
138  foreach it $content_dict {
139  dict set current_badges [DictGet $it name] [DictGet $it id]
140  }
141  incr page
142  } else {
143  break
144  }
145 }
146 
147 
148 if {[catch {glob -types d $repo_path/bin/$project-${ver} } prj_dir]} {
149  Msg CriticalWarning "Cannot find $project binaries in artifacts"
150  return
151 }
152 
153 cd $prj_dir
154 if {[file exists utilization.txt]} {
155  set fp [open utilization.txt]
156  set lines [split [read $fp] "\n"]
157  close $fp
158  set new_badges [dict create]
159  set prj_name [string map {/ _} $project]
160 
161  # Timing Badge
162  if {[file exists timing_error.txt]} {
163  generate_prj_badge $prj_name $ver "#E05D44" "timing-$prj_name.svg"
164  } elseif {[file exists timing_ok.txt]} {
165  generate_prj_badge $prj_name $ver "#006400" "timing-$prj_name.svg"
166  } else {
167  generate_prj_badge $prj_name $ver "#696969" "timing-$prj_name.svg"
168  }
169  dict set new_badges "timing-$prj_name" "timing-$prj_name"
170 
171  set usage_dict [dict create]
172  # Resource Badges
173  foreach line $lines {
174  set str [string map {| ""} $line]
175  set str [string map {"<" ""} $str]
176  set str [string trim $str]
177 
178  set usage [lindex [split $str] end]
179  foreach res [dict keys $resources] {
180  if {[string first $res $str] > -1} {
181  set res_name [dict get $resources $res]
182  dict set usage_dict $res_name $usage
183  }
184  }
185  }
186  foreach res [dict keys $usage_dict] {
187  set usage [DictGet $usage_dict $res]
188  set res_value "$usage\% "
189  if {[ expr {$usage < 50.0} ]} {
190  generate_res_badge $res $res_value "#90CAF9" "$res-$prj_name.svg"
191  } elseif {[ expr {$usage < 80.0} ]} {
192  generate_res_badge $res $res_value "#1565C0" "$res-$prj_name.svg"
193  } else {
194  generate_res_badge $res $res_value "#0D2B6B" "$res-$prj_name.svg"
195  }
196  dict set new_badges "$res-$prj_name" "$res-$prj_name"
197  }
198 
199  foreach badge_name [dict keys $new_badges] {
200  set badge_found 0
201  Msg Info "Uploading badge image $badge_name.svg ...."
202  lassign [ExecuteRet curl --request POST --header "PRIVATE-TOKEN: ${push_token}" --form "file=@$badge_name.svg" $api_url/projects/$project_id/uploads] ret content
203  set image_url [ParseJSON $content full_path]
204  set image_url $gitlab_url/$image_url
205  if {[dict exists $current_badges $badge_name]} {
206  Msg Info "Badge $badge_name exists, updating it..."
207  set badge_id [DictGet $current_badges $badge_name]
208  Execute curl --header "PRIVATE-TOKEN: $push_token" "$api_url/projects/${project_id}/badges/$badge_id" --request PUT --data "image_url=$image_url"
209  } else {
210  Msg Info "Badge $badge_name does not exist yet. Creating it..."
211  Execute curl --header "PRIVATE-TOKEN: $push_token" --request POST --data "link_url=$project_url/-/releases&image_url=$image_url&name=$badge_name" "$api_url/projects/$project_id/badges"
212  }
213  }
214 }
215 
216 cd $OldPath