|
3 | 3 | # ---------------------------------------------------------
|
4 | 4 |
|
5 | 5 | import base64
|
| 6 | +import io |
6 | 7 | import json
|
7 | 8 | import logging
|
8 | 9 | import os
|
|
18 | 19 | from threading import Thread
|
19 | 20 | from typing import Dict, Optional, Tuple
|
20 | 21 |
|
| 22 | +from azure.core.credentials import TokenCredential |
| 23 | +from azure.core.exceptions import AzureError |
| 24 | + |
21 | 25 | from azure.ai.ml._restclient.v2022_02_01_preview.models import JobBaseData
|
22 | 26 | from azure.ai.ml._utils._http_utils import HttpPipeline
|
23 | 27 | from azure.ai.ml._utils.utils import DockerProxy
|
|
27 | 31 | EXECUTION_SERVICE_URL_KEY,
|
28 | 32 | INVOCATION_BASH_FILE,
|
29 | 33 | INVOCATION_BAT_FILE,
|
30 |
| - INVOCATION_ZIP_FILE, |
31 | 34 | LOCAL_JOB_FAILURE_MSG,
|
32 | 35 | )
|
33 | 36 | from azure.ai.ml.exceptions import ErrorCategory, ErrorTarget, JobException
|
34 |
| -from azure.core.credentials import TokenCredential |
35 |
| -from azure.core.exceptions import AzureError |
36 | 37 |
|
37 | 38 | docker = DockerProxy()
|
38 | 39 | module_logger = logging.getLogger(__name__)
|
|
41 | 42 | def unzip_to_temporary_file(job_definition: JobBaseData, zip_content: bytes) -> Path:
|
42 | 43 | temp_dir = Path(tempfile.gettempdir(), AZUREML_RUNS_DIR, job_definition.name)
|
43 | 44 | temp_dir.mkdir(parents=True, exist_ok=True)
|
44 |
| - zip_path = temp_dir / INVOCATION_ZIP_FILE |
45 |
| - with zip_path.open(mode="wb") as file: |
46 |
| - file.write(zip_content) |
47 |
| - with zipfile.ZipFile(zip_path, "r") as zip_ref: |
| 45 | + with zipfile.ZipFile(io.BytesIO(zip_content)) as zip_ref: |
48 | 46 | zip_ref.extractall(temp_dir)
|
49 |
| - zip_path.unlink() |
50 | 47 | return temp_dir
|
51 | 48 |
|
52 | 49 |
|
@@ -273,23 +270,16 @@ def get_common_runtime_info_from_response(self, response: Dict[str, str]) -> Tup
|
273 | 270 | :rtype: Tuple[Dict[str, str], str]
|
274 | 271 | """
|
275 | 272 |
|
276 |
| - with tempfile.TemporaryDirectory() as tempdir: |
277 |
| - invocation_zip_path = os.path.join(tempdir, INVOCATION_ZIP_FILE) |
278 |
| - with open(invocation_zip_path, "wb") as file: |
279 |
| - file.write(response) |
280 |
| - |
281 |
| - with zipfile.ZipFile(invocation_zip_path, "r") as zip_ref: |
282 |
| - bootstrapper_path = f"{AZUREML_RUN_SETUP_DIR}/{self.COMMON_RUNTIME_BOOTSTRAPPER_INFO}" |
283 |
| - job_spec_path = f"{AZUREML_RUN_SETUP_DIR}/{self.COMMON_RUNTIME_JOB_SPEC}" |
284 |
| - if not all(file_path in zip_ref.namelist() for file_path in [bootstrapper_path, job_spec_path]): |
285 |
| - raise RuntimeError( |
286 |
| - f"{bootstrapper_path}, {job_spec_path} are not in the execution service response." |
287 |
| - ) |
| 273 | + with zipfile.ZipFile(io.BytesIO(response)) as zip_ref: |
| 274 | + bootstrapper_path = f"{AZUREML_RUN_SETUP_DIR}/{self.COMMON_RUNTIME_BOOTSTRAPPER_INFO}" |
| 275 | + job_spec_path = f"{AZUREML_RUN_SETUP_DIR}/{self.COMMON_RUNTIME_JOB_SPEC}" |
| 276 | + if not all(file_path in zip_ref.namelist() for file_path in [bootstrapper_path, job_spec_path]): |
| 277 | + raise RuntimeError(f"{bootstrapper_path}, {job_spec_path} are not in the execution service response.") |
288 | 278 |
|
289 |
| - with zip_ref.open(bootstrapper_path, "r") as bootstrapper_file: |
290 |
| - bootstrapper_json = json.loads(base64.b64decode(bootstrapper_file.read())) |
291 |
| - with zip_ref.open(job_spec_path, "r") as job_spec_file: |
292 |
| - job_spec = job_spec_file.read().decode("utf-8") |
| 279 | + with zip_ref.open(bootstrapper_path, "r") as bootstrapper_file: |
| 280 | + bootstrapper_json = json.loads(base64.b64decode(bootstrapper_file.read())) |
| 281 | + with zip_ref.open(job_spec_path, "r") as job_spec_file: |
| 282 | + job_spec = job_spec_file.read().decode("utf-8") |
293 | 283 |
|
294 | 284 | return bootstrapper_json, job_spec
|
295 | 285 |
|
@@ -427,13 +417,13 @@ def start_run_if_local(
|
427 | 417 | return snapshot_id
|
428 | 418 |
|
429 | 419 |
|
430 |
| -def _log_subprocess(io, file, show_in_console=False): |
431 |
| - def log_subprocess(io, file, show_in_console): |
432 |
| - for line in iter(io.readline, ""): |
| 420 | +def _log_subprocess(output_io, file, show_in_console=False): |
| 421 | + def log_subprocess(): |
| 422 | + for line in iter(output_io.readline, ""): |
433 | 423 | if show_in_console:
|
434 | 424 | print(line, end="")
|
435 | 425 | file.write(line)
|
436 | 426 |
|
437 |
| - thread = Thread(target=log_subprocess, args=(io, file, show_in_console)) |
| 427 | + thread = Thread(target=log_subprocess) |
438 | 428 | thread.daemon = True
|
439 | 429 | thread.start()
|
0 commit comments