aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRonan Amicel <ronan.amicel@gmail.com>2012-10-13 18:30:02 +0200
committerRonan Amicel <ronan.amicel@gmail.com>2012-10-13 18:30:02 +0200
commitfa6c4a409ae315115a75bee7fcacbba0315abb1c (patch)
treef38b745c3b82ba5956ae01211df5e3be3240a5ee
parentAdd functions to get a file's owner, group and mode (diff)
downloadfabtools-fa6c4a409ae315115a75bee7fcacbba0315abb1c.tar.xz
Only change owner or permissions when needed
-rw-r--r--fabtools/require/files.py18
1 files changed, 13 insertions, 5 deletions
diff --git a/fabtools/require/files.py b/fabtools/require/files.py
index 54f08c9..e13fcac 100644
--- a/fabtools/require/files.py
+++ b/fabtools/require/files.py
@@ -15,6 +15,7 @@ from urlparse import urlparse
from fabric.api import *
from fabtools.files import is_file, is_dir, md5sum
+import fabtools.files
BLOCKSIZE = 2 ** 20 # 1MB
@@ -28,18 +29,24 @@ def directory(path, use_sudo=False, owner='', group='', mode=''):
from fabtools import require
- require.directory('/tmp/mydir', owner='alice')
+ require.directory('/tmp/mydir', owner='alice', use_sudo=True)
.. note:: This function can be accessed directly from the
``fabtools.require`` module for convenience.
"""
func = use_sudo and sudo or run
+
if not is_dir(path):
func('mkdir -p "%(path)s"' % locals())
- if owner:
+
+ # Ensure correct owner
+ if (owner and fabtools.files.owner(path, use_sudo) != owner) or \
+ (group and fabtools.files.group(path, use_sudo) != group):
func('chown %(owner)s:%(group)s "%(path)s"' % locals())
- if mode:
+
+ # Ensure correct mode
+ if mode and fabtools.files.mode(path, use_sudo) != mode:
func('chmod %(mode)s "%(path)s"' % locals())
@@ -136,11 +143,12 @@ def file(path=None, contents=None, source=None, url=None, md5=None,
os.unlink(source)
# Ensure correct owner
- if owner:
+ if (owner and fabtools.files.owner(path, use_sudo) != owner) or \
+ (group and fabtools.files.group(path, use_sudo) != group):
func('chown %(owner)s:%(group)s "%(path)s"' % locals())
# Ensure correct mode
- if mode:
+ if mode and fabtools.files.mode(path, use_sudo) != mode:
func('chmod %(mode)s "%(path)s"' % locals())