Skip to content

Commit

Permalink
random: Add scripts to copy IMS APN as well as sort APNs
Browse files Browse the repository at this point in the history
(cherry picked from commit c6201a8)
Signed-off-by: Omkar Chandorkar <[email protected]>
Signed-off-by: Akhil Narang <[email protected]>
  • Loading branch information
gotenksIN authored and akhilnarang committed May 13, 2024
1 parent 1f2974a commit 10860ba
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
22 changes: 14 additions & 8 deletions random-py/apn-copy-entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pathlib import Path
from xml.etree import ElementTree

# ./apn.py source.xml destination.xml
# ./apn-copy-entries.py source.xml destination.xml
# If no arguments are provided, the user will be prompted to enter the file paths

if len(sys.argv) < 3:
Expand All @@ -25,19 +25,25 @@
destination_apn_xml = ElementTree.parse(destination_path)

tags_to_match = ("mcc", "mnc")
tags_to_copy = ("apn",)
required_tags = tags_to_match + tags_to_copy
tags_to_copy = ("apn")

destination_root = destination_apn_xml.getroot()

combinations = set()

for child in destination_root:
combinations.add("|".join([child.attrib[tag] for tag in tags_to_match]))
# Define a function to check if a child element is already in the destination root
def is_in_destination(child, destination_root):
for dest_child in destination_root:
if child.attrib == dest_child.attrib:
return True
return False

# Modify the loop that appends children from the source to the destination
for child in source_apn_xml.getroot():
key = "|".join([child.attrib[tag] for tag in tags_to_match])
if key in combinations:
destination_root.append(child)
# Only consider children with apn='ims'
if child.attrib.get('apn') == 'ims':
# Only append the child if it is not already in the destination
if not is_in_destination(child, destination_root):
destination_root.append(child)

destination_apn_xml.write(destination, encoding="utf-8")
34 changes: 34 additions & 0 deletions random-py/sort_apn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python

import sys
from pathlib import Path
from xml.etree import ElementTree

# ./sort_apn.py apns-conf.xml
# If no arguments are provided, the user will be prompted to enter the file paths

if len(sys.argv) < 2:
apns = input("Enter the apns-conf XML path: ")
else:
apns = sys.argv[1]

apns_path = Path(apns)

if not apns_path.exists():
print("Please ensure that the files exist")
sys.exit(1)

def sort_apns(apns_path):
# Parse the XML file into an ElementTree
tree = ElementTree.parse(apns_path)
root = tree.getroot()

# Sort the children of the root by 'mcc' and 'mnc'
root[:] = sorted(root, key=lambda child: (child.attrib.get('mcc', ''), child.attrib.get('mnc', '')))

# Write the sorted ElementTree back into the XML file
with open(apns_path, 'wb') as f:
f.write(ElementTree.tostring(root, encoding='utf-8'))

# Call the function with the XML file path
sort_apns(apns_path)

0 comments on commit 10860ba

Please sign in to comment.