def test_data():
    # Load input data
    input_torrents = []
    with open_(os.path.join(os.path.realpath(os.path.dirname(__file__)),
                            'data.json'),
               encoding='utf-8') as f:
        data = json.load(f)
    for torrent in data:
        torrent_obj = Torrent()
        torrent_obj.hash = torrent['hash']
        torrent_obj.name = torrent['name']
        torrent_obj.category = [torrent['category']
                                ] if len(torrent['category']) > 0 else []
        torrent_obj.tracker = torrent['tracker']
        torrent_obj.status = TorrentStatus[torrent['state']]
        torrent_obj.stalled = torrent['is_stalled']
        torrent_obj.size = torrent['size']
        torrent_obj.ratio = torrent['ratio']
        torrent_obj.uploaded = torrent['uploaded']
        torrent_obj.create_time = torrent['added_on']
        torrent_obj.seeding_time = torrent['seeding_time']
        torrent_obj.upload_speed = torrent['upspeed']
        torrent_obj.average_upload_speed = torrent['up_speed_avg']
        torrent_obj.downloaded = torrent['downloaded']
        torrent_obj.download_speed = torrent['dlspeed']
        torrent_obj.average_download_speed = torrent['dl_speed_avg']
        torrent_obj.last_activity = torrent['last_activity']
        torrent_obj.seeder = torrent['num_complete']
        torrent_obj.connected_seeder = torrent['num_seeds']
        torrent_obj.leecher = torrent['num_incomplete']
        torrent_obj.connected_leecher = torrent['num_leechs']
        torrent_obj.progress = torrent['progress']
        input_torrents.append(torrent_obj)

    return input_torrents
Beispiel #2
0
def test_task(qbittorrent_mocker):
    # Init loggger
    logger.Logger.init()
    lg = logger.Logger.register(__name__)

    # Set root directory
    root_dir = os.path.join(os.path.realpath(os.path.dirname(__file__)))
    lg.info('Root directory: %s', root_dir)

    qbittorrent_mocker()

    # Load files in directory
    for file in os.listdir(os.path.join(root_dir, 'cases')):
        file_path = os.path.join(root_dir, 'cases', file)

        if os.path.isfile(file_path):
            lg.info('Loading file: %s', file)
            with open_(file_path, 'r', encoding='utf-8') as f:
                conf = yaml.safe_load(f)

            # Run task
            instance = Task(file, conf['task'], False)
            instance.execute()
            assert len(instance.get_removed_torrents()
                       ) == conf['result']['num-of-removed']
