self._base_dir = base_dir
self._exclude_dir = exclude_dir
if not os.path.isfile(db_file):
- self._conn = sqlite3.connect(db_file)
+ self._conn = sqlite3.connect(db_file, check_same_thread=False)
self._create_db()
else:
- self._conn = sqlite3.connect(db_file)
+ self._conn = sqlite3.connect(db_file, check_same_thread=False)
logging.info("Opened locator for %s.", self._base_dir)
def locate_file(self, name, size, meta_time):
c = self._conn.cursor()
c.execute("SELECT file_dir FROM cache WHERE name=? AND size=? AND meta_time=?",
(name, size, meta_time))
+
cached = c.fetchone()
if cached:
+ # if os.path.isfile(cached[0]) and \
+ # os.path.getsize(cached[0]) == size and \
+ # misc.extract_meta_time(cached[0]) == meta_time:
if os.path.isfile(cached[0]) and \
- os.path.getsize(cached[0]) == size and \
- misc.extract_meta_time(cached[0]) == meta_time:
+ os.path.getsize(cached[0]) == size:
self._update_cache_access_time(name, size, meta_time)
return cached[0]
else:
self._remove_from_cache(name, size, meta_time)
- else:
- file_dir = misc.find_file(name, size, meta_time)
- if file_dir:
- self._clean_cache()
- self._add_to_cache(name, size, meta_time, file_dir)
- return file_dir
+
+ file_dir = misc.find_file(self._base_dir, name, size, meta_time,
+ self._exclude_dir)
+ if file_dir:
+ self._clean_cache()
+ self._add_to_cache(name, size, meta_time, file_dir)
+ return file_dir
def close(self):
logging.info("Closing locator for %s...", self._base_dir)
def _clean_cache(self):
c = self._conn.cursor()
- c.execute("DELETE FROM cache WHERE access_time<?", (int(time.time()) - 604800))
+ c.execute("DELETE FROM cache WHERE access_time<?", (int(time.time()) - 604800,))
self._conn.commit()
def _create_db(self):
logging.info("Creating locator database for %s...", self._base_dir)
c = self._conn.cursor()
- c.execute('''CREATE TABLE cache
- (name TEXT, size INTEGER, meta_time INTEGER, file_dir TEXT,
- access_time INTEGER)
- PRIMARY KEY (name, size, meta_time)''')
+ c.execute('''CREATE TABLE cache(
+ name TEXT, size INTEGER, meta_time INTEGER, file_dir TEXT,
+ access_time INTEGER,
+ PRIMARY KEY (name, size, meta_time))''')
self._conn.commit()