mirror of
https://github.com/wting/autojump
synced 2024-10-27 20:34:07 +00:00
use the backup file in case of problems
This commit is contained in:
parent
99d82be473
commit
eb0f287eb9
25
autojump
25
autojump
@ -56,12 +56,12 @@ def save(path_dict,dic_file):
|
|||||||
f.close()
|
f.close()
|
||||||
try:
|
try:
|
||||||
os.rename(dic_file+".tmp",dic_file) #cf. http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/
|
os.rename(dic_file+".tmp",dic_file) #cf. http://thunk.org/tytso/blog/2009/03/15/dont-fear-the-fsync/
|
||||||
except OSError:
|
|
||||||
pass #Fail quietly, this usually means a concurrent autojump process already did the job
|
|
||||||
import time #backup file
|
import time #backup file
|
||||||
if not os.path.exists(dic_file+".bak") or time.time()-os.path.getmtime(dic_file+".bak")>86400:
|
if not os.path.exists(dic_file+".bak") or time.time()-os.path.getmtime(dic_file+".bak")>86400:
|
||||||
import shutil
|
import shutil
|
||||||
shutil.copy(dic_file,dic_file+".bak")
|
shutil.copy(dic_file,dic_file+".bak")
|
||||||
|
except OSError:
|
||||||
|
pass #Fail quietly, this usually means a concurrent autojump process already did the job
|
||||||
|
|
||||||
def forget(path_dict,dic_file):
|
def forget(path_dict,dic_file):
|
||||||
"""Gradually forget about directories. Only call from the actual jump since it can take time"""
|
"""Gradually forget about directories. Only call from the actual jump since it can take time"""
|
||||||
@ -78,6 +78,19 @@ def find_matches(dirs,pattern,path_dict,result_list,re_flags,max_matches):
|
|||||||
if match(path,pattern,path_dict,re_flags):
|
if match(path,pattern,path_dict,re_flags):
|
||||||
uniqadd(result_list,path)
|
uniqadd(result_list,path)
|
||||||
|
|
||||||
|
def open_dic(dic_file,error_recovery=False):
|
||||||
|
try:
|
||||||
|
aj_file=open(dic_file)
|
||||||
|
path_dict=cPickle.load(aj_file)
|
||||||
|
aj_file.close()
|
||||||
|
return path_dict
|
||||||
|
except (IOError,EOFError):
|
||||||
|
if not error_recovery and os.path.exists(dic_file+".bak"):
|
||||||
|
import shutil
|
||||||
|
shutil.copy(dic_file+".bak",dic_file)
|
||||||
|
return open_dic(dic_file,True)
|
||||||
|
else: return {} #if everything fails, return an empty file
|
||||||
|
|
||||||
#Main code
|
#Main code
|
||||||
try:
|
try:
|
||||||
optlist, args = getopt.getopt(argv[1:], 'a',['stat','import','completion'])
|
optlist, args = getopt.getopt(argv[1:], 'a',['stat','import','completion'])
|
||||||
@ -86,13 +99,7 @@ except getopt.GetoptError, e:
|
|||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
dic_file=os.path.expanduser("~/.autojump_py")
|
dic_file=os.path.expanduser("~/.autojump_py")
|
||||||
try:
|
path_dict=open_dic(dic_file)
|
||||||
aj_file=open(dic_file)
|
|
||||||
path_dict=cPickle.load(aj_file)
|
|
||||||
aj_file.close()
|
|
||||||
except IOError:
|
|
||||||
path_dict={}
|
|
||||||
|
|
||||||
if ('-a','') in optlist:
|
if ('-a','') in optlist:
|
||||||
if(args[-1] != os.path.expanduser("~")): # home dir can be reached quickly by "cd" and may interfere with other directory
|
if(args[-1] != os.path.expanduser("~")): # home dir can be reached quickly by "cd" and may interfere with other directory
|
||||||
dicadd(path_dict,args[-1])
|
dicadd(path_dict,args[-1])
|
||||||
|
Loading…
Reference in New Issue
Block a user