9 def __init__(self, base_dir, exclude_dir, db_file):
10 self._base_dir = base_dir
11 self._exclude_dir = exclude_dir
12 if not os.path.isfile(db_file):
13 self._conn = sqlite3.connect(db_file, check_same_thread=False)
16 self._conn = sqlite3.connect(db_file, check_same_thread=False)
17 logging.info("Opened locator for %s.", self._base_dir)
19 def locate_file(self, name, size, meta_time):
20 c = self._conn.cursor()
21 c.execute("SELECT file_dir FROM cache WHERE name=? AND size=? AND meta_time=?",
22 (name, size, meta_time))
25 if os.path.isfile(cached[0]) and \
26 os.path.getsize(cached[0]) == size and \
27 misc.extract_meta_time(cached[0]) == meta_time:
28 self._update_cache_access_time(name, size, meta_time)
31 self._remove_from_cache(name, size, meta_time)
33 file_dir = misc.find_file(self._base_dir, name, size, meta_time,
37 self._add_to_cache(name, size, meta_time, file_dir)
41 logging.info("Closing locator for %s...", self._base_dir)
44 def _update_cache_access_time(self, name, size, meta_time):
45 c = self._conn.cursor()
46 c.execute("UPDATE cache SET access_time=? WHERE name=? AND size=? AND meta_time=?",
47 (int(time.time()), name, size, meta_time))
50 def _remove_from_cache(self, name, size, meta_time):
51 c = self._conn.cursor()
52 c.execute("DELETE FROM cache WHERE name=? AND size=? AND meta_time=?",
53 (name, size, meta_time))
56 def _add_to_cache(self, name, size, meta_time, file_dir):
57 c = self._conn.cursor()
58 c.execute('''INSERT INTO cache (name, size, meta_time, file_dir, access_time)
59 VALUES (?, ?, ?, ?, ?)''',
60 (name, size, meta_time, file_dir, int(time.time())))
63 def _clean_cache(self):
64 c = self._conn.cursor()
65 c.execute("DELETE FROM cache WHERE access_time<?", (int(time.time()) - 604800))
69 logging.info("Creating locator database for %s...", self._base_dir)
70 c = self._conn.cursor()
71 c.execute('''CREATE TABLE cache(
72 name TEXT, size INTEGER, meta_time INTEGER, file_dir TEXT,
74 PRIMARY KEY (name, size, meta_time))''')