Skip to content

Commit 6428354

Browse files
committed
Merge branch 'develop' into tests_python2
2 parents 6c026b3 + 46654a6 commit 6428354

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

easybuild/tools/filetools.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
from easybuild.tools.config import DEFAULT_WAIT_ON_LOCK_INTERVAL, ERROR, GENERIC_EASYBLOCK_PKG, IGNORE, WARN
6666
from easybuild.tools.config import build_option, install_path
6767
from easybuild.tools.output import PROGRESS_BAR_DOWNLOAD_ONE, start_progress_bar, stop_progress_bar, update_progress_bar
68-
from easybuild.tools.py2vs3 import HTMLParser, load_source, std_urllib, string_type
68+
from easybuild.tools.py2vs3 import HTMLParser, load_source, makedirs, std_urllib, string_type
6969
from easybuild.tools.utilities import natural_keys, nub, remove_unwanted_chars, trace_msg
7070

7171
try:
@@ -1918,15 +1918,9 @@ def mkdir(path, parents=False, set_gid=None, sticky=None):
19181918
# climb up until we hit an existing path or the empty string (for relative paths)
19191919
while existing_parent_path and not os.path.exists(existing_parent_path):
19201920
existing_parent_path = os.path.dirname(existing_parent_path)
1921-
os.makedirs(path)
1921+
makedirs(path, exist_ok=True)
19221922
else:
19231923
os.mkdir(path)
1924-
except FileExistsError as err:
1925-
if os.path.exists(path):
1926-
# This may happen if a parallel build creates the directory after we checked for its existence
1927-
_log.debug("Directory creation aborted as it seems it was already created: %s", err)
1928-
else:
1929-
raise EasyBuildError("Failed to create directory %s: %s", path, err)
19301924
except OSError as err:
19311925
raise EasyBuildError("Failed to create directory %s: %s", path, err)
19321926

easybuild/tools/py2vs3/py2.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import ConfigParser as configparser # noqa
3636
import imp
3737
import json
38+
import os
3839
import subprocess
3940
import time
4041
import urllib2 as std_urllib # noqa
@@ -115,3 +116,11 @@ def sort_looseversions(looseversions):
115116
# with Python 2, we can safely use 'sorted' on LooseVersion instances
116117
# (but we can't in Python 3, see https://bugs.python.org/issue14894)
117118
return sorted(looseversions)
119+
120+
121+
def makedirs(name, mode=0o777, exist_ok=False):
122+
try:
123+
os.makedirs(name, mode)
124+
except OSError:
125+
if not exist_ok or not os.path.isdir(name):
126+
raise

easybuild/tools/py2vs3/py3.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
from html.parser import HTMLParser # noqa
4545
from itertools import zip_longest
4646
from io import StringIO # noqa
47+
from os import makedirs # noqa
4748
from string import ascii_letters, ascii_lowercase # noqa
4849
from urllib.request import HTTPError, HTTPSHandler, Request, URLError, build_opener, urlopen # noqa
4950
from urllib.parse import urlencode # noqa

test/framework/filetools.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from unittest import TextTestRunner
4646
from easybuild.tools import run
4747
import easybuild.tools.filetools as ft
48+
import easybuild.tools.py2vs3 as py2vs3
4849
from easybuild.tools.build_log import EasyBuildError
4950
from easybuild.tools.config import IGNORE, ERROR, build_option, update_build_option
5051
from easybuild.tools.multidiff import multidiff
@@ -3389,6 +3390,17 @@ def test_set_gid_sticky_bits(self):
33893390
self.assertEqual(dir_perms & stat.S_ISGID, stat.S_ISGID)
33903391
self.assertEqual(dir_perms & stat.S_ISVTX, stat.S_ISVTX)
33913392

3393+
def test_compat_makedirs(self):
3394+
"""Test compatibility layer for Python3 os.makedirs"""
3395+
name = os.path.join(self.test_prefix, 'folder')
3396+
self.assertNotExists(name)
3397+
py2vs3.makedirs(name)
3398+
self.assertExists(name)
3399+
# exception is raised because file exists (OSError in Python 2, FileExistsError in Python 3)
3400+
self.assertErrorRegex(Exception, '.*', py2vs3.makedirs, name)
3401+
py2vs3.makedirs(name, exist_ok=True) # No error
3402+
self.assertExists(name)
3403+
33923404
def test_create_unused_dir(self):
33933405
"""Test create_unused_dir function."""
33943406
path = ft.create_unused_dir(self.test_prefix, 'folder')

0 commit comments

Comments
 (0)