Skip to content

Commit

Permalink
Reorganize utility so most code is in mache itself
Browse files Browse the repository at this point in the history
  • Loading branch information
xylar committed Feb 24, 2025
1 parent 845e2d8 commit f072a7a
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 125 deletions.
79 changes: 79 additions & 0 deletions mache/cime_machine_config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import difflib

from lxml import etree
from termcolor import colored


def extract_supported_machines(input_xml, output_xml, supported_machines):
"""
Extract supported machines from an XML file and write to a new XML file.
Parameters
----------
input_xml : str
The path to the input XML file.
output_xml : str
The path to the output XML file.
supported_machines : list of str
A list of supported machine names.
"""
tree = etree.parse(input_xml)
root = tree.getroot()

for machine in root.findall('machine'):
mach_name = machine.get('MACH')
if mach_name not in supported_machines:
root.remove(machine)

tree.write(output_xml, pretty_print=True)


def compare_machine_configs(old_xml, new_xml, machine_name):
"""
Compare the XML within the `machine` tag for a given machine between the
old and new XML files and print the differences.
Parameters
----------
old_xml : str
The path to the old XML file.
new_xml : str
The path to the new XML file.
machine_name : str
The name of the machine to compare.
"""
old_tree = etree.parse(old_xml)
new_tree = etree.parse(new_xml)

old_machine = old_tree.find(f".//machine[@MACH='{machine_name}']")
new_machine = new_tree.find(f".//machine[@MACH='{machine_name}']")

if old_machine is None:
print(f"Machine {machine_name} not found in {old_xml}.")
return

if new_machine is None:
print(f"Machine {machine_name} not found in {new_xml}.")
return

old_machine_str = etree.tostring(old_machine, pretty_print=True).decode()
new_machine_str = etree.tostring(new_machine, pretty_print=True).decode()

diff = difflib.unified_diff(
old_machine_str.splitlines(),
new_machine_str.splitlines(),
fromfile='old',
tofile='new',
lineterm=''
)

print()
print(colored(f"Comparing machine: {machine_name}", 'blue'))
for line in diff:
if line.startswith('-'):
print(colored(line, 'red'))
elif line.startswith('+'):
print(colored(line, 'green'))
else:
print(line)
print()
19 changes: 19 additions & 0 deletions mache/io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import requests


def download_file(url, local_filename):
"""
Download a file from a URL.
Parameters
----------
url : str
The URL of the file to download.
local_filename : str
The local path where the file will be saved.
"""
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
20 changes: 20 additions & 0 deletions mache/machines/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from importlib import resources as importlib_resources
from pathlib import Path


def get_supported_machines():
"""
Get a list of supported machines by reading .cfg files in the
mache.machines directory.
Returns
-------
list of str
A sorted list of supported machine names.
"""
machines = []
for file in importlib_resources.files('mache.machines').iterdir():
file_path = Path(str(file))
if file_path.suffix == '.cfg' and file_path.name != 'default.cfg':
machines.append(file_path.stem)
return sorted(machines)
6 changes: 2 additions & 4 deletions spec-file.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ jinja2
lxml
pyyaml
progressbar2
requests
rsync
termcolor

# Building
setuptools >=60

# Utilities
requests
termcolor

# Linting and testing
pip
pytest
Expand Down
128 changes: 7 additions & 121 deletions utils/update_cime_machine_config.py
Original file line number Diff line number Diff line change
@@ -1,125 +1,11 @@
#!/usr/bin/env python3

import difflib
import os

import requests
from lxml import etree
from termcolor import colored


def download_file(url, local_filename):
"""
Download a file from a URL.
Parameters
----------
url : str
The URL of the file to download.
local_filename : str
The local path where the file will be saved.
"""
with requests.get(url, stream=True) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)


def get_supported_machines(directory):
"""
Get a list of supported machines by reading .cfg files in a directory.
Parameters
----------
directory : str
The directory to search for .cfg files.
Returns
-------
list of str
A sorted list of supported machine names.
"""
machines = []
for filename in os.listdir(directory):
if filename.endswith('.cfg') and filename != 'default.cfg':
machines.append(os.path.splitext(filename)[0])
return sorted(machines)


def extract_supported_machines(input_xml, output_xml, supported_machines):
"""
Extract supported machines from an XML file and write to a new XML file.
Parameters
----------
input_xml : str
The path to the input XML file.
output_xml : str
The path to the output XML file.
supported_machines : list of str
A list of supported machine names.
"""
tree = etree.parse(input_xml)
root = tree.getroot()

for machine in root.findall('machine'):
mach_name = machine.get('MACH')
if mach_name not in supported_machines:
root.remove(machine)

tree.write(output_xml, pretty_print=True)


def compare_machine_configs(old_xml, new_xml, machine_name):
"""
Compare the XML within the `machine` tag for a given machine between the
old and new XML files and print the differences.
Parameters
----------
old_xml : str
The path to the old XML file.
new_xml : str
The path to the new XML file.
machine_name : str
The name of the machine to compare.
"""
old_tree = etree.parse(old_xml)
new_tree = etree.parse(new_xml)

old_machine = old_tree.find(f".//machine[@MACH='{machine_name}']")
new_machine = new_tree.find(f".//machine[@MACH='{machine_name}']")

if old_machine is None:
print(f"Machine {machine_name} not found in {old_xml}.")
return

if new_machine is None:
print(f"Machine {machine_name} not found in {new_xml}.")
return

old_machine_str = etree.tostring(old_machine, pretty_print=True).decode()
new_machine_str = etree.tostring(new_machine, pretty_print=True).decode()

diff = difflib.unified_diff(
old_machine_str.splitlines(),
new_machine_str.splitlines(),
fromfile='old',
tofile='new',
lineterm=''
)

print()
print(colored(f"Comparing machine: {machine_name}", 'blue'))
for line in diff:
if line.startswith('-'):
print(colored(line, 'red'))
elif line.startswith('+'):
print(colored(line, 'green'))
else:
print(line)
print()
from mache.cime_machine_config import (
compare_machine_configs,
extract_supported_machines,
)
from mache.io import download_file
from mache.machines import get_supported_machines


def main():
Expand All @@ -131,7 +17,7 @@ def main():
'master/cime_config/machines/config_machines.xml')
new_filename = 'new_config_machines.xml'
download_file(url, new_filename)
machines = get_supported_machines('mache/machines')
machines = get_supported_machines()
extract_supported_machines(new_filename,
'new_config_supported_machines.xml',
machines)
Expand Down

0 comments on commit f072a7a

Please sign in to comment.