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