Merge "Use the right disable-job parameter"
[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: 'openjdk11'";
37     java_version_yaml_tag       = "    java-version: 'openjdk11'";
38     curr_rel_yaml_tag           = "- " curr_release ":";
39     br_stable_curr_yaml_tag     = "    branch: 'stable/" curr_release "'";
40
41     # replace block for autorelease-projects
42     #new_rel_yaml_tag           = "- " new_release ":";
43     next_rel_tag_new_yaml_tag   = "    next-release-tag: '{stream}'";
44     #br_master_yaml_tag         = "    branch: 'master'";
45     jdk_yaml_tag                = "    jdk: 'openjdk8'";
46     intg_test_yaml_tag          = "    integration-test: " new_release;
47     extra_mvn_opts_tag          = "    extra-mvn-opts: -Dsft.heap.max=4g"
48     #curr_rel_yaml_tag          = "- " curr_release ":";
49     next_rel_tag_curr_yaml_tag  = "    next-release-tag: '{stream}'";
50     #br_stable_curr_yaml_tag    = "    branch: 'stable/" curr_release "'";
51
52     # search patterns
53     smaster = "^" ws "- master:"
54     sstream = "^" ws "stream:"
55     srelease = "^" ws "- " curr_release ":"
56     snext_release_tag = "^" ws "next-release-tag:"
57     #if (l ~ next_release_tag) { next_release_tag = 1; continue; }
58     sbranch = "^" ws "branch: " master
59     sfunctionality = "^" ws "functionality:"
60
61     debug = 0                                   # set to 1 to print debug info
62     file_format = 2                             # project stream format
63
64     release_found = 0
65     stream_found = 0
66     nrt_found = 0
67     func_found = 0
68 }
69
70 {
71     # exit if release info is not available
72     if ((length(new_release) == 0 || length(curr_release) == 0)) {
73         exit;
74     }
75
76     # read all lines of the file into an array
77     file[NR] = $0
78 }
79
80 END {
81     n = NR                                      # total number of lines
82     find_blks()                                 # gets number of blocks between start/end pattern
83     process_blk(arr_bs[1], arr_be[1], 1)        # pass start and end of each block and process first block
84     update_file(arr_be[1])                      # write processed content
85
86     if (debug) {
87         print "number of blocks="nb;
88         print "total records in file[]="length(f);
89         print "size of firstblk[]="length(firstblk);
90         print "size of newblk[]="length(newblk);
91         print "size of oldmaster[]="length(oldmaster);
92         print "size of newblk[]="length(newblk);
93     }
94 }
95
96 function find_blks(   i, l, bs, be) {
97     for (i = 1; i <= n; i++) {
98         l = file[i]
99         if (l ~ startpat) project = 1                        # start pattern
100         if (bs > be && l ~ endpat) arr_be[++be] = i - 1      # block end
101         if (           l ~ startpat) arr_bs[++bs] = i - 1    # block start
102     }
103     nb = be
104
105     # to handle files with single blocks
106     if (nb == 0 && length(file) > 1 && project == 1) {
107         nb = 1
108         arr_bs[1] = 1                               # start after line '---'
109         arr_be[1] = length(file)                    # set length of the file
110     }
111
112     if (debug) {
113         for (i = 1; i < nb; i++)
114             print "find_blks: nb=" nb " arr_bs[" i "]="arr_bs[i]" arr_be[" i "]="arr_be[i];
115     }
116 }
117
118 function process_blk(bs, be, bn,   i, l) {
119     if (debug) {
120         print "process_blk: bn=" bn ", bs=" bs " ,be=" be
121     }
122
123     # get the first block
124     for (i = bs + 1; i <= be ; i++) {
125         l = file[i]
126         # determine file format
127         if (l ~ /stream:/) {
128             x=index(l,":")
129             s = substr(l, x+2, length(l) - x)
130             if (s == curr_release || s == new_release) {
131                 file_format = 1
132             } else if (length(s) == 0 ) {
133                 file_format = 0
134             }
135         }
136         firstblk[++nex] = l
137     }
138
139     if (debug) {
140         print "process_blk: stream='" s "' length(s)=" length(s)" file_format='" file_format "'"
141     }
142
143     # Handle single stream format
144     if (file_format == 1) {
145         # create new block to be inserted
146         for (i = 1; i <= length(firstblk); i++) {
147             l = firstblk[i]
148             if (l ~ /name:|stream:/) sub(curr_release, new_release, l)
149             newblk[++nex1] = l
150         }
151         # re-create old block and change master to stable/branch
152         for (i = 1; i <= length(firstblk)-1; i++) {
153             l = firstblk[i]
154             if (l ~ /branch:/) sub(master, curr_branch, l)
155             oldmaster[++nex2] = l
156         }
157     } else if (file_format == 0) {
158         # Handle multi-stream format
159         for (i = 1; i <= length(firstblk)-1; i++) {
160             l = firstblk[i]
161             if (l ~ sstream) { stream_found = 1; }
162             if (l ~ srelease) { release_found = 1; indent = substr(l, 1, index(l, "-")-1); continue; }
163             if (l ~ sfunctionality) { func_found = 1; }
164             if (l ~ snext_release_tag) { nrt_found = 1; }
165             if (l ~ sbranch) {
166                 # append lines
167                 if (stream_found && release_found && !nrt_found) {
168                     newblk[++nex3] = indent new_rel_yaml_tag;
169                     newblk[++nex3] = indent br_master_yaml_tag;
170                     newblk[++nex3] = indent java_version_yaml_tag;
171                     newblk[++nex3] = indent curr_rel_yaml_tag;
172                     newblk[++nex3] = indent br_stable_curr_yaml_tag;
173                     stream_found = 0;
174                     release_found = 0;
175                     func_found = 0;
176                     continue;
177                 }
178                 if (stream_found && release_found && nrt_found) {
179                     newblk[++nex3] = indent new_rel_yaml_tag;
180                     newblk[++nex3] = indent next_rel_tag_new_yaml_tag;
181                     newblk[++nex3] = indent br_master_yaml_tag;
182                     newblk[++nex3] = indent intg_test_yaml_tag;
183                     newblk[++nex3] = indent extra_mvn_opts_tag;
184                     newblk[++nex3] = indent curr_rel_yaml_tag;
185                     newblk[++nex3] = indent next_rel_tag_curr_yaml_tag;
186                     newblk[++nex3] = indent br_stable_curr_yaml_tag;
187                     stream_found = 0; release_found = 0; nrt_found=0;
188                     continue;
189                 }
190             }
191             newblk[++nex3] = l
192
193             if (debug) {
194                 print "process_blk: append(newblk[]) : stream="stream" release_found="release_found
195             }
196         }
197     } else {
198         # exit on unknown file format
199         exit;
200     }
201 }
202
203 function update_file(be,   i, j, l) {
204     i = 1
205     # handle lines before "---"
206     while (i <= n) {
207         print l = file[i++]
208         if (l ~ op) break
209     }
210
211     if (debug) {
212         print "writing master block"
213     }
214
215     # Handle single stream format
216     if (file_format == 1) {
217         for (j = 1; j <= nex1; j++)                   # write new branch block
218             print newblk[j]
219
220         if (debug) {
221             print "writing stable block"
222         }
223
224         for (j = 1; j <= nex2; j++)                   # write updated branch block
225             print oldmaster[j]
226
227     # Handle multi-stream format
228     } else if (file_format == 0) {
229         # print the first block
230         for (j = 1; j <= nex3; j++)                   # write multi-stream block
231             print newblk[j]
232     }
233
234     if (debug) {
235         print "writing rest of the file"
236     }
237
238     while (be <= n) {                                 # write rest of the file
239         print file[be++]
240     }
241 }