-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename utils.misc to utils.file_utils; add safe_rename
- Loading branch information
Showing
6 changed files
with
96 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright 2018,2019 by MPI-SWS and Data-ken Research. Licensed under Apache 2.0. See LICENSE.txt. | ||
""" | ||
File-related utilities | ||
""" | ||
|
||
import os | ||
from os.path import dirname | ||
import shutil | ||
|
||
from dataworkspaces.errors import ConfigurationError | ||
|
||
def remove_dir_if_empty(path, base_dir, verbose=False): | ||
"""Remove an empty directory and any parents that are empty, | ||
up to base_dir. | ||
""" | ||
if path==base_dir: | ||
return | ||
elif len(os.listdir(path))==0: | ||
os.rmdir(path) | ||
if verbose: | ||
print("Removing (now) empty directory %s" % path) | ||
remove_dir_if_empty(dirname(path), base_dir, verbose) | ||
|
||
|
||
def safe_rename(src, dest): | ||
"""Safe replacement for os.rename(). The problem is that os.rename() | ||
does not work across file systems. In that case, you need to actually | ||
copy the file | ||
""" | ||
try: | ||
os.rename(src, dest) | ||
except OSError: | ||
try: | ||
shutil.copyfile(src, dest) | ||
os.remove(src) | ||
except Exception as e: | ||
raise ConfigurationError("Unable to copy %s to %s: %s"% | ||
(src, dest, e)) from e | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Test file utilities | ||
""" | ||
import os.path | ||
import unittest | ||
import tempfile | ||
import sys | ||
import shutil | ||
|
||
TEMPDIR=os.path.abspath(os.path.expanduser(__file__)).replace('.py', '_data') | ||
|
||
OTHER_FS='/tmp' | ||
|
||
try: | ||
import dataworkspaces | ||
except ImportError: | ||
sys.path.append(os.path.abspath("..")) | ||
|
||
from dataworkspaces.utils.file_utils import safe_rename | ||
|
||
class TestFileUtils(unittest.TestCase): | ||
def setUp(self): | ||
if os.path.exists(TEMPDIR): | ||
shutil.rmtree(TEMPDIR) | ||
os.mkdir(TEMPDIR) | ||
|
||
def tearDown(self): | ||
if os.path.exists(TEMPDIR): | ||
shutil.rmtree(TEMPDIR) | ||
|
||
def test_safe_rename(self): | ||
"""This test needs to be run in a case where OTHER_FS is on a separate | ||
filesystem - the underlying issue is that os.rename() does not work | ||
across filesystems and this version does copying as a fallback. | ||
""" | ||
data = "this is a test\n" | ||
with tempfile.NamedTemporaryFile(dir=OTHER_FS, delete=False) as testfile: | ||
testfile.write(data.encode('utf-8')) | ||
try: | ||
dest = os.path.join(TEMPDIR, 'data.txt') | ||
safe_rename(testfile.name, dest) | ||
self.assertTrue(os.path.exists(dest)) | ||
with open(dest, 'r') as f: | ||
data2 = f.read() | ||
self.assertEqual(data, data2) | ||
finally: | ||
if os.path.exists(testfile.name): | ||
os.remove(testfile.name) | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |