From 5b00de9a43cc8d0f9697002a796e7d9c832562b9 Mon Sep 17 00:00:00 2001 From: Luca Falavigna Date: Mon, 12 Feb 2024 08:36:11 +0100 Subject: [PATCH 1/4] Use errno.EISDIR instead of hardcoding values --- core/results.py | 6 +++--- hscommon/conflict.py | 6 ++---- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/core/results.py b/core/results.py index 3606e38a..ddc60f14 100644 --- a/core/results.py +++ b/core/results.py @@ -10,6 +10,7 @@ import re import os import os.path as op +from errno import EISDIR from xml.etree import ElementTree as ET from hscommon.jobprogress.job import nulljob @@ -375,9 +376,8 @@ def do_write(outfile): try: do_write(outfile) except OSError as e: - # If our OSError is because dest is already a directory, we want to handle that. 21 is - # the code we get on OS X and Linux, 13 is what we get on Windows. - if e.errno in {21, 13}: + # If our OSError is because dest is already a directory, we want to handle that. + if e.errno == EISDIR: p = str(outfile) dirname, basename = op.split(p) otherfiles = os.listdir(dirname) diff --git a/hscommon/conflict.py b/hscommon/conflict.py index af133ac7..68aa22f0 100644 --- a/hscommon/conflict.py +++ b/hscommon/conflict.py @@ -14,6 +14,7 @@ import os import shutil +from errno import EISDIR from pathlib import Path from typing import Callable, List @@ -75,10 +76,7 @@ def smart_copy(source_path: Path, dest_path: Path) -> None: try: _smart_move_or_copy(shutil.copy, source_path, dest_path) except OSError as e: - if e.errno in { - 21, - 13, - }: # it's a directory, code is 21 on OS X / Linux and 13 on Windows + if e.errno == EISDIR: # it's a directory _smart_move_or_copy(shutil.copytree, source_path, dest_path) else: raise From f16fce64b708adcb8aec0880944367af262988c4 Mon Sep 17 00:00:00 2001 From: Luca Falavigna Date: Mon, 12 Feb 2024 11:33:40 +0100 Subject: [PATCH 2/4] Also look for errno.EACCES to ensure compatibility with Windows --- core/results.py | 4 ++-- hscommon/conflict.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/core/results.py b/core/results.py index ddc60f14..b6f11f28 100644 --- a/core/results.py +++ b/core/results.py @@ -10,7 +10,7 @@ import re import os import os.path as op -from errno import EISDIR +from errno import EISDIR, EACCES from xml.etree import ElementTree as ET from hscommon.jobprogress.job import nulljob @@ -377,7 +377,7 @@ def do_write(outfile): do_write(outfile) except OSError as e: # If our OSError is because dest is already a directory, we want to handle that. - if e.errno == EISDIR: + if e.errno in (EISDIR, EACCES): p = str(outfile) dirname, basename = op.split(p) otherfiles = os.listdir(dirname) diff --git a/hscommon/conflict.py b/hscommon/conflict.py index 68aa22f0..f20f4885 100644 --- a/hscommon/conflict.py +++ b/hscommon/conflict.py @@ -14,7 +14,7 @@ import os import shutil -from errno import EISDIR +rom errno import EISDIR, EACCES from pathlib import Path from typing import Callable, List @@ -76,7 +76,7 @@ def smart_copy(source_path: Path, dest_path: Path) -> None: try: _smart_move_or_copy(shutil.copy, source_path, dest_path) except OSError as e: - if e.errno == EISDIR: # it's a directory + if e.errno in (EISDIR, EACCES): # it's a directory _smart_move_or_copy(shutil.copytree, source_path, dest_path) else: raise From 36212e088327661b0016320e24ca4eea960b04a0 Mon Sep 17 00:00:00 2001 From: Luca Falavigna Date: Mon, 12 Feb 2024 11:35:42 +0100 Subject: [PATCH 3/4] Fix deletion typo --- hscommon/conflict.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hscommon/conflict.py b/hscommon/conflict.py index f20f4885..31e72e54 100644 --- a/hscommon/conflict.py +++ b/hscommon/conflict.py @@ -14,7 +14,7 @@ import os import shutil -rom errno import EISDIR, EACCES +from errno import EISDIR, EACCES from pathlib import Path from typing import Callable, List From a8b98ccf5931e7aa98f7daf87a6abd44cc9793e3 Mon Sep 17 00:00:00 2001 From: Luca Falavigna Date: Mon, 19 Feb 2024 16:35:23 +0100 Subject: [PATCH 4/4] Refactor comments --- core/results.py | 3 ++- hscommon/conflict.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/core/results.py b/core/results.py index b6f11f28..1ba728a9 100644 --- a/core/results.py +++ b/core/results.py @@ -376,7 +376,8 @@ def do_write(outfile): try: do_write(outfile) except OSError as e: - # If our OSError is because dest is already a directory, we want to handle that. + # If our OSError is because dest is already a directory, we want to handle that. 21 is + # the code we get on OS X and Linux (EISDIR), 13 is what we get on Windows (EACCES). if e.errno in (EISDIR, EACCES): p = str(outfile) dirname, basename = op.split(p) diff --git a/hscommon/conflict.py b/hscommon/conflict.py index 31e72e54..0675efd0 100644 --- a/hscommon/conflict.py +++ b/hscommon/conflict.py @@ -76,7 +76,8 @@ def smart_copy(source_path: Path, dest_path: Path) -> None: try: _smart_move_or_copy(shutil.copy, source_path, dest_path) except OSError as e: - if e.errno in (EISDIR, EACCES): # it's a directory + # It's a directory, code is 21 on OS X / Linux (EISDIR) and 13 on Windows (EACCES) + if e.errno in (EISDIR, EACCES): _smart_move_or_copy(shutil.copytree, source_path, dest_path) else: raise