Hog Hog2026.2-1
generate_yaml.tcl
Go to the documentation of this file.
1 #!/usr/bin/env tclsh
2 # Copyright 2018-2026 The University of Birmingham
3 # Copyright 2018-2026 Max-Planck-Institute for Physics
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 #parsing command options
18 
19 ##nagelfar variable CI_STAGES
20 ##nagelfar variable CI_PROPS
21 
22 if {[catch {package require yaml} ERROR]} {
23  puts "$ERROR\n If you are running this script on tclsh, you can fix this by installing 'tcllib'"
24  return
25 }
26 
27 if {[catch {package require cmdline} ERROR]} {
28  puts "$ERROR\n If you are running this script on tclsh, you can fix this by installing 'tcllib'"
29  return
30 }
31 
32 set parameters {
33  {runall "If set, it will generate a gitlab-ci.yml file for all projects in the Top folder, \
34  even if it has not been modified with respect to the target branch."}
35  {static "Normally the content of the hog-child.yml file is added at the beginning of the generated yml file. If this flag is set, this will not be done."}
36  {external_path.arg "" "Path for external files not stored in the git repository."}
37 }
38 
39 set usage "Generate a gitlab-ci.yml config file for the child pipeline - USAGE: generate_yaml.tcl \[options\]"
40 
41 set old_path [pwd]
42 set tcl_path [file normalize "[file dirname [info script]]/.."]
43 
44 set repo_path [file normalize $tcl_path/../..]
45 source $tcl_path/hog.tcl
46 
47 if {[catch {array set options [cmdline::getoptions ::argv $parameters $usage]}]} {
48  Msg Info [cmdline::usage $parameters $usage]
49  exit 1
50 }
51 
52 if {$options(runall) == 1} {
53  set runall 1
54 } else {
55  set runall 0
56 }
57 if {$options(static) == 1} {
58  set static 1
59  set runall 1
60 } else {
61  set static 0
62 }
63 
64 if {$options(external_path) != ""} {
65  set ext_path $options(external_path)
66  Msg Info "External path set to $ext_path"
67 } else {
68  set ext_path ""
69 }
70 
71 
72 set stage_list $CI_STAGES
73 set prop_list $CI_PROPS
74 
75 if {$static == 1} {
76  if {[file exists "$repo_path/.gitlab-ci.yml"]} {
77  set created_yml "$repo_path/new_gitlab-ci.yml"
78  Msg Warning "$repo_path/.gitlab-ci.yml, will create (and possibly repleace) $created_yml, please rename it if you want Hog-CI to work."
79  } else {
80  set created_yml "$repo_path/.gitlab-ci.yml"
81  }
82  Msg Info "Creating new file $created_yml..."
83  set fp [open $created_yml w]
84 
85  Msg Info "Evaluating the current version of Hog to use in the ref in the yml file..."
86  cd $tcl_path
87  set ref [Git describe]
88  cd $old_path
89  # adding include hog.yml and ref
90  #set outer [huddle create "inculde" [huddle list [huddle string "project: 'hog/Hog'" "file" "'/hog.yml'" "ref" "'$ref'" ]]]
91  #puts $fp [ string trimleft [ yaml::huddle2yaml $outer ] "-" ]
92  puts $fp "include:\n - project: 'hog/Hog'\n file: 'hog.yml'\n ref: '$ref'\n"
93 } else {
94  set created_yml "$repo_path/generated-config.yml"
95  Msg Info "Copying $repo_path/Hog/YAML/hog-common.yml to $created_yml..."
96  file copy -force $repo_path/Hog/YAML/hog-common.yml $created_yml
97  set fp [open $created_yml a]
98  Msg Info "Copying $repo_path/Hog/YAML/hog-child.yml to $created_yml..."
99  set fp2 [open "$repo_path/Hog/YAML/hog-child.yml" r]
100  set file_data [read $fp2]
101  close $fp2
102  regsub -all {\-\-\-} $file_data "" file_data
103  puts $fp $file_data
104  puts $fp "\n"
105  if {[file exists "$repo_path/hog-ci-users.yml"] == 1} {
106  Msg Info "Copying $repo_path/hog-ci-users.yml to $created_yml..."
107  set fp3 [open "$repo_path/hog-ci-users.yml" r]
108  set file_data [read $fp3]
109  close $fp3
110  regsub -all {\-\-\-} $file_data "" file_data
111  puts $fp $file_data
112  puts $fp "\n"
113  }
114 }
115 
116 set projects_list [SearchHogProjects $repo_path/Top]
117 foreach proj $projects_list {
118  set proj_name [file tail $proj]
119  set dir $repo_path/Top/$proj
120  set ver [GetProjectVersion $dir $repo_path $ext_path 1]
121  set no_ver_check 0
122 
123  if {[file exists "$dir/ci.conf"] == 1} {
124  Msg Info "Foung CI configuration file $dir/ci.conf, reading configuration for $proj..."
125  set ci_confs [ReadConf $dir/ci.conf]
126  set f [open $dir/ci.conf "r"]
127  set line [gets $f]
128  close $f
129  if {[string first "NO_VER_CHECK" $line]} {
130  set no_ver_check 1
131  }
132  }
133 
134  if {$ver == 0 || $ver == -1 || $runall == 1 || $no_ver_check == 1} {
135  if {$runall == 0 && $no_ver_check == 0} {
136  Msg Info "$proj was modified, adding it to CI..."
137  } else {
138  Msg Info "$proj is set to always run, adding it to CI..."
139  }
140  if {[file exists "$dir/ci.conf"] == 1} {
141  Msg Info "Found CI configuration file $dir/ci.conf, reading configuration for $proj..."
142  puts $fp [WriteGitLabCIYAML $proj $dir/ci.conf]
143  } else {
144  Msg Info "No CI configuration file found ($dir/ci.conf) for $proj, creating all jobs..."
145  puts $fp [WriteGitLabCIYAML $proj]
146  }
147  } else {
148  Msg Info "$proj was not modified since version: $ver, skipping."
149  #Here we should provide the link to the tag in $ver
150  }
151 }
152 close $fp
153 Msg Info "$created_yml generated correctly."