Exemplo n.º 1
0
    def create_dump_file(self, workingdirypath: PathLike,
                         filepath: PathLike) -> bool:
        if not isinstance(filepath, Path):
            return False
        ext = filepath.suffix
        if ext not in ['.sql', '.txt', '.dump', '.zip']:
            return False
        if not isinstance(workingdirypath, Path) or not workingdirypath.is_dir:
            return False

        try:
            tmpfilepath = workingdirypath.joinpath(filepath.stem + '.sql')
            cmd = 'mysqldump -h {} -P {} -u {} -p{} --skip-comments {} > {}'.format(
                self.dbparams.db_host, self.dbparams.db_port,
                self.dbparams.db_user, self.dbparams.db_passwd,
                self.dbparams.db_name, str(tmpfilepath.absolute()))
            subprocess.check_call(cmd, shell=True)
            if ext == '.zip':
                with ZipFile(filepath, 'w', ZIP_DEFLATED) as outfile:
                    outfile.write(tmpfilepath, tmpfilepath.name)
                tmpfilepath.unlink()
            else:
                shutil.move(str(tmpfilepath.absolute()),
                            str(filepath.absolute()))
        except Exception:
            return False
        return True
Exemplo n.º 2
0
def validate_paths(src: PathLike,
                   dst: Optional[PathLike] = None,
                   date_fmt: Optional[str] = None) -> tuple[Path, Path]:
    src = Path(src)
    dst = Path(dst) if dst else src.parent
    timestamp = datetime.now().strftime(date_fmt) if date_fmt else ''
    if not src.is_file():
        raise FileNotFoundError(f'Failed to locate specified file {src}')
    if dst.is_dir():
        dst = dst / (src.stem + timestamp)
    elif not dst.parent.is_dir():
        raise NotADirectoryError(
            f'Failed to find destination directory {dst.parent}')
    return src.absolute(), dst.absolute()
Exemplo n.º 3
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)