1
0
mirror of https://github.com/wting/autojump synced 2024-10-27 20:34:07 +00:00

cosmetic changes

This commit is contained in:
Joël Schaerer 2011-09-27 15:47:24 +02:00
parent 4e3d55f9d0
commit 27617eb64f

View File

@ -26,6 +26,7 @@ from sys import argv, stderr, version_info, exit, getfilesystemencoding
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
from operator import itemgetter from operator import itemgetter
import os import os
import shutil
AUTOJUMP_VERSION = "release v17" AUTOJUMP_VERSION = "release v17"
MAX_KEYWEIGHT = 1000 MAX_KEYWEIGHT = 1000
@ -77,24 +78,24 @@ def unico(text):
def save(path_dict, dic_file): def save(path_dict, dic_file):
"""Save the database in an atomic way, and preserve """Save the database in an atomic way, and preserve
a backup file.""" a backup file."""
# If the dic_file exists and os is not windows, check that it belongs to us # If the dic_file exists and os supports permissions, check that dic_file belongs to us
# Otherwise, fail quietly # Otherwise, fail quietly
if (not os.path.exists(dic_file)) or os.name == 'nt' or os.getuid() == os.stat(dic_file)[4]: if (not os.path.exists(dic_file)) or os.name == 'nt' or os.getuid() == os.stat(dic_file)[4]:
temp = NamedTemporaryFile(dir=CONFIG_DIR, delete=False) temp = NamedTemporaryFile(dir=CONFIG_DIR, delete=False)
for path,weight in sorted(path_dict.items(),key=itemgetter(1),reverse=True): for path,weight in sorted(path_dict.items(),key=itemgetter(1),reverse=True):
# the db is stored in utf-8 # the db is stored in utf-8
temp.write((unico("%s\t%s\n")%(weight,path)).encode("utf-8")) temp.write((unico("%s\t%s\n")%(weight,path)).encode("utf-8"))
#cf. http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/
temp.flush() temp.flush()
os.fsync(temp) os.fsync(temp)
temp.close() temp.close()
#cf. http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/ # Use shutil.move instead of os.rename because windows doesn't support
import shutil # using rename to overwrite files
shutil.move(temp.name, dic_file) shutil.move(temp.name, dic_file)
try: #backup file try: #backup file
import time import time
if (not os.path.exists(dic_file+".bak") or if (not os.path.exists(dic_file+".bak") or
time.time()-os.path.getmtime(dic_file+".bak")>86400): time.time()-os.path.getmtime(dic_file+".bak")>86400):
import shutil
shutil.copy(dic_file, dic_file+".bak") shutil.copy(dic_file, dic_file+".bak")
except OSError as ex: except OSError as ex:
print("Error while creating backup autojump file. (%s)" % print("Error while creating backup autojump file. (%s)" %
@ -116,7 +117,6 @@ def open_dic(dic_file, error_recovery=False):
if not error_recovery and os.path.exists(dic_file+".bak"): if not error_recovery and os.path.exists(dic_file+".bak"):
print('Problem with autojump database,\ print('Problem with autojump database,\
trying to recover from backup...', file=stderr) trying to recover from backup...', file=stderr)
import shutil
shutil.copy(dic_file+".bak", dic_file) shutil.copy(dic_file+".bak", dic_file)
return open_dic(dic_file, True) return open_dic(dic_file, True)
else: else: