From ef7ea1b32e3bddd43e12f5cb6dfa7c9fa379ec71 Mon Sep 17 00:00:00 2001 From: lainme Date: Fri, 17 Jan 2025 16:03:22 +0800 Subject: [PATCH] fix running in bash environment on windows --- src/aiida/cmdline/commands/cmd_computer.py | 4 +++- src/aiida/manage/configuration/config.py | 4 +++- src/aiida/manage/profile_access.py | 3 ++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/aiida/cmdline/commands/cmd_computer.py b/src/aiida/cmdline/commands/cmd_computer.py index 42feb36d25..d0630bbde7 100644 --- a/src/aiida/cmdline/commands/cmd_computer.py +++ b/src/aiida/cmdline/commands/cmd_computer.py @@ -136,12 +136,14 @@ def _computer_create_temp_file(transport, scheduler, authinfo, computer): transport.makedirs(workdir, ignore_existing=True) - with tempfile.NamedTemporaryFile(mode='w+') as tempf: + with tempfile.NamedTemporaryFile(mode='w+', delete=False) as tempf: fname = os.path.split(tempf.name)[1] remote_file_path = os.path.join(workdir, fname) tempf.write(file_content) tempf.flush() + tempf.close() transport.putfile(tempf.name, remote_file_path) + os.remove(tempf.name) if not transport.path_exists(remote_file_path): return False, f'failed to create the file `{remote_file_path}` on the remote' diff --git a/src/aiida/manage/configuration/config.py b/src/aiida/manage/configuration/config.py index fff83f4330..2bf3cc5cdb 100644 --- a/src/aiida/manage/configuration/config.py +++ b/src/aiida/manage/configuration/config.py @@ -21,6 +21,7 @@ import json import os import uuid +import shutil from pathlib import Path from typing import Any, Dict, List, Optional, Tuple @@ -780,7 +781,8 @@ def _atomic_write(self, filepath=None): os.umask(umask) handle.flush() - os.rename(handle.name, self.filepath) + handle.close() + shutil.move(handle.name, self.filepath) def filepaths(self, profile: Profile): """Return the filepaths used by a profile. diff --git a/src/aiida/manage/profile_access.py b/src/aiida/manage/profile_access.py index c65364af03..be87be3e05 100644 --- a/src/aiida/manage/profile_access.py +++ b/src/aiida/manage/profile_access.py @@ -11,6 +11,7 @@ import contextlib import os import typing +import shutil from pathlib import Path import psutil @@ -76,7 +77,7 @@ def request_access(self) -> None: # it will read the incomplete command, won't be able to correctly compare it with its running # process, and will conclude the record is old and clean it up. filepath_tmp.write_text(str(self.process.cmdline())) - os.rename(filepath_tmp, filepath_pid) + shutil.move(filepath_tmp, filepath_pid) # Check again in case a lock was created in the time between the first check and creating the # access record file.