Beispiel #3
0
def test_strategies(mocker, test_data, test_env):
    # Logger
    lg = logger.Logger.register(__name__)

    # Exceptions mapping
    exception_map = {
        IllegalCharacter: 'IllegalCharacter',
        ConditionSyntaxError: 'ConditionSyntaxError',
        NoSuchCondition: 'NoSuchCondition'
    }

    # Check each case
    base_dir = os.path.join(os.path.realpath(os.path.dirname(__file__)),
                            'cases')
    lg.info('Base directory: %s' % base_dir)

    for item in os.listdir(base_dir):
        if os.path.isdir(os.path.join(base_dir, item)):
            # Enter a directory
            lg.info("Entering directory '%s'..." % item)

            for conf_file in os.listdir(os.path.join(base_dir, item)):
                conf_path = os.path.join(base_dir, item, conf_file)
                if os.path.isfile(conf_path):
                    # Load file
                    lg.info('Loading file: %s' % conf_file)
                    with open_(conf_path, encoding='utf-8') as f:
                        conf = yaml.safe_load(f)

                    try:
                        # Make strategy and run
                        mocker.patch('time.time',
                                     return_value=test_env['time.time'])
                        mocker.patch(
                            'psutil.disk_usage',
                            return_value=test_env['psutil.disk_usage'])
                        stgy = Strategy(conf_file, conf['test'])
                        stgy.execute(test_data)

                        # Check result
                        if 'remain' in conf:
                            assert set([
                                x.name for x in stgy.remain_list
                            ]) == set(conf['remain']
                                      if conf['remain'] is not None else [])
                        if 'remove' in conf:
                            assert set([
                                x.name for x in stgy.remove_list
                            ]) == set(conf['remove']
                                      if conf['remove'] is not None else [])
                    except Exception as e:
                        if 'exceptions' in conf and exception_map[
                                sys.exc_info()[0]] in conf['exceptions']:
                            pass
                        else:
                            raise e

            # Leave the directory
            lg.info("Leaving directory '%s'..." % item)
    def runner():
        # Set root directory
        root_dir = os.path.join(os.path.realpath(os.path.dirname(__file__)))

        # Mock qBittorrent API version
        # Force caller to use API v1.0
        requests_mock.get('mock://qbittorrent/api/v2/app/webapiVersion',
                          status_code=404)
        requests_mock.get('mock://qbittorrent/version/api', text='10')

        # Mock qBittorrent version
        requests_mock.get('mock://qbittorrent/version/qbittorrent',
                          text='Fake qBittorrent')
        #lg.info('qBittorrent version was mocked.')

        # Mock qBittorrent logging in interface
        requests_mock.post('mock://qbittorrent/login', text='Ok.')
        #lg.info('Logging in was mocked.')

        # Mock qBittorrent torrents list
        with open_(os.path.join(root_dir, 'qbittorrent-snapshots',
                                '4-1-5.json'),
                   'r',
                   encoding='utf-8') as f:
            torrent_list = json.load(f)
            requests_mock.get('mock://qbittorrent/query/torrents',
                              json=torrent_list)
            #lg.info('Torrents list was mocked.')

        # Mock qBittorent torrent details
        for torrent in torrent_list:
            with open_(
                    os.path.join(root_dir, 'qbittorrent-snapshots', 'torrents',
                                 '%s.json' % torrent['hash'])) as fp:
                metadata = json.load(fp)
                requests_mock.get(
                    'mock://qbittorrent/query/propertiesGeneral/%s' %
                    torrent['hash'],
                    json=metadata['properties'])
                requests_mock.get(
                    'mock://qbittorrent/query/propertiesTrackers/%s' %
                    torrent['hash'],
                    json=metadata['trackers'])
Beispiel #5
0
def pre_processor(argv):
    # View Mode
    view_mode = False
    # The path of the configuration file
    conf_path = 'config.yml'
    # Task
    task = None

    # Set default logging path to current working directory
    logger.Logger.log_path = ''

    # Get arguments
    try:
        opts = getopt.getopt(argv, 'vc:t:l:', ['view', 'conf=', 'task=', 'log='])[0]
    except getopt.GetoptError:
        print('Invalid arguments.')
        sys.exit(255)
    for opt,arg in opts:
        if opt in ('-v', '--view'): # View mode (without deleting)
            view_mode = True
        elif opt in ('-c', '--conf'):
            conf_path = arg
        elif opt in ('-t', '--task'):
            task = arg
        elif opt in ('-l', '--log'):
            logger.Logger.log_path = arg

    # Logger
    lg = logger.Logger.register(__name__)

    # Run autoremove
    try:
        # Show version
        lg.info('Auto Remove Torrents %s' % __version__)
        # Load configurations
        lg.info('Loading configurations...')
        with open_(conf_path, 'r', encoding='utf-8') as stream:
            result = yaml.safe_load(stream)
        lg.info('Found %d task(s) in the file.' % len(result))

        # Run tasks
        if task == None: # Task name specified
            for task_name in result:
                try:
                    Task(task_name, result[task_name], not view_mode).execute()
                except Exception:
                    lg.error(traceback.format_exc().splitlines()[-1])
                    lg.error('Task %s fails. ' % task_name)
                    lg.debug('Exception Logged', exc_info=True)
        else:
            Task(task, result[task], not view_mode).execute()
    except Exception:
        lg.error(traceback.format_exc().splitlines()[-1])
        lg.debug('Exception Logged', exc_info=True)
        lg.critical('An error occured. If you think this is a bug or need help, you can submit an issue.')
