summaryrefslogtreecommitdiffstats
path: root/base/common/python/pki/util.py
diff options
context:
space:
mode:
authorEndi Sukma Dewata <edewata@redhat.com>2013-05-30 23:35:41 -0400
committerEndi S. Dewata <edewata@redhat.com>2013-07-08 11:29:14 -0400
commited2b7740614aafbdac405c898542a5c26d794409 (patch)
treeaec98d78d97f955f4d373d90af50d126ab9c1d49 /base/common/python/pki/util.py
parent73be9e5fa4a49864092e7a5cc9521d4bbd82fe45 (diff)
downloadpki-ed2b7740614aafbdac405c898542a5c26d794409.tar.gz
pki-ed2b7740614aafbdac405c898542a5c26d794409.tar.xz
pki-ed2b7740614aafbdac405c898542a5c26d794409.zip
Added support to backup folders during upgrade.
The upgrade framework has been updated to support backup and restore operations for folders and their contents. Ticket #583
Diffstat (limited to 'base/common/python/pki/util.py')
-rw-r--r--base/common/python/pki/util.py69
1 files changed, 65 insertions, 4 deletions
diff --git a/base/common/python/pki/util.py b/base/common/python/pki/util.py
index 9b501a342..4d25390a6 100644
--- a/base/common/python/pki/util.py
+++ b/base/common/python/pki/util.py
@@ -22,13 +22,74 @@
import os
import shutil
+def copy(source, dest):
+ """
+ Copy a file or a folder and its contents.
+ """
+
+ # remove trailing slashes
+ if source[-1] == '/': source = source[:-1]
+ if dest[-1] == '/': dest = dest[:-1]
+
+ sourceparent = os.path.dirname(source)
+ destparent = os.path.dirname(dest)
+
+ copydirs(sourceparent, destparent)
+
+ if os.path.isfile(source):
+ copyfile(source, dest)
+
+ else:
+ for sourcepath, _, filenames in os.walk(source):
+
+ relpath = sourcepath[len(source):]
+ destpath = dest + relpath
+ if destpath == '': destpath = '/'
+
+ copydirs(sourcepath, destpath)
+
+ for filename in filenames:
+ sourcefile = os.path.join(sourcepath, filename)
+ targetfile = os.path.join(destpath, filename)
+ copyfile(sourcefile, targetfile)
def copyfile(source, dest):
+ """
+ Copy a file or link while preserving its attributes.
+ """
+
+ if os.path.islink(source):
+ target = os.readlink(source)
+ os.symlink(target, dest)
+
+ st = os.lstat(source)
+ os.lchown(dest, st.st_uid, st.st_gid)
+
+ else:
+ shutil.copyfile(source, dest)
+
+ st = os.stat(source)
+ os.utime(dest, (st.st_atime, st.st_mtime))
+ os.chmod(dest, st.st_mode)
+ os.chown(dest, st.st_uid, st.st_gid)
+
+def copydirs(source, dest):
+ """
+ Copy a folder and its parents while preserving their attributes.
+ """
+
+ if os.path.exists(dest):
+ return
+
+ destparent = os.path.dirname(dest)
+
+ if not os.path.exists(destparent):
+ sourceparent = os.path.dirname(source)
+ copydirs(sourceparent, destparent)
- dest_dir = os.path.dirname(dest)
- if not os.path.exists(dest_dir):
- os.makedirs(dest_dir)
+ os.mkdir(dest)
- shutil.copy2(source, dest)
st = os.stat(source)
+ os.utime(dest, (st.st_atime, st.st_mtime))
+ os.chmod(dest, st.st_mode)
os.chown(dest, st.st_uid, st.st_gid)