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)
16 self._conn = sqlite3.connect(db_file)
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(name, size, meta_time)
36 self._add_to_cache(name, size, meta_time, file_dir)
40 logging.info("Closing locator for %s...", self._base_dir)
43 def _update_cache_access_time(self, name, size, meta_time):
44 c = self._conn.cursor()
45 c.execute("UPDATE cache SET access_time=? WHERE name=? AND size=? AND meta_time=?",
46 (int(time.time()), name, size, meta_time))
49 def _remove_from_cache(self, name, size, meta_time):
50 c = self._conn.cursor()
51 c.execute("DELETE FROM cache WHERE name=? AND size=? AND meta_time=?",
52 (name, size, meta_time))
55 def _add_to_cache(self, name, size, meta_time, file_dir):
56 c = self._conn.cursor()
57 c.execute('''INSERT INTO cache (name, size, meta_time, file_dir, access_time)
58 VALUES (?, ?, ?, ?, ?)''',
59 (name, size, meta_time, file_dir, int(time.time())))
62 def _clean_cache(self):
63 c = self._conn.cursor()
64 c.execute("DELETE FROM cache WHERE access_time<?", (int(time.time()) - 604800))
68 logging.info("Creating locator database for %s...", self._base_dir)
69 c = self._conn.cursor()
70 c.execute('''CREATE TABLE cache(
71 name TEXT, size INTEGER, meta_time INTEGER, file_dir TEXT,
73 PRIMARY KEY (name, size, meta_time))''')