Last active
March 22, 2022 18:29
-
-
Save plavjanik/ffade41d7f5c2e7343c5532fc3f2aeb3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 subprocess | |
| import json | |
| class ZoweCallError(Exception): | |
| def __init__(self, returncode, cmd, arguments, output=None, stderr=None): | |
| self.cmd = cmd | |
| self.arguments = arguments | |
| self.returncode = returncode | |
| self.output = self.stdout = output | |
| self.stderr = stderr | |
| self.error = None | |
| self.message = None | |
| self._parse_zowe_error(output) | |
| def _parse_zowe_error(self, output): | |
| try: | |
| parsed = json.loads(output) | |
| if "exitCode" in parsed: | |
| self.returncode = parsed["exitCode"] | |
| if "stdout" in parsed: | |
| self.output = self.stdout = parsed["stdout"] | |
| if "stderr" in parsed: | |
| self.stderr = parsed["stderr"] | |
| self.message = parsed.get("message") | |
| self.error = parsed.get("error") | |
| except json.JSONDecodeError: | |
| pass | |
| def __str__(self): | |
| if self.returncode and self.returncode < 0: | |
| return "Zowe CLI command '%s' died with return code %d." % (self.cmd, -self.returncode) | |
| else: | |
| if self.stderr: | |
| return "Zowe CLI command with arguments '%s' returned non-zero exit status %d and error:\n%s" % ( | |
| self.arguments, self.returncode, self.stderr) | |
| else: | |
| return "Zowe CLI command with arguments '%s' returned non-zero exit status %d." % ( | |
| self.arguments, self.returncode) | |
| def __repr__(self): | |
| return "ZoweCallError(%s, %s, %s, %s, %s)" % (repr(self.returncode), repr(self.cmd), repr(self.arguments), repr(self.stdout), repr(self.stderr)) | |
| def zowe(arguments): | |
| zowe_command = "zowe " + arguments + " --rfj" | |
| try: | |
| completed_process = subprocess.run(zowe_command, shell=True, capture_output=True, check=True, encoding="utf8") | |
| parsed_output = json.loads(completed_process.stdout) | |
| return parsed_output.get("data", parsed_output) | |
| except subprocess.CalledProcessError as e: | |
| raise ZoweCallError(e.returncode, e.cmd, arguments, output=e.output, stderr=e.stderr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment