add purge option to clear database of missing entries

pull/125/head
William Ting 12 years ago
parent a4cbff774b
commit 60a9c1c2f1

@ -114,6 +114,8 @@ Options must be passed to 'autojump' and not the 'j' wrapper function.
-a, --add DIR manually add path to database
--purge deletes all database entries that no longer exist on system
--stat show database entries and their key weights
--version show version information and exit

@ -1,4 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright © 2008-2012 Joel Schaerer
Copyright © 2012 William Ting
@ -28,7 +29,7 @@ import shutil
import sys
from tempfile import NamedTemporaryFile
VERSION = 'release-v20-3'
VERSION = 'release-v20-4'
MAX_KEYWEIGHT = 1000
MAX_STORED_PATHS = 1000
COMPLETION_SEPARATOR = '__'
@ -122,6 +123,16 @@ class Database:
self.trim()
self.save()
def purge(self):
""" Deletes all entries that no longer exist on system. """
removed = []
for path in self.data.keys():
if not os.path.exists(path):
removed.append(path)
del self.data[path]
self.save()
return removed
def save(self):
""" Save database atomically and preserve backup. """
# check file existence and permissions
@ -186,6 +197,8 @@ def options():
help='enclose directory quotes to prevent errors')
parser.add_argument('--complete', action="store_true", default=False,
help='used for bash tab completion')
parser.add_argument('--purge', action="store_true", default=False,
help='delete all database entries that no longer exist on system')
parser.add_argument('--stat', action="store_true", default=False,
help='show database entries and their key weights')
parser.add_argument('--version', action="version", version="%(prog)s " + VERSION,
@ -201,6 +214,15 @@ def options():
db.save()
return True
if (ARGS.purge):
db = Database(DB_FILE)
removed = db.purge()
if len(removed) > 0:
for dir in removed:
output(unico(dir))
print("Number of database entries removed: %d" % len(removed))
return True
if (ARGS.stat):
db = Database(DB_FILE)
dirs = list(db.data.items())

@ -35,6 +35,8 @@ wrapper function.
\f[C]
-a,\ --add\ DIR\ \ \ \ \ \ \ manually\ add\ path\ to\ database
--purge\ \ \ \ \ \ \ \ \ \ \ \ \ deletes\ all\ database\ entries\ that\ no\ longer\ exist\ on\ system
--stat\ \ \ \ \ \ \ \ \ \ \ \ \ \ show\ database\ entries\ and\ their\ key\ weights
--version\ \ \ \ \ \ \ \ \ \ \ show\ version\ information\ and\ exit

@ -4,6 +4,8 @@ Options must be passed to 'autojump' and not the 'j' wrapper function.
-a, --add DIR manually add path to database
--purge deletes all database entries that no longer exist on system
--stat show database entries and their key weights
--version show version information and exit

@ -15,4 +15,4 @@ else
gitrevision="$1"
fi
sed -i "s/^AUTOJUMP_VERSION = \".*\"$/AUTOJUMP_VERSION = \"$gitrevision\"/" ./bin/autojump
sed -i "s/^VERSION = \".*\"$/VERSION = \"$gitrevision\"/" ./bin/autojump

Loading…
Cancel
Save