- if we store it inside the media folder, we inadvertently bump the folder mod
   time every time sqlite creates a journal file

- close/reopen the media db as the deck is closed/opened
This commit is contained in:
Damien Elmes 2011-09-12 05:03:31 +09:00
parent c59dd854fb
commit 87bfb38e2b
3 changed files with 19 additions and 8 deletions

View file

@ -125,12 +125,14 @@ crt=?, mod=?, scm=?, dty=?, lastSync=?, conf=?""",
self.rollback()
self.db.close()
self.db = None
self.media.close()
def reopen(self):
"Reconnect to DB (after changing threads, etc). Doesn't reload."
import anki.db
if not self.db:
self.db = anki.db.DB(self.path)
self.media.connect()
def rollback(self):
self.db.rollback()

View file

@ -22,12 +22,19 @@ class MediaManager(object):
os.makedirs(self._dir)
os.chdir(self._dir)
# change database
path = os.path.join(self.dir(), "media.db")
self.connect()
def connect(self):
path = self.dir()+"db"
create = not os.path.exists(path)
self.db = DB(path)
if create:
self._initDB()
def close(self):
self.db.close()
self.db = None
def dir(self):
return self._dir
@ -124,7 +131,7 @@ If the same name exists, compare checksums."""
# loop through directory and find unused & missing media
unused = []
for file in os.listdir(mdir):
if file.startswith("latex-") or file.startswith("media.db"):
if file.startswith("latex-"):
continue
path = os.path.join(mdir, file)
if not os.path.isfile(path):
@ -159,8 +166,8 @@ If the same name exists, compare checksums."""
# in the log, a mod time of zero indicates a delete
self.db.executescript("""
create table media (fname text primary key, csum text, mod int);
create table meta (dirMod int);
insert into meta values (0);
create table meta (dirMod int, inSync int);
insert into meta values (0, 0);
create table log (id int, fname text, mod int);
create index ix_log_id on log (id);
""")
@ -209,8 +216,8 @@ create index ix_log_id on log (id);
changed = []
# loop through on-disk files
for f in os.listdir(self.dir()):
# ignore our db and folders
if f.startswith("media.db") or os.path.isdir(f):
# ignore folders
if os.path.isdir(f):
continue
# newly added?
if f not in self.cache:
@ -255,3 +262,5 @@ create index ix_log_id on log (id);
self.db.executemany("delete from media where fname = ?",
mediaRem)
self.db.execute("update meta set dirMod = ?", self._mtime(self.dir()))
self.db.commit()

View file

@ -14,8 +14,8 @@ def test_latex():
f = d.newFact()
f['Front'] = u"[latex]hello[/latex]"
d.addFact(f)
# but since latex couldn't run, it will only have the media.db
assert len(os.listdir(d.media.dir())) == 1
# but since latex couldn't run, there's nothing there
assert len(os.listdir(d.media.dir())) == 0
# check the error message
msg = f.cards()[0].q()
assert "executing latex" in msg