summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Brown <ben.brown@codethink.co.uk>2017-07-18 13:01:43 +0100
committerBen Brown <ben.brown@codethink.co.uk>2017-07-19 15:10:27 +0000
commit3f65a08ca5dca5dee749676d79b4a77c21fa410f (patch)
treeb3d5a1c460020a10a9996c4132d585c325ef042e
parentf85165679ba7c351f5182a2cff2cd857ee86518a (diff)
downloadybd-3f65a08ca5dca5dee749676d79b4a77c21fa410f.tar.gz
Add helper method to create a directory with 'exist_ok' optional arg
Will optionally ignore errors if the directory already exists.
-rw-r--r--ybd/rpm.py14
-rw-r--r--ybd/sandbox.py7
-rw-r--r--ybd/utils.py8
3 files changed, 12 insertions, 17 deletions
diff --git a/ybd/rpm.py b/ybd/rpm.py
index 3307951..ebc085b 100644
--- a/ybd/rpm.py
+++ b/ybd/rpm.py
@@ -14,7 +14,7 @@ import yaml
import repos
import requests
import tempfile
-import errno
+import utils
# Because rpm is otherwise totally broken
@@ -572,16 +572,8 @@ def package_rpms(system, whitelist=None):
command = 'rpm ' + common_rpm_args + ' --initdb'
# os.path.exists turned out to be unreliable, so ensuring the
# directories exist is done via catching any EEXIST errors.
- try:
- os.makedirs(rpmdb_path)
- except OSError as e:
- if e.errno == errno.EEXIST:
- pass
- try:
- os.makedirs(rpmdir)
- except OSError as e:
- if e.errno == errno.EEXIST:
- pass
+ utils.makedirs(rpmdb_path, exist_ok=True)
+ utils.makedirs(rpmdir, exist_ok=True)
sandbox.run_sandboxed(system, command, env_vars)
diff --git a/ybd/sandbox.py b/ybd/sandbox.py
index fe72b07..2a6c911 100644
--- a/ybd/sandbox.py
+++ b/ybd/sandbox.py
@@ -18,7 +18,6 @@
import sandboxlib
import contextlib
import os
-import errno
import pipes
import shutil
import stat
@@ -79,11 +78,7 @@ def install(dn, component, subdir=None):
destdir = dn['sandbox']
else:
destdir = os.path.join(dn['sandbox'], subdir)
- try:
- os.mkdir(destdir)
- except OSError as e:
- if e.errno == errno.EEXIST:
- pass
+ utils.makedirs(destdir, exist_ok=True)
# populate destdir with the artifact files from component
if os.path.exists(os.path.join(destdir, 'baserock',
component['name'] + '.meta')):
diff --git a/ybd/utils.py b/ybd/utils.py
index a3493e7..9ee321e 100644
--- a/ybd/utils.py
+++ b/ybd/utils.py
@@ -490,3 +490,11 @@ def tempdir(dir=None):
yield tempdir
finally:
shutil.rmtree(tempdir, ignore_errors=True)
+
+
+def makedirs(name, exist_ok=False, **kwargs):
+ try:
+ os.makedirs(name, **kwargs)
+ except OSError as e:
+ if exist_ok and e.errno == errno.EEXIST:
+ pass