mirror of
https://github.com/clearml/clearml
synced 2025-04-22 07:15:57 +00:00
Add Hydra support phase one: fix current working dir (issue #219). Fix cwd outside of repository root folder
This commit is contained in:
parent
59629d7a15
commit
49b578b979
@ -560,7 +560,8 @@ class ScriptInfo(object):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# Use os.path.relpath as it calculates up dir movements (../)
|
# Use os.path.relpath as it calculates up dir movements (../)
|
||||||
entry_point = os.path.relpath(str(script_path), str(Path.cwd()))
|
entry_point = os.path.relpath(
|
||||||
|
str(script_path), str(cls._get_working_dir(repo_root, return_abs=True)))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# Working directory not under repository root
|
# Working directory not under repository root
|
||||||
entry_point = script_path.relative_to(repo_root)
|
entry_point = script_path.relative_to(repo_root)
|
||||||
@ -568,14 +569,30 @@ class ScriptInfo(object):
|
|||||||
return Path(entry_point).as_posix()
|
return Path(entry_point).as_posix()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _get_working_dir(cls, repo_root):
|
def _get_working_dir(cls, repo_root, return_abs=False):
|
||||||
repo_root = Path(repo_root).absolute()
|
repo_root = Path(repo_root).absolute()
|
||||||
|
cwd = None
|
||||||
|
|
||||||
|
# check if running with hydra
|
||||||
|
if sys.modules.get('hydra'):
|
||||||
|
# noinspection PyBroadException
|
||||||
|
try:
|
||||||
|
# noinspection PyPackageRequirements
|
||||||
|
import hydra
|
||||||
|
cwd = Path(hydra.utils.get_original_cwd()).absolute()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if not cwd:
|
||||||
|
cwd = Path.cwd().absolute()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return Path.cwd().relative_to(repo_root).as_posix()
|
# do not change: test if we are under the repo root folder, it will throw an exception if we are not
|
||||||
|
relative = cwd.relative_to(repo_root).as_posix()
|
||||||
|
return cwd.as_posix() if return_abs else relative
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# Working directory not under repository root
|
# Working directory not under repository root, default to repo root
|
||||||
return os.path.curdir
|
return repo_root.as_posix() if return_abs else '.'
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _get_script_code(cls, script_path):
|
def _get_script_code(cls, script_path):
|
||||||
|
Loading…
Reference in New Issue
Block a user