|
16 | 16 | import re
|
17 | 17 | import tempfile
|
18 | 18 | import traceback
|
19 |
| -from os import path, walk |
| 19 | +from os import path, walk, environ |
20 | 20 | from random import sample
|
21 | 21 | from string import ascii_uppercase, digits
|
22 | 22 |
|
|
35 | 35 | vtoc = MissingImport("vtoc")
|
36 | 36 |
|
37 | 37 | try:
|
38 |
| - from zoautil_py import datasets, exceptions, gdgs |
| 38 | + from zoautil_py import datasets, exceptions, gdgs, mvscmd, ztypes |
39 | 39 | except ImportError:
|
40 | 40 | datasets = ZOAUImportError(traceback.format_exc())
|
41 | 41 | exceptions = ZOAUImportError(traceback.format_exc())
|
42 | 42 | gdgs = ZOAUImportError(traceback.format_exc())
|
43 |
| - Dataset = ZOAUImportError(traceback.format_exc()) |
| 43 | + mvscmd = ZOAUImportError(traceback.format_exc()) |
| 44 | + ztypes = ZOAUImportError(traceback.format_exc()) |
44 | 45 |
|
45 | 46 |
|
46 | 47 | class DataSet(object):
|
@@ -2105,6 +2106,119 @@ def _build_volume_string_iehprogm(volumes):
|
2105 | 2106 | volume_string += single_volume_string + ")\n"
|
2106 | 2107 | return volume_string
|
2107 | 2108 |
|
| 2109 | + @staticmethod |
| 2110 | + def get_name_if_data_set_is_alias(name, tmp_hlq=None): |
| 2111 | + """Checks the catalog to see if 'name' corresponds to a data set |
| 2112 | + alias and returns the original data set name in case it is. |
| 2113 | + Creates a temp data set to hold the IDCAMS command. |
| 2114 | +
|
| 2115 | + Parameters |
| 2116 | + ---------- |
| 2117 | + name : str |
| 2118 | + Name of a data set or alias. |
| 2119 | +
|
| 2120 | + Keyword Parameters |
| 2121 | + ------------------ |
| 2122 | + tmp_hlq : str |
| 2123 | + Temp HLQ to use with mvscmdauth. |
| 2124 | +
|
| 2125 | + Returns |
| 2126 | + ------- |
| 2127 | + tuple(bool, str) |
| 2128 | + A tuple containing whether name corresponds to a data |
| 2129 | + set alias and the name of the data set that the alias |
| 2130 | + points to. |
| 2131 | + """ |
| 2132 | + # We need to unescape because this call to the system can handle |
| 2133 | + # special characters just fine. |
| 2134 | + name = name.upper().replace("\\", '') |
| 2135 | + idcams_cmd = f" LISTCAT ENTRIES('{name}') ALL" |
| 2136 | + response = DataSet._execute_idcams_cmd(idcams_cmd, tmp_hlq=tmp_hlq) |
| 2137 | + |
| 2138 | + if response.rc > 0 or response.stderr_response != '': |
| 2139 | + raise MVSCmdExecError( |
| 2140 | + rc=response.rc, |
| 2141 | + stdout=response.stdout_response, |
| 2142 | + stderr=response.stderr_response |
| 2143 | + ) |
| 2144 | + |
| 2145 | + if re.search(r'(ALIAS -+)(1)', response.stdout_response): |
| 2146 | + base_name = re.search( |
| 2147 | + r'(ASSOCIATIONS\s*\n\s*[0-9a-zA-Z]+-+)([0-9a-zA-Z\.@\$#-]+)', |
| 2148 | + response.stdout_response |
| 2149 | + ).group(2) |
| 2150 | + return True, base_name |
| 2151 | + else: |
| 2152 | + return False, name |
| 2153 | + |
| 2154 | + @staticmethod |
| 2155 | + def _execute_idcams_cmd( |
| 2156 | + cmd, |
| 2157 | + tmp_hlq=None, |
| 2158 | + space_primary=1, |
| 2159 | + space_type='k', |
| 2160 | + record_format='fb', |
| 2161 | + record_length=120 |
| 2162 | + ): |
| 2163 | + """Runs an IDCAMS command using mvscmdauth's Python API. |
| 2164 | +
|
| 2165 | + Parameters |
| 2166 | + ---------- |
| 2167 | + cmd : str |
| 2168 | + IDCAMS command to run. |
| 2169 | +
|
| 2170 | + Keyword Parameters |
| 2171 | + ------------------ |
| 2172 | + tmp_hlq : str |
| 2173 | + Temp HLQ to use with mvscmdauth. |
| 2174 | + space_primary : int |
| 2175 | + Units of primary space for the input data set for IDCAMS. |
| 2176 | + space_type : str |
| 2177 | + Unit of data set space. |
| 2178 | + record_format : str |
| 2179 | + Record format for the input data set. |
| 2180 | + record_length : int |
| 2181 | + Record length for the input data set. |
| 2182 | +
|
| 2183 | + Returns |
| 2184 | + ------- |
| 2185 | + ztypes.ZOAUResponse |
| 2186 | + Response object returned by mvscmd.execute_authorized. |
| 2187 | + """ |
| 2188 | + temp_dd_location = None |
| 2189 | + |
| 2190 | + try: |
| 2191 | + temp_dd_location = DataSet.create_temp( |
| 2192 | + hlq=tmp_hlq, |
| 2193 | + type='SEQ', |
| 2194 | + record_format=record_format, |
| 2195 | + space_primary=space_primary, |
| 2196 | + space_secondary=0, |
| 2197 | + space_type=space_type, |
| 2198 | + record_length=record_length |
| 2199 | + ) |
| 2200 | + |
| 2201 | + datasets.write(temp_dd_location, cmd) |
| 2202 | + cmd_dd = ztypes.DatasetDefinition(temp_dd_location, disposition='SHR') |
| 2203 | + |
| 2204 | + dds = [ |
| 2205 | + ztypes.DDStatement('SYSPRINT', '*'), |
| 2206 | + ztypes.DDStatement('SYSIN', cmd_dd) |
| 2207 | + ] |
| 2208 | + |
| 2209 | + if tmp_hlq: |
| 2210 | + environ['TMPHLQ'] = tmp_hlq |
| 2211 | + |
| 2212 | + response = mvscmd.execute_authorized('IDCAMS', dds=dds) |
| 2213 | + |
| 2214 | + if tmp_hlq: |
| 2215 | + del environ['TMPHLQ'] |
| 2216 | + |
| 2217 | + return response |
| 2218 | + finally: |
| 2219 | + if temp_dd_location: |
| 2220 | + datasets.delete(temp_dd_location) |
| 2221 | + |
2108 | 2222 |
|
2109 | 2223 | class DataSetUtils(object):
|
2110 | 2224 | def __init__(self, data_set, tmphlq=None):
|
|
0 commit comments