示例#1
0
文件: cache.py 项目: scidd/scidd-core
    def __init__(self,
                 path: os.PathLike = pathlib.Path.home() / ".scidd_cache",
                 name: str = "_SciDD_API_Cache.sqlite"):

        self._dbFilepath = path / name  # the full path + filename for the cache

        # create database path if needed
        if not path.exists():
            try:
                os.makedirs(path)
            except FileExistsError as e:
                logger.debug(
                    f"Path '{path}' appears not to exist, but 'os.makedirs(path)' is raising FileExistsError: {e}"
                )
            except OSError as e:
                raise OSError(
                    f"Unable to create specified path '{path}'; error: {e} ")

        if path.is_symlink():  # or os.path.islink(fp)
            if not os.path.exists(os.readlink(path)):
                # broken link
                raise Exception(
                    f"The path where the SciDD cache is expected ('{path}') is symlink pointing to a target that is no longer there. "
                    + "Either remove the symlink or fix the destination.")

        self._initialize_database()
示例#2
0
def create_symlink(source_filename: PathLike,
                   symlink_filename: PathLike,
                   relative: bool = False):
    """
    :param source_filename: path to point to
    :param symlink_filename: path at which to create symlink
    :param relative: make symlink relative to source location
    """

    if not isinstance(source_filename, Path):
        source_filename = Path(source_filename)
    if not isinstance(symlink_filename, Path):
        symlink_filename = Path(symlink_filename)

    if symlink_filename.is_symlink():
        logging.debug(f'removing symlink "{symlink_filename}"')
        os.remove(symlink_filename)
    symlink_filename = symlink_filename.parent.absolute().resolve(
    ) / symlink_filename.name

    starting_directory = None
    if relative:
        starting_directory = Path().cwd().resolve()
        os.chdir(symlink_filename.parent)
        if source_filename.is_absolute():
            try:
                source_filename = source_filename.relative_to(
                    symlink_filename.parent)
            except ValueError as error:
                warnings.warn(error)
                os.chdir(starting_directory)
    else:
        source_filename = source_filename.absolute()

    try:
        symlink_filename.symlink_to(source_filename)
    except Exception as error:
        warnings.warn(f'could not create symbolic link: {error}')
        shutil.copyfile(source_filename, symlink_filename)

    if starting_directory is not None:
        os.chdir(starting_directory)