#210 Add a script for enclosing interfaces in ifndef statements
Merged 2 years ago by zpytela. Opened 2 years ago by vmojzis.
rpms/ vmojzis/selinux-policy ifndefy  into  rawhide

file added
+80
@@ -0,0 +1,80 @@ 

+ #!/usr/bin/env python3

+ #

+ # Copyright (C) 2021 Red Hat, Inc.

+ #

+ # This library is free software; you can redistribute it and/or

+ # modify it under the terms of the GNU Lesser General Public

+ # License as published by the Free Software Foundation; either

+ # version 2.1 of the License, or (at your option) any later version.

+ #

+ # This library is distributed in the hope that it will be useful,

+ # but WITHOUT ANY WARRANTY; without even the implied warranty of

+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU

+ # Lesser General Public License for more details.

+ #

+ # You should have received a copy of the GNU Lesser General Public

+ # License along with this library.  If not, see

+ # <http://www.gnu.org/licenses/>.

+ 

+ # About:

+ # When provided with an interface file, this script outputs a new interface

+ # file, where all original interfaces are enclosed in an ifndef statement

+ # dependent on their name. All indentation inside the interface file should

+ # be converted to tabs before running this script.

+ # 

+ # Known issues:

+ # - interface already enclosed in an ifndef statement is not ignored

+ # - "`"  and "'" inside comments are not ignored

+ # - \t is always used for indentation - may result in mixture of spaces and tabs

+ 

+ import sys

+ import os

+ import re

+ 

+ if len(sys.argv) < 1:

+     print(("Usage: {} <policy>.if > <output>.if").format(sys.argv[0]), file=sys.stderr)

+     exit(os.EX_USAGE)

+ 

+ # ending index of interface

+ end = 0

+ with open(sys.argv[1], 'r') as f:

+     interfaces = f.read()

+     file_len = len(interfaces)

+ 

+     while end < file_len:

+         start = interfaces.find("interface(`", end)

+         if start < 0:

+             #no more interfaces

+             print(interfaces[end:], end ="")

+             break

+         name_end = interfaces.find("'", start+11)

+         name = interfaces[start+11:name_end]

+ 

+         # locate the end of the interface - i.e. ending apostrophe

+         reg = re.compile(r"[`']")

+         # skip the ' after interface name

+         i = name_end+1

+         # counter for the depth (` opens a new block, while ' closes it)

+         graves = 0

+         while i < file_len:

+             match = reg.search(interfaces, i)

+             if not match:

+                 print("Malformed interface: {}".format(name), file=sys.stderr)

+                 exit(1)

+ 

+             i = match.end()

+             if match.group(0) == '`':

+                 graves += 1

+             else:

+                 graves -= 1

+                 if graves == 0:

+                     break

+ 

+         # keep everything between the end of previous interface and start of a new one

+         print(interfaces[end:start], end ="")

+         # interface ends in "')" -- add 1 for the edning bracket

+         end = i+1

+         # print the whole interface enclosed in "ifndef"

+         print("ifndef(`{}',`".format(name))

+         print('\n'.join([('\t' + x) if x else x for x in interfaces[start:end].split('\n')]))

+         print("')", end ="")

Build failed. More information on how to proceed and troubleshoot errors available at https://fedoraproject.org/wiki/Zuul-based-ci

rebased onto 94f210067fff15b812ba00826385ab8c259d2cff

2 years ago

Build failed. More information on how to proceed and troubleshoot errors available at https://fedoraproject.org/wiki/Zuul-based-ci

rebased onto 5eda2e5

2 years ago

Unable to freeze job graph: Unable to modify final job <Job rpm-sti-test branches: {MatchAny:{BranchMatcher:rawhide},{BranchMatcher:main}} source: fedora-project-config/zuul.d/_jobs-base.yaml@master#3> attribute timeout=21600 with variant <Job rpm-sti-test branches: None source: fedora-zuul-jobs-config/zuul.d/projects.yaml@master#108>

rebased onto 5eda2e5

2 years ago

Unable to freeze job graph: Unable to modify final job <Job rpm-sti-test branches: {MatchAny:{BranchMatcher:rawhide},{BranchMatcher:main}} source: fedora-project-config/zuul.d/_jobs-base.yaml@master#3> attribute timeout=21600 with variant <Job rpm-sti-test branches: None source: fedora-zuul-jobs-config/zuul.d/projects.yaml@master#108>

Pull-Request has been merged by zpytela

2 years ago
Metadata
Changes Summary 1