Fix for Pike/Queens Jobs
[releng/builder.git] / scripts / branch_cut / branch_cut.awk
1 #!/usr/bin/awk -f
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 #
11 ##############################################################################
12
13 BEGIN {
14     new_tag                     = new_reltag       # new release tag
15     curr_tag                    = curr_reltag      # current release tag
16     prev_tag                    = prev_reltag      # previous release tag
17
18     new_release                 = tolower(new_tag)
19     curr_release                = tolower(curr_tag)
20     prev_release                = tolower(prev_tag)
21
22     ws = "[\\t ]*"                                 # white-spaces
23     startpat = "^" ws "- project:"                 # start pattern
24     endpat = startpat                              # end pattern
25     op = "^" ws "---" ws "$"                       # match files starts with "---"
26
27     next_release_tag            = "^" ws "next-release-tag: '{stream}'"
28     master                      = "'master'"
29     new_branch                  = "'stable/" new_release "'"
30     curr_branch                 = "'stable/" curr_release "'"
31     prev_branch                 = "'stable/" prev_release "'"
32
33     # replace block to add new release
34     new_rel_yaml_tag            = "- " new_release ":";
35     br_master_yaml_tag          = "    branch: 'master'";
36     jre_yaml_tag                = "    jre: 'openjdk8'";
37     curr_rel_yaml_tag           = "- " curr_release ":";
38     br_stable_curr_yaml_tag     = "    branch: 'stable/" curr_release "'";
39
40     # replace block for autorelease-projects
41     #new_rel_yaml_tag           = "- " new_release ":";
42     next_rel_tag_new_yaml_tag   = "    next-release-tag: '{stream}'";
43     #br_master_yaml_tag         = "    branch: 'master'";
44     jdk_yaml_tag                = "    jdk: 'openjdk8'";
45     intg_test_yaml_tag          = "    integration-test: " new_release;
46     #curr_rel_yaml_tag          = "- " curr_release ":";
47     next_rel_tag_curr_yaml_tag  = "    next-release-tag: '{stream}'";
48     #br_stable_curr_yaml_tag    = "    branch: 'stable/" curr_release "'";
49
50     # search patterns
51     smaster = "^" ws "- master:"
52     sstream = "^" ws "stream:"
53     srelease = "^" ws "- " curr_release ":"
54     snext_release_tag = "^" ws "next-release-tag:"
55     #if (l ~ next_release_tag) { next_release_tag = 1; continue; }
56     sbranch = "^" ws "branch: " master
57     sfunctionality = "^" ws "functionality:"
58
59     debug = 0                                   # set to 1 to print debug info
60     file_format = 2                             # project stream format
61
62     release_found = 0
63     stream_found = 0
64     nrt_found = 0
65     func_found = 0
66 }
67
68 {
69     # exit if release info is not available
70     if ((length(new_release) == 0 || length(curr_release) == 0)) {
71         exit;
72     }
73
74     # read all lines of the file into an array
75     file[NR] = $0
76 }
77
78 END {
79     n = NR                                      # total number of lines
80     find_blks()                                 # gets number of blocks between start/end pattern
81     process_blk(arr_bs[1], arr_be[1], 1)        # pass start and end of each block and process first block
82     update_file(arr_be[1])                      # write processed content
83
84     if (debug) {
85         print "number of blocks="nb;
86         print "total records in file[]="length(f);
87         print "size of firstblk[]="length(firstblk);
88         print "size of newblk[]="length(newblk);
89         print "size of oldmaster[]="length(oldmaster);
90         print "size of newblk[]="length(newblk);
91     }
92 }
93
94 function find_blks(   i, l, bs, be) {
95     for (i = 1; i <= n; i++) {
96         l = file[i]
97         if (l ~ startpat) project = 1                        # start pattern
98         if (bs > be && l ~ endpat) arr_be[++be] = i - 1      # block end
99         if (           l ~ startpat) arr_bs[++bs] = i - 1    # block start
100     }
101     nb = be
102
103     # to handle files with single blocks
104     if (nb == 0 && length(file) > 1 && project == 1) {
105         nb = 1
106         arr_bs[1] = 1                               # start after line '---'
107         arr_be[1] = length(file)                    # set length of the file
108     }
109
110     if (debug) {
111         for (i = 1; i < nb; i++)
112             print "find_blks: nb=" nb " arr_bs[" i "]="arr_bs[i]" arr_be[" i "]="arr_be[i];
113     }
114 }
115
116 function process_blk(bs, be, bn,   i, l) {
117     if (debug) {
118         print "process_blk: bn=" bn ", bs=" bs " ,be=" be
119     }
120
121     # get the first block
122     for (i = bs + 1; i <= be ; i++) {
123         l = file[i]
124         # determine file format
125         if (l ~ /stream:/) {
126             x=index(l,":")
127             s = substr(l, x+2, length(l) - x)
128             if (s == curr_release || s == new_release) {
129                 file_format = 1
130             } else if (length(s) == 0 ) {
131                 file_format = 0
132             }
133         }
134         firstblk[++nex] = l
135     }
136
137     if (debug) {
138         print "process_blk: stream='" s "' length(s)=" length(s)" file_format='" file_format "'"
139     }
140
141     # Handle single stream format
142     if (file_format == 1) {
143         # create new block to be inserted
144         for (i = 1; i <= length(firstblk); i++) {
145             l = firstblk[i]
146             if (l ~ /name:|stream:/) sub(curr_release, new_release, l)
147             newblk[++nex1] = l
148         }
149         # re-create old block and change master to stable/branch
150         for (i = 1; i <= length(firstblk)-1; i++) {
151             l = firstblk[i]
152             if (l ~ /branch:/) sub(master, curr_branch, l)
153             oldmaster[++nex2] = l
154         }
155     } else if (file_format == 0) {
156         # Handle multi-stream format
157         for (i = 1; i <= length(firstblk)-1; i++) {
158             l = firstblk[i]
159             if (l ~ sstream) { stream_found = 1; }
160             if (l ~ srelease) { release_found = 1; indent = substr(l, 1, index(l, "-")-1); continue; }
161             if (l ~ sfunctionality) { func_found = 1; }
162             if (l ~ snext_release_tag) { nrt_found = 1; }
163             if (l ~ sbranch) {
164                 # append lines
165                 if (stream_found && release_found && !nrt_found) {
166                     newblk[++nex3] = indent new_rel_yaml_tag;
167                     newblk[++nex3] = indent br_master_yaml_tag;
168                     # set 'jdk' macro for patch-test jobs
169                     if (!func_found) {
170                         newblk[++nex3] = indent jdk_yaml_tag;
171                     } else {
172                         newblk[++nex3] = indent jre_yaml_tag;
173                     }
174                     newblk[++nex3] = indent curr_rel_yaml_tag;
175                     newblk[++nex3] = indent br_stable_curr_yaml_tag;
176                     stream_found = 0;
177                     release_found = 0;
178                     func_found = 0;
179                     continue;
180                 }
181                 if (stream_found && release_found && nrt_found) {
182                     newblk[++nex3] = indent new_rel_yaml_tag;
183                     newblk[++nex3] = indent next_rel_tag_new_yaml_tag;
184                     newblk[++nex3] = indent br_master_yaml_tag;
185                     newblk[++nex3] = indent jdk_yaml_tag;
186                     newblk[++nex3] = indent intg_test_yaml_tag;
187                     newblk[++nex3] = indent curr_rel_yaml_tag;
188                     newblk[++nex3] = indent next_rel_tag_curr_yaml_tag;
189                     newblk[++nex3] = indent br_stable_curr_yaml_tag;
190                     stream_found = 0; release_found = 0; nrt_found=0;
191                     continue;
192                 }
193             }
194             newblk[++nex3] = l
195
196             if (debug) {
197                 print "process_blk: append(newblk[]) : stream="stream" release_found="release_found
198             }
199         }
200     } else {
201         # exit on unknown file format
202         exit;
203     }
204 }
205
206 function update_file(be,   i, j, l) {
207     i = 1
208     # handle lines before "---"
209     while (i <= n) {
210         print l = file[i++]
211         if (l ~ op) break
212     }
213
214     if (debug) {
215         print "writing master block"
216     }
217
218     # Handle single stream format
219     if (file_format == 1) {
220         for (j = 1; j <= nex1; j++)                   # write new branch block
221             print newblk[j]
222
223         if (debug) {
224             print "writing stable block"
225         }
226
227         for (j = 1; j <= nex2; j++)                   # write updated branch block
228             print oldmaster[j]
229
230     # Handle multi-stream format
231     } else if (file_format == 0) {
232         # print the first block
233         for (j = 1; j <= nex3; j++)                   # write multi-stream block
234             print newblk[j]
235     }
236
237     if (debug) {
238         print "writing rest of the file"
239     }
240
241     while (be <= n) {                                 # write rest of the file
242         print file[be++]
243     }
244 }