Skip to content

Instantly share code, notes, and snippets.

@carlwilson
Created February 29, 2024 09:13
Show Gist options
  • Save carlwilson/6083be08c432e372839d34d00993e257 to your computer and use it in GitHub Desktop.
Save carlwilson/6083be08c432e372839d34d00993e257 to your computer and use it in GitHub Desktop.
Running commons-ip from Python
import json
import os
import subprocess
from swagger_server.models import ValidationReport
MAIN_OPTS = [
'java',
'-jar',
'/home/cfw/Downloads/commons-ip2-cli-2.0.0.jar',
'validate',
'-i'
]
REP_OPTS = [
'-r',
'eark'
]
def validate_ip(info_pack):
"""Returns a tuple comprising the process exit code, the validation report
and the captured stderr."""
ret_code, file_name, stderr = java_runner(info_pack)
validation_report = None
if ret_code == 0:
with open(file_name, 'r', encoding='utf-8') as _f:
contents = _f.read()
os.remove(file_name)
validation_report = ValidationReport.from_dict(json.loads(contents))
return ret_code, validation_report, stderr
def java_runner(ip_root):
command = MAIN_OPTS
command.append(ip_root)
command+=REP_OPTS
proc_results = subprocess.run(command, capture_output=True)
return proc_results.returncode, proc_results.stdout.rstrip(), proc_results.stderr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment