-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize utility so most code is in mache itself
- Loading branch information
Showing
5 changed files
with
125 additions
and
125 deletions.
There are no files selected for viewing
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
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() |
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
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) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from importlib import resources as importlib_resources | ||
|
||
|
||
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(): | ||
if file.suffix == '.cfg' and file.name != 'default.cfg': | ||
machines.append(file.stem) | ||
return sorted(machines) |
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
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