summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2021-07-01 20:46:43 +0200
committerChristian Poessinger <christian@poessinger.com>2021-07-01 20:46:43 +0200
commit63e6c00864a8a4134c9bd3187d2422a6511c84b0 (patch)
tree2883e8d2b8282d60af3d58f656bb184d0e82fc99
parentf4dd2ea487d6dc11fdc73d4218bb23bd7b9ca2f9 (diff)
downloadvyos-1x-63e6c00864a8a4134c9bd3187d2422a6511c84b0.tar.gz
vyos-1x-63e6c00864a8a4134c9bd3187d2422a6511c84b0.zip
vyos.util: fix IsADirectoryError and SameFileError for copy_file
Commit 5303ec39 ("vyos.util: add new helper copy_file()") added a new helper function to copy a file from A -> B and create the destination directory if required. It did also throw an excpetion if the destination file already existed and consisted of the same file - this is now ignored and we always copy the source to the destination.
-rw-r--r--python/vyos/util.py10
1 files changed, 8 insertions, 2 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py
index c318d58de..446285a1b 100644
--- a/python/vyos/util.py
+++ b/python/vyos/util.py
@@ -231,8 +231,14 @@ def copy_file(source, destination, mkdstdir=False, user=None, group=None):
dirname = os.path.dirname(destination)
if not os.path.isdir(dirname):
makedir(dirname, user, group)
-
- shutil.copyfile(source, destination)
+ try:
+ filename = os.path.basename(source)
+ if not destination.endswith('/'):
+ destination + '/'
+ shutil.copyfile(source, destination + filename)
+ except shutil.SameFileError:
+ # We don't care if we copy the same file again
+ pass
chown(destination, user, group)
def read_json(fname, defaultonfailure=None):