From 8f28d2882a05cff5df58f10d192846e59db1c0dd Mon Sep 17 00:00:00 2001 From: clearml <> Date: Mon, 24 Feb 2025 13:23:50 +0200 Subject: [PATCH] Fix pip freeze dump to comply with yaml fancy print --- clearml_agent/commands/worker.py | 7 +++-- clearml_agent/helper/base.py | 26 ++++++++++++++++++- .../helper/package/pip_api/system.py | 8 +++++- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/clearml_agent/commands/worker.py b/clearml_agent/commands/worker.py index b1c9efc..34bbbe5 100644 --- a/clearml_agent/commands/worker.py +++ b/clearml_agent/commands/worker.py @@ -91,7 +91,6 @@ from clearml_agent.errors import ( from clearml_agent.helper.base import ( return_list, print_parameters, - dump_yaml, warning, normalize_path, check_directory_path, @@ -110,7 +109,7 @@ from clearml_agent.helper.base import ( is_linux_platform, rm_file, add_python_path, - safe_remove_tree, get_python_version, + safe_remove_tree, get_python_version, dump_flat_dict, ) from clearml_agent.helper.check_update import start_check_update_daemon from clearml_agent.helper.console import ensure_text, print_text, decode_binary_lines @@ -2471,7 +2470,7 @@ class Worker(ServiceCommandSection): print("Restoring running environment of task id [%s]:" % task_id) if freeze: print("Summary - installed python packages:") - print(dump_yaml(freeze)) + print(dump_flat_dict(freeze)) else: print("No freeze information available") @@ -2989,7 +2988,7 @@ class Worker(ServiceCommandSection): if freeze: print("Summary - installed python packages:") - print(dump_yaml(freeze)) + print(dump_flat_dict(freeze)) else: print("No freeze information available") diff --git a/clearml_agent/helper/base.py b/clearml_agent/helper/base.py index fab4b97..4fda446 100644 --- a/clearml_agent/helper/base.py +++ b/clearml_agent/helper/base.py @@ -22,7 +22,7 @@ from .._vendor import furl from .._vendor import six from .._vendor.attr import fields_dict from .._vendor.pathlib2 import Path -from .._vendor.six.moves import reduce +from .._vendor.six.moves import reduce # noqa from .._vendor import pyyaml as yaml from clearml_agent.errors import CommandFailedError @@ -310,6 +310,30 @@ def dump_yaml(obj, path=None, dump_all=False, **kwargs): dump_func(obj, output, **base_kwargs) +def _dump_flat_dict(flat_dict): + if not isinstance(flat_dict, (dict, )): + flat_dict = {"": flat_dict} + + out = "" + for k in flat_dict.keys(): + out += "{}:\n".format(k) + values = flat_dict[k] + if not isinstance(values, (list, tuple)): + values = [values] + for v in values: + out += "- {}\n".format(v) + + return out + + +def dump_flat_dict(flat_dict): + # noinspection PyBroadException + try: + return _dump_flat_dict(flat_dict) + except Exception: + return dump_yaml(flat_dict) + + def one_value(dct): return next(iter(six.itervalues(dct))) diff --git a/clearml_agent/helper/package/pip_api/system.py b/clearml_agent/helper/package/pip_api/system.py index 23daef5..790e4e4 100644 --- a/clearml_agent/helper/package/pip_api/system.py +++ b/clearml_agent/helper/package/pip_api/system.py @@ -32,7 +32,13 @@ class SystemPip(PackageManager): pass def install_from_file(self, path): - self.run_with_env(('install', '-r', path) + self.install_flags(), cwd=self.cwd) + try: + self.run_with_env(('install', '-r', path) + self.install_flags(), cwd=self.cwd) + except Exception: + print("ERROR: installing pip requirements failed, requirements.txt:\n{}\n".format( + "\n".join([line for line in Path(path).read_text().splitlines() if line.strip()]) + )) + raise def install_packages(self, *packages): self._install(*(packages + self.install_flags()))