Пример #1
0
def main(arguments):
    match_extension = allowed_extensions
    if arguments.source_extension and arguments.source_extension != '*':
        match_extension = arguments.source_extension.split(',')

    arc_manager = ArchiveHandler()

    for source_item in arguments.source:
        if os.path.isdir(source_item):
            match_files = get_files(source_item, match_extension)
        else:
            filename = os.path.split(source_item)[-1]
            source_dir = os.path.split(source_item)[0]
            match_files = {
                source_dir: {
                    'dir': source_item.split(os.path.sep)[-2],
                    'src': source_dir,
                    'files_list': [(filename, source_item)]
                }
            }
        for current_dir, dir_info in match_files.items():
            for (filename, filepath) in dir_info['files_list']:
                unpack_dir = arc_manager.unpack_archive(filepath)
                if arguments.naming_method == 'source_name':
                    target_filename = '.'.join(
                        filename.split('.')[0:-1] + [arguments.archive_type])
                else:
                    target_filename = generate_filename(
                        vars(arguments), arguments.archive_type, filename,
                        dir_info['dir'])
                if arguments.strip_accents:
                    target_filename = strip_accents(target_filename)
                target_destination = os.path.join(arguments.destination,
                                                  target_filename)
                unpacked_files = get_files(unpack_dir)
                for unpacked_source, files in unpacked_files.items():
                    arc_manager.pack_files(files['files_list'],
                                           target_destination)
Пример #2
0
def test_unpack_cbr(source_file: str, destination: Optional[dict],
                    lambda_validator: Any, file_list: Optional[list]):
    try:
        unpack_dir = unpack_cbr(source_file, destination)
    except Exception as e:
        assert lambda_validator(e)
    else:
        assert lambda_validator(unpack_dir)
        assert path.isdir(unpack_dir)
        unpacked_files = get_files(unpack_dir)
        unpacked_list = list(
            map(lambda x: x[0],
                unpacked_files.popitem()[1]['files_list']))
        unpacked_list = natsorted(unpacked_list, alg=ns.IGNORECASE)
        assert unpacked_list == file_list
        rmtree(unpack_dir)
def main(arguments):
    """
    Main function
    :param arguments: parsed CLI arguments
    :return: None
    """
    files_list = common.get_files(arguments.source,
                                  common.allowed_file_extensions)
    arc_manager = ArchiveHandler()

    for source, files in files_list.items():
        target_filename = common.generate_filename(vars(arguments),
                                                   arguments.archive_type,
                                                   source, files['dir'])
        if arguments.strip_accents:
            target_filename = common.strip_accents(target_filename)
        arc_manager.pack_files(files['files_list'], target_filename)
Пример #4
0
def assert_cbr(target_file: str, expected_list: List[Tuple[str, str]]):
    """
    Check created archive's content
    :param target_file: CBR archive
    :type target_file: str
    :param expected_list: files expected to be in the archive
    :type expected_list: list
    :return: None
    """
    assert run_command([RAR_BIN, 't', target_file]) == (0, '')
    assert run_command([RAR_BIN, 'e', target_file, OUTPUT_DIR]) == (0, '')
    extracted = list(
        x for x in map(lambda x: x[0],
                       get_files(OUTPUT_DIR)[OUTPUT_DIR]['files_list'])
        if x != path.split(target_file)[-1])

    assert extracted == expected_list
    for item in extracted:
        unlink(path.join(OUTPUT_DIR, item))
Пример #5
0
def test_get_files(test_dir: str, expected: dict):
    found_files = get_files(test_dir)
    assert expected == found_files