diff --git a/elodie/compatability.py b/elodie/compatability.py index e0d8576..d4a68dd 100644 --- a/elodie/compatability.py +++ b/elodie/compatability.py @@ -63,3 +63,13 @@ def _copyfile(src, dst): os.close(fout) except: pass + + +# If you want cross-platform overwriting of the destination, +# use os.replace() instead of rename(). +# https://docs.python.org/3/library/os.html#os.rename +def _rename(src, dst): + if (constants.python_version == 3): + return os.replace(src, dst) + else: + return os.rename(src, dst) diff --git a/elodie/tests/helper.py b/elodie/tests/helper.py index bc17951..bda3a34 100644 --- a/elodie/tests/helper.py +++ b/elodie/tests/helper.py @@ -14,6 +14,7 @@ import urllib from datetime import datetime from datetime import timedelta +from elodie.compatability import _rename from elodie import constants def checksum(file_path, blocksize=65536): @@ -148,19 +149,30 @@ def isclose(a, b, rel_tol = 1e-8): diff <= abs(rel_tol * b)) def reset_dbs(): - hash_db = constants.hash_db - if os.path.isfile(hash_db): - os.rename(hash_db, '{}-test'.format(hash_db)) - - location_db = constants.location_db - if os.path.isfile(location_db): - os.rename(location_db, '{}-test'.format(location_db)) - -def restore_dbs(): + """ Back up hash_db and location_db """ hash_db = '{}-test'.format(constants.hash_db) - if os.path.isfile(hash_db): - os.rename(hash_db, hash_db.replace('-test', '')) + if not os.path.isfile(hash_db): + hash_db = constants.hash_db + if os.path.isfile(hash_db): + _rename(hash_db, '{}-test'.format(hash_db)) + #else restore_dbs wasn't called by a previous test, keep the + #existing hash_db backup + location_db = '{}-test'.format(constants.location_db) if os.path.isfile(location_db): - os.rename(location_db, location_db.replace('-test', '')) + location_db = constants.location_db + if os.path.isfile(location_db): + _rename(location_db, '{}-test'.format(location_db)) + #else restore_dbs wasn't called by a previous test, keep the + #existing location_db backup + +def restore_dbs(): + """ Restore back ups of hash_db and location_db """ + hash_db = '{}-test'.format(constants.hash_db) + if os.path.isfile(hash_db): + _rename(hash_db, hash_db.replace('-test', '')) + + location_db = '{}-test'.format(constants.location_db) + if os.path.isfile(location_db): + _rename(location_db, location_db.replace('-test', ''))