withself.file_db.lock() as trx: # The repo is already cloned and in the cache if key in trx["lrucache"]: trx["lrucache"][key] += 1 return Repo(repo_path)
# The url is bad if key in trx["badcache"]: returnNone
iflen(trx["lrucache"]) >= self.max_size: # remove the least used item least_used = min(trx["lrucache"].items(), key=lambda x: x[1]) least_key = least_used[0] least_path = os.path.join(self.repo_base, least_key) least_lock = os.path.join(self.repo_base, least_key + ".lock") if os.path.exists(least_path): shutil.rmtree(least_path) if os.path.exists(least_lock): os.unlink(least_lock) del trx["lrucache"][least_key]
# acquire a file lock of the repo # if the lock is acquired, we are the only process/thread that is cloning the repo # otherwise, we wait for the other process to finish cloning with FileLock(repo_lock): # The repo has been cloned by another process # We can just open it if os.path.exists(repo_path): inst = Repo(repo_path) return inst
withself.file_db.lock() as trx: # The url is bad if key in trx["badcache"]: returnNone
# Clone the repo try: inst = Repo.clone_from(git_url, repo_path) except Exception as e: # Clone failed, bad git url logger.error(e) withself.file_db.lock() as trx: trx["badcache"].add(key) returnNone
# Clone succeeded withself.file_db.lock() as trx: if key notin trx["lrucache"]: trx["lrucache"][key] = 1 else: trx["lrucache"][key] += 1 return Repo(repo_path)