Beispiel #6
0
    def runner():
        # Set root directory
        root_dir = os.path.join(os.path.realpath(os.path.dirname(__file__)))

        # Load mock URLs
        with open_(os.path.join(root_dir, 'mocks.json'), 'r',
                   encoding='utf-8') as f:
            mocks = json.load(f)
            for url in mocks:
                # Mock GET and POST requests
                requests_mock.get(url, **mocks[url])
                requests_mock.post(url, **mocks[url])
def test_status():
    cs = ClientStatus()
    with open_(os.path.join(os.path.realpath(os.path.dirname(__file__)),
                            'clientstatus.json'),
               encoding='utf-8') as f:
        data = json.load(f)

        cs.download_speed = data['download_speed']
        cs.total_downloaded = data['total_downloaded']
        cs.upload_speed = data['upload_speed']
        cs.total_uploaded = data['total_uploaded']
        cs.port_status = PortStatus[data['port_status']]
        cs.free_space = lambda _: data['free_space']

    return cs
Beispiel #8
0
def test_client(env_dist):
    # Init logger
    logger.Logger.init()
    lg = logger.Logger.register(__name__)

    # Mapping of exceptions
    exception_map = {
        ConnectionFailure: 'ConnectionFailure',
        LoginFailure: 'LoginFailure',
        NoSuchClient: 'NoSuchClient',
        NoSuchTorrent: 'NoSuchTorrent',
        RemoteFailure: 'RemoteFailure'
    }

    # Set basic directory
    basic_dir = os.path.join(os.path.realpath(os.path.dirname(__file__)),
                             'cases')
    lg.info('Basic directory: %s' % basic_dir)

    for file in os.listdir(basic_dir):
        file_path = os.path.join(basic_dir, file)

        if os.path.isfile(file_path):
            # Load file
            lg.info('Loading file: %s' % file)
            with open_(file_path, encoding='utf-8') as f:
                conf = yaml.safe_load(f)

            # Make take and run
            try:
                Task(file, conf['task'], True).execute()
                if 'exceptions' in conf and len(
                        conf['exceptions']) > 0:  # No exceptions was raised
                    raise AssertionError(
                        "It didn't raise exceptions as expected")
            except Exception as e:
                # Check if the excpetion is expected
                found = False
                for e in exception_map:
                    if sys.exc_info()[0] == e:
                        if 'exceptions' in conf and exception_map[e] in conf[
                                'exceptions']:
                            lg.info('An expected exception was raised: %s.' %
                                    exception_map[e])
                            found = True
                            break  # An expected exception
                if not found:
                    raise e  # Unexpected exception
#-*- coding:UTF-8 -*-

from setuptools import setup, find_packages
from autoremovetorrents.version import __version__
from autoremovetorrents.compatibility.disk_usage_ import SUPPORT_SHUTIL
from autoremovetorrents.compatibility.open_ import open_
from autoremovetorrents.compatibility.pyyaml_version_ import PYYAML_VERSION

setup(
    name='autoremove-torrents',
    version=__version__,
    description='Automatically remove torrents according to your strategies.',
    long_description=open_('README.rst', 'r', encoding='utf-8').read(),
    classifiers=[
        'Environment :: Console', 'License :: OSI Approved :: MIT License',
        'Programming Language :: Python', 'Topic :: Utilities'
    ],  # Get classifiers from https://pypi.org/pypi?%3Aaction=list_classifiers
    keywords='python autoremove torrent',
    author='jerrymakesjelly',
    author_email='*****@*****.**',
    url='https://github.com/jerrymakesjelly/autoremove-torrents',
    license='MIT',
    packages=find_packages(),
    include_package_data=True,
    zip_safe=True,
    install_requires=[
        'deluge-client',
        'enum34',
        'ply',
        '' if SUPPORT_SHUTIL else 'psutil',
        PYYAML_VERSION,
Beispiel #10
0
def test_env():
    with open_(os.path.join(os.path.realpath(os.path.dirname(__file__)),
                            'environment.json'),
               encoding='utf-8') as f:
        env = json.load(f)
    return env