Hog v10.5.0
check_proj_ver.tcl
Go to the documentation of this file.
1 #!/usr/bin/env tclsh
2 # Copyright 2018-2026 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 set tcl_path [file normalize "[file dirname [info script]]/.."]
17 set repo_path [file normalize "$tcl_path/../.."]
18 
19 source $tcl_path/hog.tcl
20 
21 # Import tcllib for libero
22 if {[IsLibero]} {
23  if {[info exists env(HOG_TCLLIB_PATH)]} {
24  lappend auto_path $env(HOG_TCLLIB_PATH)
25  } else {
26  puts "ERROR: To run Hog with Microsemi Libero SoC, you need to define the HOG_TCLLIB_PATH variable."
27  return
28  }
29 }
30 
31 #parsing command options
32 if {[catch {package require cmdline} ERROR] || [catch {package require struct::matrix} ERROR]} {
33  puts "$ERROR\n Tcllib not found. If you are running this script on tclsh, you can fix this by installing 'tcllib'"
34  return
35 }
36 
37 
38 
39 set parameters {
40  {sim "If set, checks also the version of the simulation files."}
41  {ext_path.arg "" "Sets the absolute path for the external libraries."}
42 }
43 
44 set usage "- USAGE: $::argv0 \[OPTIONS\] <project> \n. Options:"
45 
46 if {$::argc eq 0} {
47  Msg Info [cmdline::usage $parameters $usage]
48  exit 1
49 } elseif {[IsQuartus] && [catch {array set options [cmdline::getoptions quartus(args) $parameters $usage]}] || $::argc eq 0} {
50  #Quartus
51  Msg Info [cmdline::usage $parameters $usage]
52  exit 1
53 } elseif {[catch {array set options [cmdline::getoptions ::argv $parameters $usage]}]} {
54  Msg Info [cmdline::usage $parameters $usage]
55  exit 1
56  ##nagelfar ignore Unknown variable
57 }
58 
59 set project [lindex $argv 0]
60 set sim 0
61 set ext_path ""
62 set project_dir $repo_path/Top/$project
63 
64 if {$options(sim) == 1} {
65  set sim 1
66  Msg Info "Will check also the version of the simulation files..."
67 }
68 
69 if {$options(ext_path) != ""} {
70  set ext_path $options(ext_path)
71  Msg Info "External path set to $ext_path"
72 }
73 
74 set ci_run 0
75 if {[info exists env(HOG_PUSH_TOKEN)] && [info exist env(CI_PROJECT_ID)] && [info exist env(CI_API_V4_URL)] } {
76  set token $env(HOG_PUSH_TOKEN)
77  set api_url $env(CI_API_V4_URL)
78  set project_id $env(CI_PROJECT_ID)
79  set ci_run 1
80 }
81 
82 cd $repo_path
83 
84 set ver [GetProjectVersion $project_dir $repo_path $ext_path $sim]
85 
86 if {$ver == 0} {
87  Msg Info "$project was modified, continuing with the CI..."
88  if {$ci_run == 1 && ![IsQuartus] && ![IsISE]} {
89  Msg Info "Checking if the project has been already built in a previous CI run..."
90  lassign [GetRepoVersions $project_dir $repo_path] sha
91  Msg Info "Checking if project $project has been built in a previous CI run with sha $sha..."
92  set result [catch {package require json} JsonFound]
93  if {"$result" != "0"} {
94  Msg CriticalWarning "Cannot find JSON package equal or higher than 1.0.\n $JsonFound\n Exiting"
95  return
96  }
97  lassign [ExecuteRet curl --header "PRIVATE-TOKEN: $token" "$api_url/projects/$project_id/pipelines"] ret content
98  set pipeline_dict [json::json2dict $content]
99  if {[llength $pipeline_dict] > 0} {
100  foreach pip $pipeline_dict {
101  set pip_sha [DictGet $pip sha]
102  set source [DictGet $pip source]
103  if {$source == "merge_request_event" && [string first $sha $pip_sha] != -1} {
104  Msg Info "Found pipeline with sha $pip_sha for project $project"
105  set pipeline_id [DictGet $pip id]
106  # tclint-disable-next-line line-length
107  lassign [ExecuteRet curl --header "PRIVATE-TOKEN: $token" "$api_url/projects/${project_id}/pipelines/${pipeline_id}/jobs?pagination=keyset&per_page=100"] ret2 content2
108  set jobs_dict [json::json2dict $content2]
109  if {[llength $jobs_dict] > 0} {
110  foreach job $jobs_dict {
111  set job_name [DictGet $job name]
112  set job_id [DictGet $job id]
113  set artifacts [DictGet $job artifacts_file]
114  set status [DictGet $job status]
115  set current_job_name $env(CI_JOB_NAME)
116  if {$current_job_name == $job_name && $status == "success"} {
117  # tclint-disable-next-line line-length
118  lassign [ExecuteRet curl --location --output artifacts.zip --header "PRIVATE-TOKEN: $token" --url "$api_url/projects/$project_id/jobs/$job_id/artifacts"] ret3 content3
119  if {$ret3 != 0} {
120  Msg CriticalWarning "Cannot download artifacts for job $job_name with id $job_id"
121  return
122  } else {
123  lassign [ExecuteRet unzip -o $repo_path/artifacts.zip] ret_zip
124  if {$ret_zip != 0} {
125 
126  } else {
127  Msg Info "Artifacts for job $job_name with id $job_id downloaded and unzipped."
128  file mkdir $repo_path/Projects/$project
129  set fp [open "$repo_path/Projects/$project/skip.me" w+]
130  close $fp
131  exit 0
132  }
133  }
134  }
135  }
136  }
137  }
138  }
139  }
140  }
141 } elseif {$ver != -1} {
142  Msg Info "$project was not modified since version: $ver, disabling the CI..."
143  file mkdir $repo_path/Projects/$project
144  set fp [open "$repo_path/Projects/$project/skip.me" w+]
145  close $fp
146  exit 0
147 }