Hog Hog2024.2-4
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 set OldPath [pwd]
20 set TclPath [file dirname [info script]]/..
21 set repo_path [file normalize "$TclPath/../.."]
22 source $TclPath/hog.tcl
23 
24 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>"
25 
26 if { [llength $argv] < 7 } {
27  Msg Info [cmdline::usage $usage]
28  cd $OldPath
29  return
30 }
31 
32 set result [catch {package require json} JsonFound]
33 if {"$result" != "0"} {
34  Msg CriticalWarning "Cannot find JSON package equal or higher than 1.0.\n $JsonFound\n Exiting"
35  return -1
36 }
37 
38 set push_token [lindex $argv 0]
39 set api_url [lindex $argv 1]
40 set project_id [lindex $argv 2]
41 set project_url [lindex $argv 3]
42 set gitlab_url [lindex $argv 4]
43 set project [lindex $argv 5]
44 set ext_path [lindex $argv 6]
45 
46 set resources [dict create "LUTs" "LUTs" "Registers" "FFs" "Block" "BRAM" "URAM" "URAM" "DSPs" "DSPs"]
47 set ver [ GetProjectVersion $repo_path/Top/$project $repo_path $ext_path 0 ]
48 
49 set accumulated ""
50 set current_badges []
51 set page 1
52 
53 while {1} {
54  lassign [ExecuteRet curl --header "PRIVATE-TOKEN: $push_token" "$api_url/projects/${project_id}/badges?page=$page" --request GET] ret content
55  if {[llength $content] > 0 && $page < 100} {
56  set accumulated "$accumulated$content"
57  incr page
58  } else {
59  set current_badges [json::json2dict $accumulated]
60  break
61  }
62 }
63 
64 
65 if {[catch {glob -types d $repo_path/bin/$project-${ver} } prj_dir]} {
66  Msg CriticalWarning "Cannot find $project binaries in artifacts"
67  return
68 }
69 
70 cd $prj_dir
71 if {[file exists utilization.txt]} {
72  set fp [open utilization.txt]
73  set lines [split [read $fp] "\n"]
74  close $fp
75  set new_badges [dict create]
76  set prj_name [string map {/ _} $project]
77 
78  set res_value ""
79  set usage_dict [dict create]
80  # Resource Badges
81  foreach line $lines {
82  set str [string map {| ""} $line]
83  set str [string map {"<" ""} $str]
84  set str [string trim $str]
85 
86  set usage [lindex [split $str] end]
87  foreach res [dict keys $resources] {
88  if {[string first $res $str] > -1} {
89  set res_name [dict get $resources $res]
90  dict set usage_dict $res_name $usage
91  }
92  }
93  }
94  foreach res [dict keys $usage_dict] {
95  set usage [DictGet $usage_dict $res]
96  append res_value $res ": $usage\% "
97  }
98 
99  Execute anybadge -l "$project-$ver" -v "$res_value" -f $prj_name.svg --color=blue -o;
100  dict set new_badges "$prj_name" "$prj_name"
101 
102  # Timing Badge
103  if {[file exists timing_error.txt]} {
104  Execute anybadge -l timing -v "FAILED" -f timing-$prj_name.svg --color=red -o;
105  } elseif {[file exists timing_ok.txt]} {
106  Execute anybadge -l timing -v "OK" -f timing-$prj_name.svg --color=green -o;
107  } else {
108  Execute anybadge -l timing -v "UNKNOWN" -f timing-$prj_name.svg --color=orange -o;
109  }
110  dict set new_badges "timing-$prj_name" "timing-$prj_name"
111 
112 
113 
114  foreach badge_name [dict keys $new_badges] {
115  set badge_found 0
116  Msg Info "Uploading badge image $badge_name.svg ...."
117  lassign [ExecuteRet curl --request POST --header "PRIVATE-TOKEN: ${push_token}" --form "file=@$badge_name.svg" $api_url/projects/$project_id/uploads] ret content
118  set image_url [ParseJSON $content full_path]
119  set image_url $gitlab_url/$image_url
120  foreach badge $current_badges {
121  set current_badge_name [dict get $badge "name"]
122  set badge_id [dict get $badge "id"]
123  if {$current_badge_name == $badge_name} {
124  set badge_found 1
125  Msg Info "Badge $badge_name exists, updating it..."
126  Execute curl --header "PRIVATE-TOKEN: $push_token" "$api_url/projects/${project_id}/badges/$badge_id" --request PUT --data "image_url=$image_url"
127  break
128  }
129  }
130  if {$badge_found == 0} {
131  Msg Info "Badge $badge_name does not exist yet. Creating it..."
132  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"
133  }
134  }
135 }
136 
137 cd $OldPath