Ejemplo n.º 1
0
 def is_rar_supported():
     """Check rar unpacking support."""
     try:
         rarfile.custom_check([rarfile.UNRAR_TOOL], True)
     except rarfile.RarExecError:
         logger.log('UNRAR tool not available.', logger.WARNING)
         return False
     except Exception as msg:
         logger.log('Rar Not Supported: {error}'.format(error=ex(msg)),
                    logger.ERROR)
         return False
     return True
Ejemplo n.º 2
0
    def init_libraries(self):
        try_executables = []
        custom_unrar = os.environ.get("SZ_UNRAR_TOOL")
        if custom_unrar:
            if os.path.isfile(custom_unrar):
                try_executables.append(custom_unrar)

        unrar_exe = None
        if Core.runtime.os == "Windows":
            unrar_exe = os.path.abspath(os.path.join(self.libraries_root, "Windows", "i386", "UnRAR", "UnRAR.exe"))

        elif Core.runtime.os == "MacOSX":
            unrar_exe = os.path.abspath(os.path.join(self.libraries_root, "MacOSX", "i386", "UnRAR", "unrar"))

        elif Core.runtime.os == "Linux":
            unrar_exe = os.path.abspath(os.path.join(self.libraries_root, "Linux", Core.runtime.cpu, "UnRAR", "unrar"))

        if unrar_exe and os.path.isfile(unrar_exe):
            try_executables.append(unrar_exe)

        try_executables.append("unrar")

        for exe in try_executables:
            rarfile.UNRAR_TOOL = exe
            rarfile.ORIG_UNRAR_TOOL = exe
            if os.path.isfile(exe) and not os.access(exe, os.X_OK):
                st = os.stat(exe)
                try:
                    Log.Debug("setting generic executable permissions for %s", exe)
                    # fixme: too broad?
                    os.chmod(exe, st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
                except:
                    Log.Debug("failed setting generic executable permissions for %s: %s", exe, traceback.format_exc())
                    try:
                        Log.Debug("setting executable permissions for %s", exe)
                        os.chmod(exe, st.st_mode | stat.S_IEXEC)
                    except:
                        Log.Debug("failed setting executable permissions for %s: %s", exe, traceback.format_exc())
            try:
                rarfile.custom_check([rarfile.UNRAR_TOOL], True)
            except:
                Log.Debug("custom check failed for: %s", exe)
                continue

            rarfile.OPEN_ARGS = rarfile.ORIG_OPEN_ARGS
            rarfile.EXTRACT_ARGS = rarfile.ORIG_EXTRACT_ARGS
            rarfile.TEST_ARGS = rarfile.ORIG_TEST_ARGS
            Log.Info("Using UnRAR from: %s", exe)
            self.unrar = exe
            return

        Log.Warn("UnRAR not found")
Ejemplo n.º 3
0
    def __init__(self, username=None, password=None):

        # Provider needs UNRAR installed. If not available raise ConfigurationError
        try:
            rarfile.custom_check([rarfile.UNRAR_TOOL], True)
        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
Ejemplo n.º 4
0
    def __init__(self, username=None, password=None):

        # Provider needs UNRAR installed. If not available raise ConfigurationError
        try:
            rarfile.custom_check([rarfile.UNRAR_TOOL], True)
        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
Ejemplo n.º 5
0
def init_binaries():
    from utils import get_binary
    exe = get_binary("unrar")

    rarfile.UNRAR_TOOL = exe
    rarfile.ORIG_UNRAR_TOOL = exe
    try:
        rarfile.custom_check([rarfile.UNRAR_TOOL], True)
    except Exception:
        logging.debug("custom check failed for: %s", exe)

    logging.debug("Using UnRAR from: %s", exe)
    unrar = exe

    return unrar
Ejemplo n.º 6
0
def init_binaries():
    exe = get_binary("unrar")

    rarfile.UNRAR_TOOL = exe
    rarfile.ORIG_UNRAR_TOOL = exe
    try:
        rarfile.custom_check([rarfile.UNRAR_TOOL], True)
    except:
        logging.debug("custom check failed for: %s", exe)

    rarfile.OPEN_ARGS = rarfile.ORIG_OPEN_ARGS
    rarfile.EXTRACT_ARGS = rarfile.ORIG_EXTRACT_ARGS
    rarfile.TEST_ARGS = rarfile.ORIG_TEST_ARGS
    logging.info("Using UnRAR from: %s", exe)
    unrar = exe

    return unrar
Ejemplo n.º 7
0
    def init_libraries(self):
        try_executables = []
        custom_unrar = os.environ.get("SZ_UNRAR_TOOL")
        if custom_unrar:
            if os.path.isfile(custom_unrar):
                try_executables.append(custom_unrar)

        unrar_exe = None
        if Core.runtime.os == "Windows":
            unrar_exe = os.path.abspath(
                os.path.join(self.libraries_root, "Windows", "i386", "UnRAR",
                             "UnRAR.exe"))

        elif Core.runtime.os == "MacOSX":
            unrar_exe = os.path.abspath(
                os.path.join(self.libraries_root, "MacOSX", "i386", "UnRAR",
                             "unrar"))

        elif Core.runtime.os == "Linux":
            unrar_exe = os.path.abspath(
                os.path.join(self.libraries_root, "Linux", Core.runtime.cpu,
                             "UnRAR", "unrar"))

        if unrar_exe and os.path.isfile(unrar_exe):
            try_executables.append(unrar_exe)

        try_executables.append("unrar")

        for exe in try_executables:
            rarfile.UNRAR_TOOL = exe
            rarfile.ORIG_UNRAR_TOOL = exe
            try:
                rarfile.custom_check([rarfile.UNRAR_TOOL], True)
            except:
                Log.Debug("custom check failed for: %s", exe)
                continue

            rarfile.OPEN_ARGS = rarfile.ORIG_OPEN_ARGS
            rarfile.EXTRACT_ARGS = rarfile.ORIG_EXTRACT_ARGS
            rarfile.TEST_ARGS = rarfile.ORIG_TEST_ARGS
            Log.Info("Using UnRAR from: %s", exe)
            self.unrar = exe
            return

        Log.Warn("UnRAR not found")
Ejemplo n.º 8
0
def init_binaries():
    binaries_dir = os.path.realpath(
        os.path.join(os.path.dirname(__file__), '..', 'bin'))

    unrar_exe = None
    exe = None
    installed_unrar = which('unrar')

    if installed_unrar and os.path.isfile(installed_unrar):
        unrar_exe = installed_unrar
    else:
        if platform.system() == "Windows":  # Windows
            unrar_exe = os.path.abspath(
                os.path.join(binaries_dir, "Windows", "i386", "UnRAR",
                             "UnRAR.exe"))

        elif platform.system() == "Darwin":  # MacOSX
            unrar_exe = os.path.abspath(
                os.path.join(binaries_dir, "MacOSX", "i386", "UnRAR", "unrar"))

        elif platform.system() == "Linux":  # Linux
            unrar_exe = os.path.abspath(
                os.path.join(binaries_dir, "Linux", platform.machine(),
                             "UnRAR", "unrar"))

    if unrar_exe and os.path.isfile(unrar_exe):
        exe = unrar_exe

    rarfile.UNRAR_TOOL = exe
    rarfile.ORIG_UNRAR_TOOL = exe
    try:
        rarfile.custom_check([rarfile.UNRAR_TOOL], True)
    except:
        logging.debug("custom check failed for: %s", exe)

    rarfile.OPEN_ARGS = rarfile.ORIG_OPEN_ARGS
    rarfile.EXTRACT_ARGS = rarfile.ORIG_EXTRACT_ARGS
    rarfile.TEST_ARGS = rarfile.ORIG_TEST_ARGS
    logging.info("Using UnRAR from: %s", exe)
    unrar = exe

    return unrar
Ejemplo n.º 9
0
    def init_libraries(self):
        try_executables = []
        custom_unrar = os.environ.get("SZ_UNRAR_TOOL")
        if custom_unrar:
            if os.path.isfile(custom_unrar):
                try_executables.append(custom_unrar)

        unrar_exe = None
        if Core.runtime.os == "Windows":
            unrar_exe = os.path.abspath(os.path.join(self.libraries_root, "Windows", "i386", "UnRAR", "UnRAR.exe"))

        elif Core.runtime.os == "MacOSX":
            unrar_exe = os.path.abspath(os.path.join(self.libraries_root, "MacOSX", "i386", "UnRAR", "unrar"))

        elif Core.runtime.os == "Linux":
            unrar_exe = os.path.abspath(os.path.join(self.libraries_root, "Linux", Core.runtime.cpu, "UnRAR", "unrar"))

        if unrar_exe and os.path.isfile(unrar_exe):
            try_executables.append(unrar_exe)

        try_executables.append("unrar")

        for exe in try_executables:
            rarfile.UNRAR_TOOL = exe
            rarfile.ORIG_UNRAR_TOOL = exe
            try:
                rarfile.custom_check([rarfile.UNRAR_TOOL], True)
            except:
                Log.Debug("custom check failed for: %s", exe)
                continue

            rarfile.OPEN_ARGS = rarfile.ORIG_OPEN_ARGS
            rarfile.EXTRACT_ARGS = rarfile.ORIG_EXTRACT_ARGS
            rarfile.TEST_ARGS = rarfile.ORIG_TEST_ARGS
            Log.Info("Using UnRAR from: %s", exe)
            self.unrar = exe
            return

        Log.Warn("UnRAR not found")
Ejemplo n.º 10
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
Ejemplo n.º 11
0
def change_unrar_tool(unrar_tool, alt_unrar_tool):
    unrar_tool = unrar_tool or sickbeard.UNRAR_TOOL or rarfile.UNRAR_TOOL or rarfile.ORIG_UNRAR_TOOL
    alt_unrar_tool = alt_unrar_tool or sickbeard.ALT_TOOL or rarfile.ALT_TOOL

    try:
        rarfile.custom_check(unrar_tool)
        sickbeard.UNRAR_TOOL = unrar_tool
        rarfile.UNRAR_TOOL = unrar_tool
        rarfile.ORIG_UNRAR_TOOL = unrar_tool
    except (rarfile.RarCannotExec, rarfile.RarExecError, OSError, IOError):
        if platform.system() == 'Windows':
            for unrar_tool in ('C:\\Program Files\WinRAR\\UnRAR.exe',
                               'C:\\Program Files (x86)\WinRAR\\UnRAR.exe'):
                try:
                    rarfile.custom_check(unrar_tool)
                    sickbeard.UNRAR_TOOL = unrar_tool
                    rarfile.UNRAR_TOOL = unrar_tool
                    rarfile.ORIG_UNRAR_TOOL = unrar_tool
                except (rarfile.RarCannotExec, rarfile.RarExecError, OSError,
                        IOError):
                    pass
                    # Either host an extracted unrar on sickrage.github.io, or add it to the source. unrarw32.exe is an installer
                    # logger.log('Unrar not found at the path specified. Trying to download unrar and set the path')
                    # unrar_tool = "unrar.exe"
                    # if helpers.download_file("http://www.rarlab.com/rar/unrarw32.exe", session=helpers.make_session(), filename=unrar_tool):
                    #     try:
                    #         rarfile.custom_check(unrar_tool)
                    #         sickbeard.UNRAR_TOOL = unrar_tool
                    #         rarfile.UNRAR_TOOL = unrar_tool
                    #         rarfile.ORIG_UNRAR_TOOL = unrar_tool
                    #     except (rarfile.RarCannotExec, rarfile.RarExecError, OSError, IOError):
                    #         logger.log('Sorry, unrar was not set up correctly. Try installing WinRAR and make sure it is on the system PATH')
                    #         pass

    try:
        rarfile.custom_check(alt_unrar_tool)
        sickbeard.ALT_UNRAR_TOOL = alt_unrar_tool
        rarfile.ALT_TOOL = alt_unrar_tool
    except (rarfile.RarCannotExec, rarfile.RarExecError, OSError, IOError):
        pass

    # noinspection PyProtectedMember
    try:
        test = rarfile._check_unrar_tool()
    except (rarfile.RarCannotExec, rarfile.RarExecError, OSError, IOError):
        test = False

    if sickbeard.UNPACK and not test:
        logger.log('Disabling UNPACK setting because no unrar is installed.')
        sickbeard.UNPACK = 0

    return test
Ejemplo n.º 12
0
def change_unrar_tool(unrar_tool, alt_unrar_tool):

    # Check for failed unrar attempt, and remove it
    # Must be done before unrar is ever called or the self-extractor opens and locks startup
    bad_unrar = os.path.join(sickbeard.DATA_DIR, 'unrar.exe')
    if os.path.exists(bad_unrar) and os.path.getsize(bad_unrar) == 447440:
        try:
            os.remove(bad_unrar)
        except OSError as e:
            logger.log("Unable to delete bad unrar.exe file {0}: {1}. You should delete it manually".format(bad_unrar, e.strerror), logger.WARNING)

    try:
        rarfile.custom_check(unrar_tool)
    except (rarfile.RarCannotExec, rarfile.RarExecError, OSError, IOError):
        # Let's just return right now if the defaults work
        try:
            # noinspection PyProtectedMember
            test = rarfile._check_unrar_tool()
            if test:
                # These must always be set to something before returning
                sickbeard.UNRAR_TOOL = rarfile.UNRAR_TOOL
                sickbeard.ALT_UNRAR_TOOL = rarfile.ALT_TOOL
                return True
        except (rarfile.RarCannotExec, rarfile.RarExecError, OSError, IOError):
            pass

        if platform.system() == 'Windows':
            # Look for WinRAR installations
            found = False
            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(sickbeard.PROG_DIR, 'unrar\\unrar.exe'))

            for check in check_locations:
                if ek(os.path.isfile, check):
                    # Can use it?
                    try:
                        rarfile.custom_check(check)
                        unrar_tool = check
                        found = True
                        break
                    except (rarfile.RarCannotExec, rarfile.RarExecError, OSError, IOError):
                        found = False

            # Download
            if not found:
                logger.log('Trying to download unrar.exe and set the path')
                unrar_store = ek(os.path.join, sickbeard.PROG_DIR, 'unrar')  # ./unrar (folder)
                unrar_zip = ek(os.path.join, sickbeard.PROG_DIR, 'unrar_win.zip')  # file download

                if (helpers.download_file(
                    "http://sickrage.github.io/unrar/unrar_win.zip", filename=unrar_zip, session=helpers.make_session()
                ) and helpers.extractZip(archive=unrar_zip, targetDir=unrar_store)):
                    try:
                        ek(os.remove, unrar_zip)
                    except OSError as e:
                        logger.log("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.custom_check(check)
                        unrar_tool = check
                        logger.log('Successfully downloaded unrar.exe and set as unrar tool', )
                    except (rarfile.RarCannotExec, rarfile.RarExecError, OSError, IOError):
                        logger.log('Sorry, unrar was not set up correctly. Try installing WinRAR and make sure it is on the system PATH')
                else:
                    logger.log('Unable to download unrar.exe')

    # These must always be set to something before returning
    sickbeard.UNRAR_TOOL = rarfile.UNRAR_TOOL = rarfile.ORIG_UNRAR_TOOL = unrar_tool
    sickbeard.ALT_UNRAR_TOOL = rarfile.ALT_TOOL = alt_unrar_tool

    try:
        # noinspection PyProtectedMember
        test = rarfile._check_unrar_tool()
    except (rarfile.RarCannotExec, rarfile.RarExecError, OSError, IOError):
        if sickbeard.UNPACK == 1:
            logger.log('Disabling UNPACK setting because no unrar is installed.')
            sickbeard.UNPACK = 0
        test = False

    return test
Ejemplo n.º 13
0
def change_unrar_tool(unrar_tool):
    # Check for failed unrar attempt, and remove it
    # Must be done before unrar is ever called or the self-extractor opens and locks startup
    bad_unrar = os.path.join(sickrage.app.data_dir, 'unrar.exe')
    if os.path.exists(bad_unrar) and os.path.getsize(bad_unrar) == 447440:
        try:
            os.remove(bad_unrar)
        except OSError as e:
            sickrage.app.log.warning(
                "Unable to delete bad unrar.exe file {}: {}. You should delete it manually"
                .format(bad_unrar, e.strerror))

    for check in [unrar_tool, 'unrar']:
        try:
            rarfile.custom_check([check], True)
            sickrage.app.unrar_tool = rarfile.UNRAR_TOOL = check
            return True
        except (rarfile.RarCannotExec, rarfile.RarExecError, OSError, IOError):
            continue

    if sys.platform == 'win32':
        # 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(sickrage.PROG_DIR,
                                         'unrar\\unrar.exe'))

        for check in check_locations:
            if os.path.isfile(check):
                # Can use it?
                try:
                    rarfile.custom_check([check], True)
                    sickrage.app.unrar_tool = rarfile.UNRAR_TOOL = check
                    return True
                except (rarfile.RarCannotExec, rarfile.RarExecError, OSError,
                        IOError):
                    continue

        # Download
        sickrage.app.log.info('Trying to download unrar.exe and set the path')
        unrar_zip = os.path.join(sickrage.app.data_dir, 'unrar_win.zip')

        if WebSession().download("https://sickrage.ca/downloads/unrar_win.zip",
                                 filename=unrar_zip) and extract_zipfile(
                                     archive=unrar_zip,
                                     targetDir=sickrage.app.data_dir):
            try:
                os.remove(unrar_zip)
            except OSError as e:
                sickrage.app.log.info(
                    "Unable to delete downloaded file {}: {}. You may delete it manually"
                    .format(unrar_zip, e.strerror))

            check = os.path.join(sickrage.app.data_dir, "unrar.exe")

            try:
                rarfile.custom_check([check], True)
                sickrage.app.unrar_tool = rarfile.UNRAR_TOOL = check
                sickrage.app.log.info(
                    'Successfully downloaded unrar.exe and set as unrar tool')
                return True
            except (rarfile.RarCannotExec, rarfile.RarExecError, OSError,
                    IOError):
                sickrage.app.log.info(
                    'Sorry, unrar was not set up correctly. Try installing WinRAR and '
                    'make sure it is on the system PATH')
        else:
            sickrage.app.log.info('Unable to download unrar.exe')

    if sickrage.app.config.general.unpack:
        sickrage.app.log.info(
            'Disabling UNPACK setting because no unrar is installed.')
        sickrage.app.config.general.unpack = False