Created
May 12, 2023 17:56
-
-
Save lanbugs/155d98b2997b99cbc626058ecc53b8e5 to your computer and use it in GitHub Desktop.
Broadcom ProxySG CPL Delta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import sys | |
from loguru import logger | |
@logger.catch | |
def cpl_parser(lines): | |
root = {} | |
START = False | |
END = False | |
for line in lines: | |
if re.match('^\;.*', line): | |
continue | |
if re.match('define (condition|subnet|category|action) (.*)', line): | |
# FIND START | |
get_start = re.compile('define (condition|subnet|category|action) (.*)') | |
match = get_start.match(line) | |
TYPE = match.group(1) | |
GROUP_NAME = match.group(2).replace("\"","") | |
if TYPE not in root.keys(): | |
root[TYPE] = {} | |
root[TYPE][GROUP_NAME] = [] | |
START = True | |
END = False | |
if re.match('^(.*)$', line) and START is True and END is False and not re.match('define (condition|subnet|category|action) (.*)', line) and not re.match('^end.*', line): | |
# CONTENT | |
get_content = re.compile('^(.*)$') | |
match = get_content.match(line) | |
if len(match.group(1).strip()) > 1: | |
root[TYPE][GROUP_NAME].append(match.group(1).strip()) | |
if re.match('^end .*$', line) and START is True: | |
# FIND END | |
print(line) | |
END = True | |
return root | |
@logger.catch | |
def main(left_file, right_file): | |
with open(left_file, "r") as f: | |
buffer_left = cpl_parser(f.readlines()) | |
with open(right_file, "r") as f: | |
buffer_right = cpl_parser(f.readlines()) | |
buffer_delta = {} | |
buffer_delta['condition'] = {} | |
buffer_delta['subnet'] = {} | |
buffer_delta['category'] = {} | |
buffer_delta['action'] = {} | |
print(f"Left file: {left_file}") | |
print(f"Right file: {right_file}") | |
for xtype, value in buffer_right.items(): | |
for xgroup, xvalue in value.items(): | |
if xtype in buffer_left.keys(): | |
if xgroup in buffer_left[xtype]: | |
for element in xvalue: | |
if element not in buffer_left[xtype][xgroup]: | |
if xgroup not in buffer_delta[xtype]: | |
buffer_delta[xtype][xgroup] = [] | |
buffer_delta[xtype][xgroup].append(element) | |
else: | |
buffer_delta[xtype][xgroup] = buffer_right[xtype][xgroup] | |
from pprint import pprint | |
pprint(buffer_delta) | |
if __name__ == "__main__": | |
main(sys.argv[1], sys.argv[2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment