Exemple #1
0
    def _verify_dependencies():
        try:
            import rarfile
        except ImportError as error:
            raise ModuleNotFoundError from error

        try:
            # For me bsdtar was not working
            rarfile.tool_setup(bsdtar=False)
        except rarfile.RarCannotExec as error:
            raise RuntimeError from error

        return rarfile
Exemple #2
0
    def __init__(self, username=None, password=None, featured_only=False):

        # Provider needs UNRAR installed. If not available raise ConfigurationError
        try:
            rarfile.tool_setup()
        except rarfile.RarExecError:
            raise ConfigurationError('UNRAR tool not available')

        if any((username, password)) and not all((username, password)):
            raise ConfigurationError('Username and password must be specified')

        self.username = username
        self.password = password
        self.logged_in = False
        self.session = None
        self.featured_only = featured_only
Exemple #3
0
    def is_supported(self):
        if rarfile.__version__ == '4.0':
            try:
                rarfile.tool_setup()
            except rarfile.RarCannotExec:
                return False
            return True

        #
        # handle old rarfile versions
        #
        available_tools = {
            rarfile.UNRAR_TOOL: [],
            rarfile.ALT_TOOL: rarfile.ALT_CHECK_ARGS,  # pylint: disable=E1101
        }

        for tool, check_args in available_tools.items():
            try:
                cmd = [tool] + list(check_args)
                rarfile.custom_check(cmd, True)  # pylint: disable=E1101
                return True
            except rarfile.RarCannotExec:
                pass
        return False
Exemple #4
0
def change_unrar_tool(unrar_tool, unar_tool, bsdtar_tool):
    try:
        rarfile.tool_setup()
    except rarfile.RarCannotExec:
        pass
    else:
        settings.UNAR_TOOL = rarfile.UNRAR_TOOL
        settings.UNAR_TOOL = rarfile.UNAR_TOOL
        settings.BSDTAR_TOOL = rarfile.BSDTAR_TOOL
        return True

    _unrar_tool = rarfile.UNRAR_TOOL
    _unar_tool = rarfile.UNAR_TOOL
    _bsdtar_tool = rarfile.BSDTAR_TOOL

    if unrar_tool and unrar_tool != rarfile.UNRAR_TOOL:
        rarfile.UNRAR_TOOL = unrar_tool

    if unar_tool and unar_tool != rarfile.UNAR_TOOL:
        rarfile.UNAR_TOOL = unar_tool

    if bsdtar_tool and bsdtar_tool != rarfile.BSDTAR_TOOL:
        rarfile.BSDTAR_TOOL = bsdtar_tool

    try:
        rarfile.tool_setup()
    except rarfile.RarCannotExec:
        pass
    else:
        settings.UNAR_TOOL = rarfile.UNRAR_TOOL
        settings.UNAR_TOOL = rarfile.UNAR_TOOL
        settings.BSDTAR_TOOL = rarfile.BSDTAR_TOOL
        return True

    rarfile.UNRAR_TOOL = _unar_tool
    rarfile.UNAR_TOOL = _unar_tool
    rarfile.BSDTAR_TOOL = _bsdtar_tool

    if platform.system() == "Windows":
        # Look for WinRAR installations
        winrar_path = "WinRAR\\UnRAR.exe"
        # Make a set of unique paths to check from existing environment variables
        check_locations = {
            os.path.join(location, winrar_path)
            for location in (
                os.environ.get("ProgramW6432"),
                os.environ.get("ProgramFiles(x86)"),
                os.environ.get("ProgramFiles"),
                re.sub(r"\s?\(x86\)", "", os.environ["ProgramFiles"]),
            ) if location
        }
        check_locations.add(os.path.join(settings.DATA_DIR,
                                         "unrar\\unrar.exe"))

        for check in check_locations:
            if os.path.isfile(check):
                try:
                    rarfile.UNRAR_TOOL = check
                    rarfile.tool_setup()
                    settings.UNRAR_TOOL = check
                    return True
                except rarfile.RarCannotExec:
                    pass

        logger.info("Trying to download unrar.exe and set the path")
        unrar_store = os.path.join(settings.DATA_DIR,
                                   "unrar")  # ./unrar (folder)
        unrar_zip = os.path.join(settings.DATA_DIR,
                                 "unrar_win.zip")  # file download

        if helpers.download_file(
                "https://sickchill.github.io/unrar/unrar_win.zip",
                filename=unrar_zip,
                session=helpers.make_session()) and helpers.extractZip(
                    archive=unrar_zip, targetDir=unrar_store):
            try:
                os.remove(unrar_zip)
            except OSError as e:
                logger.info(
                    "Unable to delete downloaded file {0}: {1}. You may delete it manually"
                    .format(unrar_zip, e.strerror))

            check = os.path.join(unrar_store, "unrar.exe")
            try:
                rarfile.UNRAR_TOOL = check
                rarfile.tool_setup()
                settings.UNRAR_TOOL = check
                logger.info(
                    "Successfully downloaded unrar.exe and set as unrar tool")
                return True
            except (rarfile.RarCannotExec, rarfile.RarExecError, OSError,
                    IOError):
                logger.info(
                    "Sorry, unrar was not set up correctly. Try installing WinRAR and make sure it is on the system PATH"
                )
        else:
            logger.info("Unable to download unrar.exe")

    # These must always be set to something before returning
    settings.UNRAR_TOOL = rarfile.UNRAR_TOOL = _unrar_tool
    settings.UNAR_TOOL = rarfile.UNAR_TOOL = _unar_tool
    settings.BSDTAR_TOOL = rarfile.BSDTAR_TOOL = _bsdtar_tool

    try:
        rarfile.tool_setup()
        return True
    except rarfile.RarCannotExec:
        if settings.UNPACK == settings.UNPACK_PROCESS_CONTENTS:
            logger.info(
                _("Disabling UNPACK setting because no unrar/unar/bsdtar is found and accessible in the path or setting."
                  ))
            settings.UNPACK = settings.UNPACK_DISABLED
        return False
Exemple #5
0
def install_unar_tool():
    rarfile.tool_setup(unrar=False, unar=True, bsdtar=False, force=True)
Exemple #6
0
def uninstall_alt_tool():
    rarfile.tool_setup(force=True)