Hog Hog2024.2-4
get_binary_links.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 # Retrieves binary files links or creates new one to be uploaded as Releases
18 
19 
20 set OldPath [pwd]
21 set TclPath [file dirname [info script]]/..
22 set repo_path [file normalize "$TclPath/../.."]
23 source $TclPath/hog.tcl
24 
25 if {[catch {package require cmdline} ERROR]} {
26  Msg Error "$ERROR\n If you are running this script on tclsh, you can fix this by installing 'tcllib'"
27  return
28 }
29 
30 set parameters {
31  {force "Forces the creation of new project links"}
32 }
33 
34 set usage "- CI script that retrieves binary files links or creates new ones to be uploaded to a GitLab release\n USAGE: $::argv0 <tag> <ext_path> \[OPTIONS\] \n. Options:"
35 
36 if {[catch {array set options [cmdline::getoptions ::argv $parameters $usage]}] || [llength $argv] < 2 } {
37  Msg Info [cmdline::usage $parameters $usage]
38  cd $OldPath
39  return
40 }
41 
42 set tag [lindex $argv 0]
43 set ext_path [lindex $argv 1]
44 cd $repo_path
45 # Find link for every project:
46 set projects_list [SearchHogProjects $repo_path/Top]
47 
48 foreach proj $projects_list {
49  #find project version
50  set proj_name [file tail $proj]
51  set proj_dir [file dirname $proj]
52  set dir $repo_path/Top/$proj
53  set ver [ GetProjectVersion $dir $repo_path $ext_path 1 ]
54  if {"$ver"=="0" || "$ver"=="$tag" || $options(force)==1} {
55  # Project was modified in current version, upload the files
56  Msg Info "Retrieving $proj binaries and tag $tag..."
57  if {[catch {glob -types d $repo_path/bin/$proj* } prj_dir]} {
58  Msg CriticalWarning "Cannot find $proj binaries in artifacts"
59  continue
60  }
61  if { $proj_dir != "." } {
62  set proj_zip [string map {/ _} $proj_dir]
63  set files [glob -nocomplain -directory "$repo_path/zipped/" ${proj_zip}_${proj_name}-${tag}.z*]
64  } else {
65  set files [glob -nocomplain -directory "$repo_path/zipped/" ${proj_name}-${tag}.z*]
66  }
67  foreach f $files {
68  set ext [file extension $f]
69  Execute glab release upload $tag "$f#${proj}-${tag}$ext"
70  }
71  } elseif {"$ver"=="-1"} {
72  # Something went wrong...
73  Msg CriticalWarning "Something went wrong when tried to retrieve version for project $proj"
74  cd $OldPath
75  return
76  } else {
77  # Project was not modified in current version. Let's retrieve the last available link.
78  Msg Info "Retrieving existing link for $proj binaries and tag $ver"
79  lassign [ExecuteRet glab release view $ver] ret msg
80  if {$ret != 0} {
81  Msg Warning "Some problem when fetching release $ver : $msg"
82  } else {
83  set link ""
84  foreach line [split $msg "\n"] {
85  if {[string first "${proj}-${ver}.z" $line] > -1} {
86  set name [lindex [split $line] 0]
87  set link [lindex [split $line] 1]
88  set json "\[{ \"name\": \"$name\",\"url\": \"$link\",\"link_type\": \"other\" } \]"
89  Execute glab release upload $tag --assets-links=$json
90  }
91  }
92  }
93  }
94 }
95 cd $OldPath