diff options
author | Mike Milner <mike.milner@canonical.com> | 2012-01-17 11:47:12 -0400 |
---|---|---|
committer | Mike Milner <mike.milner@canonical.com> | 2012-01-17 11:47:12 -0400 |
commit | 44ea733b7a462442fe2cc3c858001c0cd909dd1b (patch) | |
tree | 0761d92233a90b021d8076846359cf24ba36abfc /cloudinit | |
parent | dfadfb05c1d8fcb50f97952a6ba7ca662babeba4 (diff) | |
download | vyos-cloud-init-44ea733b7a462442fe2cc3c858001c0cd909dd1b.tar.gz vyos-cloud-init-44ea733b7a462442fe2cc3c858001c0cd909dd1b.zip |
Added delete_dir_contents function to cloudinit.util.
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/util.py | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/cloudinit/util.py b/cloudinit/util.py index b690d517..3701f42d 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -18,6 +18,7 @@ import yaml import os import os.path +import shutil import errno import subprocess from Cheetah.Template import Template @@ -127,6 +128,19 @@ def mergedict(src, cand): src[k] = mergedict(src[k], v) return src +def delete_dir_contents(dirname): + """ + Deletes all contents of a directory without deleting the directory itself. + + @param dirname: The directory whose contents should be deleted. + """ + for node in os.listdir(dirname): + node_fullpath = os.path.join(dirname, node) + if os.path.isdir(node_fullpath): + shutil.rmtree(node_fullpath) + else: + os.unlink(node_fullpath) + def write_file(filepath, content, mode=0644, omode="wb"): """ Writes a file with the given content and sets the file mode as specified. |