Skip to content

Commit f5376bf

Browse files
rexeminfernandofloresg
authored andcommitted
[Enabler] [module_utils/copy.py] Replace calls to cp for dcp's Python API (#1831)
* Replace calls to cp for dcp * Replace copy functions in copy.py * Replace copy calls in modules * Fix typo * Fix return statement * Add changelog fragment * Fix sanity issues
1 parent 6407365 commit f5376bf

File tree

5 files changed

+43
-212
lines changed

5 files changed

+43
-212
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
trivial:
2+
- module_utils/copy.py - Replaced calls to cp with calls to ZOAU's
3+
datasets' Python API.
4+
(https://github.com/ansible-collections/ibm_zos_core/pull/1831)

plugins/module_utils/copy.py

Lines changed: 27 additions & 198 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@
2929
from ansible_collections.ibm.ibm_zos_core.plugins.module_utils.import_handler import \
3030
ZOAUImportError
3131

32-
from shlex import quote
33-
3432
try:
35-
from zoautil_py import datasets, gdgs
33+
from zoautil_py import datasets, gdgs, exceptions as zoau_exceptions
3634
except Exception:
3735
datasets = ZOAUImportError(traceback.format_exc())
3836
gdgs = ZOAUImportError(traceback.format_exc())
37+
zoau_exceptions = ZOAUImportError(traceback.format_exc())
3938

4039
REPRO = """ REPRO INDATASET({}) -
4140
OUTDATASET({}) REPLACE """
@@ -79,146 +78,51 @@ def _validate_path(path):
7978
return parsed_args.get("path")
8079

8180

82-
def copy_uss2mvs(src, dest, ds_type, is_binary=False):
83-
"""Copy uss a file or path to an MVS data set.
81+
def copy_uss_mvs(src, dest, is_binary=False):
82+
"""Wrapper function for datasets.copy that handles possible
83+
exceptions that may occur.
8484
8585
Parameters
8686
----------
8787
src : str
88-
The uss file or path to be copied.
88+
Source dataset or file.
8989
dest : str
90-
The destination MVS data set, it must be a PS or PDS(E).
91-
ds_type : str
92-
The dsorg of the dest.
90+
Destination dataset name or file.
9391
9492
Keyword Parameters
9593
------------------
9694
is_binary : bool
97-
Whether the file to be copied contains binary data.
95+
Whether to perform a binary copy.
9896
9997
Returns
10098
-------
101-
bool
102-
The return code after the copy command executed successfully.
103-
str
104-
The stdout after the copy command executed successfully.
105-
str
106-
The stderr after the copy command executed successfully.
99+
tuple
100+
Tuple containing return code, standard output and standard
101+
error from the datasets API.
107102
108103
Raises
109104
------
110-
USSCmdExecError
111-
When any exception is raised during the conversion.
105+
ZOAUImportError
106+
When there's an issue calling the datasets API.
112107
"""
113-
module = AnsibleModuleHelper(argument_spec={})
114-
src = _validate_path(src)
115-
dest = _validate_data_set_name(dest)
116-
if ds_type == "PO":
117-
cp_uss2mvs = "cp -CM -F rec {0} \"//'{1}'\"".format(quote(src), dest)
118-
else:
119-
cp_uss2mvs = "cp -F rec {0} \"//'{1}'\"".format(quote(src), dest)
120-
if is_binary:
121-
cp_uss2mvs = cp_uss2mvs.replace("rec", "bin", 1)
122-
rc, out, err = module.run_command(cp_uss2mvs, errors='replace')
123-
if rc:
124-
raise USSCmdExecError(cp_uss2mvs, rc, out, err)
125-
return rc, out, err
126-
127-
128-
def copy_ps2uss(src, dest, is_binary=False):
129-
"""Copy a PS data set to a uss file.
130-
131-
Parameters
132-
----------
133-
src : str
134-
The MVS data set to be copied, it must be a PS data set
135-
or a PDS(E) member.
136-
dest : str
137-
The destination uss file.
138-
139-
Keyword Parameters
140-
------------------
141-
is_binary : bool
142-
Whether the file to be copied contains binary data.
143-
144-
Returns
145-
-------
146-
bool
147-
The return code after the copy command executed successfully.
148-
str
149-
The stdout after the copy command executed successfully.
150-
str
151-
The stderr after the copy command executed successfully.
108+
copy_args = {
109+
"options": ""
110+
}
152111

153-
Raises
154-
------
155-
USSCmdExecError
156-
When any exception is raised during the conversion.
157-
"""
158-
module = AnsibleModuleHelper(argument_spec={})
159-
src = _validate_data_set_name(src)
160-
dest = _validate_path(dest)
161-
cp_ps2uss = "cp -F rec \"//'{0}'\" {1}".format(src, quote(dest))
162112
if is_binary:
163-
cp_ps2uss = cp_ps2uss.replace("rec", "bin", 1)
164-
rc, out, err = module.run_command(cp_ps2uss, errors='replace')
165-
if rc:
166-
raise USSCmdExecError(cp_ps2uss, rc, out, err)
167-
return rc, out, err
168-
169-
170-
def copy_pds2uss(src, dest, is_binary=False, asa_text=False):
171-
"""Copy the whole PDS(E) to a uss path.
172-
173-
Parameters
174-
----------
175-
src : str
176-
The MVS data set to be copied, it must be a PDS(E) data set.
177-
dest : str
178-
The destination uss path.
179-
180-
Keyword Parameters
181-
------------------
182-
is_binary : bool
183-
Whether the file to be copied contains binary data.
184-
asa_text : bool
185-
Whether the file to be copied contains ASA control
186-
characters.
187-
188-
Returns
189-
-------
190-
bool
191-
The return code after the USS command executed successfully.
192-
str
193-
The stdout after the USS command executed successfully.
194-
str
195-
The stderr after the USS command executed successfully.
196-
197-
Raises
198-
------
199-
USSCmdExecError
200-
When any exception is raised during the conversion.
201-
"""
202-
module = AnsibleModuleHelper(argument_spec={})
203-
src = _validate_data_set_name(src)
204-
dest = _validate_path(dest)
205-
206-
cp_pds2uss = "cp -U -F rec \"//'{0}'\" {1}".format(src, quote(dest))
207-
208-
# When dealing with ASA control chars, each record follows a
209-
# different format than what '-F rec' means, so we remove it
210-
# to allow the system to leave the control chars in the
211-
# destination.
212-
if asa_text:
213-
cp_pds2uss = cp_pds2uss.replace("-F rec", "", 1)
214-
elif is_binary:
215-
cp_pds2uss = cp_pds2uss.replace("rec", "bin", 1)
113+
copy_args["options"] = "-B"
216114

217-
rc, out, err = module.run_command(cp_pds2uss, errors='replace')
218-
if rc:
219-
raise USSCmdExecError(cp_pds2uss, rc, out, err)
115+
try:
116+
datasets.copy(source=src, target=dest, **copy_args)
117+
except zoau_exceptions.ZOAUException as copy_exception:
118+
# Returning the exception content instead of raising it
119+
# since a lot of code that uses this function expects it
120+
# so they can decide what to do in case of an error.
121+
return copy_exception.response.rc, \
122+
copy_exception.response.stdout_response, \
123+
copy_exception.response.stderr_response
220124

221-
return rc, out, err
125+
return 0, "", ""
222126

223127

224128
def copy_gdg2uss(src, dest, is_binary=False, asa_text=False):
@@ -264,81 +168,6 @@ def copy_gdg2uss(src, dest, is_binary=False, asa_text=False):
264168
return True
265169

266170

267-
def copy_uss2uss_binary(src, dest):
268-
"""Copy a USS file to a USS location in binary mode.
269-
270-
Parameters
271-
----------
272-
src : str
273-
The source USS path.
274-
dest : str
275-
The destination USS path.
276-
277-
Returns
278-
-------
279-
bool
280-
The return code after the USS command executed successfully.
281-
str
282-
The stdout after the USS command executed successfully.
283-
str
284-
The stderr after the USS command executed successfully.
285-
286-
Raises
287-
------
288-
USSCmdExecError
289-
When any exception is raised during the conversion.
290-
"""
291-
module = AnsibleModuleHelper(argument_spec={})
292-
src = _validate_path(src)
293-
dest = _validate_path(dest)
294-
cp_uss2uss = "cp -F bin {0} {1}".format(quote(src), quote(dest))
295-
rc, out, err = module.run_command(cp_uss2uss, errors='replace')
296-
if rc:
297-
raise USSCmdExecError(cp_uss2uss, rc, out, err)
298-
return rc, out, err
299-
300-
301-
def copy_mvs2mvs(src, dest, is_binary=False):
302-
"""Copy an MVS source to MVS target.
303-
304-
Parameters
305-
----------
306-
src : str
307-
Name of source data set.
308-
dest : str
309-
Name of destination data set.
310-
311-
Keyword Parameters
312-
------------------
313-
is_binary : bool
314-
Whether the data set to be copied contains binary data.
315-
316-
Returns
317-
-------
318-
bool
319-
The return code after the USS command executed successfully.
320-
str
321-
The stdout after the USS command executed successfully.
322-
str
323-
The stderr after the USS command executed successfully.
324-
325-
Raises
326-
------
327-
USSCmdExecError
328-
When any exception is raised during the conversion.
329-
"""
330-
module = AnsibleModuleHelper(argument_spec={})
331-
src = _validate_data_set_name(src)
332-
dest = _validate_data_set_name(dest)
333-
cp_mvs2mvs = "cp -F rec \"//'{0}'\" \"//'{1}'\"".format(src, dest)
334-
if is_binary:
335-
cp_mvs2mvs = cp_mvs2mvs.replace("rec", "bin", 1)
336-
rc, out, err = module.run_command(cp_mvs2mvs, errors='replace')
337-
if rc:
338-
raise USSCmdExecError(cp_mvs2mvs, rc, out, err)
339-
return rc, out, err
340-
341-
342171
def copy_vsam_ps(src, dest, tmphlq=None):
343172
"""Copy a VSAM(KSDS) data set to a PS data set vise versa.
344173

plugins/module_utils/encode.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,10 @@ def mvs_convert_encoding(
510510
if src_type == "PS":
511511
temp_src_fo = NamedTemporaryFile()
512512
temp_src = temp_src_fo.name
513-
rc, out, err = copy.copy_ps2uss(src, temp_src)
513+
rc, out, err = copy.copy_uss_mvs(src, temp_src)
514514
if src_type == "PO":
515515
temp_src = mkdtemp()
516-
rc, out, err = copy.copy_pds2uss(src, temp_src)
516+
rc, out, err = copy.copy_uss_mvs(src, temp_src)
517517
if src_type == "KSDS":
518518
reclen, space_u = self.listdsi_data_set(src.upper(), tmphlq=tmphlq)
519519
# RDW takes the first 4 bytes in the VB format, hence we need to add an extra buffer to the vsam max recl.
@@ -522,7 +522,7 @@ def mvs_convert_encoding(
522522
rc, out, err = copy.copy_vsam_ps(src.upper(), temp_ps, tmphlq=tmphlq)
523523
temp_src_fo = NamedTemporaryFile()
524524
temp_src = temp_src_fo.name
525-
rc, out, err = copy.copy_ps2uss(temp_ps, temp_src)
525+
rc, out, err = copy.copy_uss_mvs(temp_ps, temp_src)
526526
if dest_type == "PS" or dest_type == "KSDS":
527527
temp_dest_fo = NamedTemporaryFile()
528528
temp_dest = temp_dest_fo.name
@@ -538,17 +538,17 @@ def mvs_convert_encoding(
538538
# RDW takes the first 4 bytes or records in the VB format, hence we need to add an extra buffer to the vsam max recl.
539539
reclen += 4
540540
temp_ps = self.temp_data_set(reclen, space_u)
541-
rc, out, err = copy.copy_uss2mvs(temp_dest, temp_ps, "PS")
541+
rc, out, err = copy.copy_uss_mvs(temp_dest, temp_ps)
542542
rc, out, err = copy.copy_vsam_ps(temp_ps, dest.upper(), tmphlq=tmphlq)
543543
convert_rc = True
544544
elif dest_type == "PO":
545545
for (dir, subdir, files) in walk(temp_dest):
546546
for file in files:
547547
temp_file = path.join(validation.validate_safe_path(dir), validation.validate_safe_path(file))
548-
rc, out, err = copy.copy_uss2mvs(temp_file, dest, "PO")
548+
rc, out, err = copy.copy_uss_mvs(temp_file, dest)
549549
convert_rc = True
550550
else:
551-
rc, out, err = copy.copy_uss2mvs(temp_dest, dest, dest_type)
551+
rc, out, err = copy.copy_uss_mvs(temp_dest, dest)
552552
convert_rc = True
553553
except Exception:
554554
raise

plugins/modules/zos_copy.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,7 +1657,7 @@ def _copy_to_file(self, src, dest, content_copy, conv_path):
16571657
new_src = conv_path or src
16581658
try:
16591659
if self.is_binary:
1660-
copy.copy_uss2uss_binary(new_src, dest)
1660+
copy.copy_uss_mvs(new_src, dest, is_binary=True)
16611661
else:
16621662
opts = dict()
16631663
opts["options"] = ""
@@ -1931,11 +1931,10 @@ def _mvs_copy_to_uss(
19311931
stderr=response.stderr_response
19321932
)
19331933
else:
1934-
copy.copy_pds2uss(
1934+
copy.copy_uss_mvs(
19351935
src,
19361936
dest,
1937-
is_binary=self.is_binary,
1938-
asa_text=self.asa_text
1937+
is_binary=self.is_binary
19391938
)
19401939
except CopyOperationError as err:
19411940
raise err

plugins/modules/zos_mount.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,7 @@
548548
backup as Backup,
549549
)
550550
from ansible_collections.ibm.ibm_zos_core.plugins.module_utils.copy import (
551-
copy_ps2uss,
552-
copy_uss2mvs,
551+
copy_uss_mvs
553552
)
554553

555554

@@ -1045,7 +1044,7 @@ def run_module(module, arg_def):
10451044
tmp_file_filename = tmp_file.name
10461045
tmp_file.close()
10471046

1048-
copy_ps2uss(data_store, tmp_file_filename, False)
1047+
copy_uss_mvs(data_store, tmp_file_filename, is_binary=False)
10491048

10501049
module.run_command(
10511050
"chtag -tc ISO8859-1 " + tmp_file_filename, use_unsafe_shell=False, errors='replace'
@@ -1076,7 +1075,7 @@ def run_module(module, arg_def):
10761075
module.run_command(
10771076
"mrm " + data_store, use_unsafe_shell=False, errors='replace'
10781077
)
1079-
copy_uss2mvs(tmp_file_filename, data_store, "", True)
1078+
copy_uss_mvs(tmp_file_filename, data_store, is_binary=True)
10801079

10811080
if os.path.isfile(tmp_file_filename):
10821081
os.unlink(tmp_file_filename)

0 commit comments

Comments
 (0)