Skip to content

Commit

Permalink
Add makedirs support to file.rename
Browse files Browse the repository at this point in the history
  • Loading branch information
thatch45 committed Jun 27, 2012
1 parent b9b0a36 commit 8e51797
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions salt/states/file.py
Expand Up @@ -1668,17 +1668,14 @@ def rename(name, source, force=False, makedirs=False):
The location of the file to rename to
source
The location
The location of the file to move to the location specified with name
source_hash:
This can be either a file which contains a source hash string for
the source, or a source hash string. The source hash string is the
hash algorithm followed by the hash of the file:
md5=e138491e9d5b97023cea823fe17bac22
force:
If the target location is present then the file will not be moved,
specify "force: True" to overwrite the target file
user
The user to own the file, this defaults to the user salt is running as
on the minion
makedirs:
If the target subdirectories don't exist create them
'''
ret = {
Expand Down Expand Up @@ -1709,7 +1706,16 @@ def rename(name, source, force=False, makedirs=False):
)
ret['result'] = None
return ret

# Run makedirs
dname = os.path.dirname(name)
if not os.path.isdir(dname):
if makedirs:
os.makedirs(dname)
else:
return _error(
ret,
'The target directory {0} is not present'.format(dname))
# All tests pass, move the file into place
try:
shutil.move(source, name)
except (IOError, OSError):
Expand Down

0 comments on commit 8e51797

Please sign in to comment.