Skip to content

Commit 04c3770

Browse files
Merge pull request #1816 from ansible-collections/enabler/1652/migrate_shell_call_cp_to_python_module
[Enabler][1652]migrate_shell_call_cp_to_python_module
2 parents 95f5948 + 9f04361 commit 04c3770

File tree

2 files changed

+75
-24
lines changed

2 files changed

+75
-24
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
trivial:
2+
- zos_fetch - Migrate calls from cp to python API copy.
3+
(https://github.com/ansible-collections/ibm_zos_core/pull/1816).

plugins/modules/zos_fetch.py

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@
320320
mvscmd = ZOAUImportError(traceback.format_exc())
321321
ztypes = ZOAUImportError(traceback.format_exc())
322322

323+
try:
324+
from zoautil_py import exceptions as zoau_exceptions
325+
except Exception:
326+
zoau_exceptions = ZOAUImportError(traceback.format_exc())
327+
323328

324329
class FetchHandler:
325330
def __init__(self, module):
@@ -621,29 +626,37 @@ def _fetch_pdse(self, src, is_binary, temp_dir=None, encoding=None):
621626
622627
Raises
623628
------
624-
fail_json
629+
ZOSFetchError
625630
Error copying partitioned dataset to USS.
626631
fail_json
627632
Error converting encoding of the member.
628633
"""
629634
dir_path = tempfile.mkdtemp(dir=temp_dir)
630-
cmd = "cp -B \"//'{0}'\" {1}"
631-
if not is_binary:
632-
cmd = cmd.replace(" -B", "")
633-
rc, out, err = self._run_command(cmd.format(src, dir_path))
634-
if rc != 0:
635+
636+
copy_args = {
637+
"options": ""
638+
}
639+
640+
if is_binary:
641+
copy_args["options"] = "-B"
642+
643+
try:
644+
datasets.copy(source=src, target=dir_path, **copy_args)
645+
646+
except zoau_exceptions.ZOAUException as copy_exception:
635647
rmtree(dir_path)
636-
self._fail_json(
648+
raise ZOSFetchError(
637649
msg=(
638650
"Error copying partitioned data set {0} to USS. Make sure it is"
639651
" not empty".format(src)
640652
),
641-
stdout=out,
642-
stderr=err,
643-
stdout_lines=out.splitlines(),
644-
stderr_lines=err.splitlines(),
645-
rc=rc,
653+
rc=copy_exception.response.rc,
654+
stdout=copy_exception.response.stdout_response,
655+
stderr=copy_exception.response.stderr_response,
656+
stdout_lines=copy_exception.response.stdout_response.splitlines(),
657+
stderr_lines=copy_exception.response.stderr_response.splitlines(),
646658
)
659+
647660
if (not is_binary) and encoding:
648661
enc_utils = encode.EncodeUtils()
649662
from_code_set = encoding.get("from")
@@ -740,7 +753,7 @@ def _fetch_mvs_data(self, src, is_binary, temp_dir=None, file_override=None, enc
740753
741754
Raises
742755
------
743-
fail_json
756+
ZOSFetchError
744757
Unable to copy to USS.
745758
fail_json
746759
Error converting encoding of the dataset.
@@ -754,22 +767,27 @@ def _fetch_mvs_data(self, src, is_binary, temp_dir=None, file_override=None, enc
754767
fd, file_path = tempfile.mkstemp(dir=temp_dir)
755768
os.close(fd)
756769

757-
cmd = "cp -B \"//'{0}'\" {1}"
758-
if not is_binary:
759-
cmd = cmd.replace(" -B", "")
770+
copy_args = {
771+
"options": ""
772+
}
760773

761-
rc, out, err = self._run_command(cmd.format(src, file_path))
774+
if is_binary:
775+
copy_args["options"] = "-B"
762776

763-
if rc != 0:
777+
try:
778+
datasets.copy(source=src, target=file_path, **copy_args)
779+
780+
except zoau_exceptions.ZOAUException as copy_exception:
764781
os.remove(file_path)
765-
self._fail_json(
782+
raise ZOSFetchError(
766783
msg="Unable to copy {0} to USS".format(src),
767-
stdout=str(out),
768-
stderr=str(err),
769-
rc=rc,
770-
stdout_lines=str(out).splitlines(),
771-
stderr_lines=str(err).splitlines(),
784+
rc=copy_exception.response.rc,
785+
stdout=copy_exception.response.stdout_response,
786+
stderr=copy_exception.response.stderr_response,
787+
stdout_lines=copy_exception.response.stdout_response.splitlines(),
788+
stderr_lines=copy_exception.response.stderr_response.splitlines(),
772789
)
790+
773791
if (not is_binary) and encoding:
774792
enc_utils = encode.EncodeUtils()
775793
from_code_set = encoding.get("from")
@@ -1036,6 +1054,36 @@ def run_module():
10361054
module.exit_json(**res_args)
10371055

10381056

1057+
class ZOSFetchError(Exception):
1058+
def __init__(self, msg, rc="", stdout="", stderr="", stdout_lines="", stderr_lines=""):
1059+
"""Error in a copy operation.
1060+
1061+
Parameters
1062+
----------
1063+
msg : str
1064+
Human readable string describing the exception.
1065+
rc : int
1066+
Result code.
1067+
stdout : str
1068+
Standart output.
1069+
stderr : str
1070+
Standart error.
1071+
stdout_lines : str
1072+
Standart output divided in lines.
1073+
stderr_lines : str
1074+
Standart error divided in lines.
1075+
"""
1076+
self.json_args = dict(
1077+
msg=msg,
1078+
rc=rc,
1079+
stdout=stdout,
1080+
stderr=stderr,
1081+
stdout_lines=stdout_lines,
1082+
stderr_lines=stderr_lines,
1083+
)
1084+
super().__init__(self.msg)
1085+
1086+
10391087
def main():
10401088
run_module()
10411089

0 commit comments

Comments
 (0)