Skip to content

Commit

Permalink
Revert "Cache path to cache directory"
Browse files Browse the repository at this point in the history
This reverts commit 70645cd.
  • Loading branch information
karkhaz committed Mar 12, 2022
1 parent 18f22c4 commit 1e42357
Showing 1 changed file with 36 additions and 44 deletions.
80 changes: 36 additions & 44 deletions lib/litani.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import asyncio
import contextlib
import dataclasses
import json
import logging
import os
Expand Down Expand Up @@ -68,48 +67,6 @@ class TimeoutExpired(Exception):
pass


@dataclasses.dataclass
class CacheDir:
cache_dir_path: pathlib.Path = None

@staticmethod
def get(lookup_path=os.getcwd()):
if CacheDir.cache_dir_path:
return CacheDir.cache_dir_path
def cache_pointer_dirs():
current = pathlib.Path(lookup_path).resolve(strict=True)
yield current
while current.parent != current:
current = current.parent
yield current

for possible_dir in cache_pointer_dirs():
logging.debug(
"Searching for cache pointer in %s", possible_dir)
possible_pointer = possible_dir / CACHE_POINTER
try:
if possible_pointer.exists():
logging.debug(
"Found a cache pointer at %s", possible_pointer)
with open(possible_pointer) as handle:
pointer = handle.read().strip()
possible_cache = pathlib.Path(pointer)
if possible_cache.exists():
logging.debug("cache is at %s", possible_cache)
CacheDir.cache_dir_path = possible_cache
return possible_cache
logging.warning(
"Found a cache file at %s pointing to %s, but that "
"directory does not exist. Continuing search...",
possible_pointer, possible_cache)
except PermissionError:
pass

logging.error(
"Could not find a pointer to a litani cache. Did you forget "
"to run `litani init`?")
raise FileNotFoundError


class LockableDirectory:
"""POSIX-compliant directory locking"""
Expand Down Expand Up @@ -190,9 +147,44 @@ async def try_acquire_wait(self, timeout=0):



def _get_cache_dir(path=os.getcwd()):
def cache_pointer_dirs():
current = pathlib.Path(path).resolve(strict=True)
yield current
while current.parent != current:
current = current.parent
yield current

for possible_dir in cache_pointer_dirs():
logging.debug(
"Searching for cache pointer in %s", possible_dir)
possible_pointer = possible_dir / CACHE_POINTER
try:
if possible_pointer.exists():
logging.debug(
"Found a cache pointer at %s", possible_pointer)
with open(possible_pointer) as handle:
pointer = handle.read().strip()
possible_cache = pathlib.Path(pointer)
if possible_cache.exists():
logging.debug("cache is at %s", possible_cache)
return possible_cache
logging.warning(
"Found a cache file at %s pointing to %s, but that "
"directory does not exist. Continuing search...",
possible_pointer, possible_cache)
except PermissionError:
pass

logging.error(
"Could not find a pointer to a litani cache. Did you forget "
"to run `litani init`?")
raise FileNotFoundError


def get_cache_dir(path=os.getcwd()):
try:
return CacheDir.get(path)
return _get_cache_dir(path)
except FileNotFoundError:
sys.exit(1)

Expand Down

0 comments on commit 1e42357

Please sign in to comment.