|  | 
| 29 | 29 | from ansible_collections.ibm.ibm_zos_core.plugins.module_utils.import_handler import \ | 
| 30 | 30 |     ZOAUImportError | 
| 31 | 31 | 
 | 
| 32 |  | -from shlex import quote | 
| 33 |  | - | 
| 34 | 32 | try: | 
| 35 |  | -    from zoautil_py import datasets, gdgs | 
|  | 33 | +    from zoautil_py import datasets, gdgs, exceptions as zoau_exceptions | 
| 36 | 34 | except Exception: | 
| 37 | 35 |     datasets = ZOAUImportError(traceback.format_exc()) | 
| 38 | 36 |     gdgs = ZOAUImportError(traceback.format_exc()) | 
|  | 37 | +    zoau_exceptions = ZOAUImportError(traceback.format_exc()) | 
| 39 | 38 | 
 | 
| 40 | 39 | REPRO = """  REPRO INDATASET({}) - | 
| 41 | 40 |     OUTDATASET({}) REPLACE """ | 
| @@ -79,146 +78,51 @@ def _validate_path(path): | 
| 79 | 78 |     return parsed_args.get("path") | 
| 80 | 79 | 
 | 
| 81 | 80 | 
 | 
| 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. | 
| 84 | 84 | 
 | 
| 85 | 85 |     Parameters | 
| 86 | 86 |     ---------- | 
| 87 | 87 |     src : str | 
| 88 |  | -        The uss file or path to be copied. | 
|  | 88 | +        Source dataset or file. | 
| 89 | 89 |     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. | 
| 93 | 91 | 
 | 
| 94 | 92 |     Keyword Parameters | 
| 95 | 93 |     ------------------ | 
| 96 | 94 |     is_binary : bool | 
| 97 |  | -        Whether the file to be copied contains binary data. | 
|  | 95 | +        Whether to perform a binary copy. | 
| 98 | 96 | 
 | 
| 99 | 97 |     Returns | 
| 100 | 98 |     ------- | 
| 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. | 
| 107 | 102 | 
 | 
| 108 | 103 |     Raises | 
| 109 | 104 |     ------ | 
| 110 |  | -    USSCmdExecError | 
| 111 |  | -        When any exception is raised during the conversion. | 
|  | 105 | +    ZOAUImportError | 
|  | 106 | +        When there's an issue calling the datasets API. | 
| 112 | 107 |     """ | 
| 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 | +    } | 
| 152 | 111 | 
 | 
| 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)) | 
| 162 | 112 |     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" | 
| 216 | 114 | 
 | 
| 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 | 
| 220 | 124 | 
 | 
| 221 |  | -    return rc, out, err | 
|  | 125 | +    return 0, "", "" | 
| 222 | 126 | 
 | 
| 223 | 127 | 
 | 
| 224 | 128 | 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): | 
| 264 | 168 |     return True | 
| 265 | 169 | 
 | 
| 266 | 170 | 
 | 
| 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 |  | - | 
| 342 | 171 | def copy_vsam_ps(src, dest, tmphlq=None): | 
| 343 | 172 |     """Copy a VSAM(KSDS) data set to a PS data set vise versa. | 
| 344 | 173 | 
 | 
|  | 
0 commit comments