예제 #1
0
def file_division(filename, max_size, zip_content):
    def content_division(file_content):
        if len(file_content) > max_size * 1024:
            result_parts = []
            parts_count = len(file_content) // (max_size * 1024)

            for p in range(parts_count):
                part_begin = max_size * 1024 * p
                part_end = max_size * 1024 * (p + 1) - 1
                result_parts.append(file_content[part_begin:part_end])

            if len(file_content) - 1 >= max_size * 1024 * parts_count:
                result_parts.append(file_content[max_size * 1024 *
                                                 parts_count:])

            return result_parts

    if not zip_content:
        if not pathlib.Path(filename).exists():
            raise exceptions.IncorrectFileNameError(filename)
        try:
            with open(filename, 'rb') as file:
                file_content = base64.b64encode(file.read())
        except (IOError, FileNotFoundError, FileExistsError) as exception:
            raise exceptions.FileError('{}: {}'.format(type(exception),
                                                       exception))
    else:
        file_content = zip_content

    return content_division(file_content) or [file_content]
 def inject_rtl_params_to_loop(self, file_dict: dict, omp_rtl_params: list):
     if not omp_rtl_params:
         return
     c_file_path = file_dict['file_full_path']
     e.assert_file_exist(c_file_path)
     with open(c_file_path, 'r') as input_file:
         c_code = input_file.read()
     e.assert_file_is_empty(c_code)
     for loop_id in range(1, self.files_loop_dict[file_dict['file_id_by_rel_path']][0] + 1):
         start_loop_marker = f'{Fragmentator.get_start_label()}{loop_id}'
         end_loop_marker = f'{Fragmentator.get_end_label()}{loop_id}'
         start_marker_pattern = rf'{start_loop_marker}[ \t]*\n'
         loop_pattern = rf'{start_marker_pattern}.*{end_loop_marker}[ \t]*\n'
         loop_before_changes = re.search(loop_pattern, c_code, re.DOTALL).group()
         loop_prefix_pattern = rf'{start_marker_pattern}.*?(?=[\t ]*for[ \t]*\()'
         loop_prefix = re.search(loop_prefix_pattern, loop_before_changes, re.DOTALL).group()
         loop_body_and_suffix = re.sub(re.escape(loop_prefix), '', loop_before_changes)
         params_code = f'{start_loop_marker}\n'
         for param in omp_rtl_params:
             omp_function_name = re.search(r'.+(?=\(.*\))', param).group()
             if omp_function_name in loop_prefix:
                 loop_prefix = re.sub(rf'{omp_function_name}[^;]*;[^\n]*\n', '', loop_prefix, re.DOTALL)
             params_code += f'{param}\n'
         loop_prefix = re.sub(rf'{start_marker_pattern}', params_code, loop_prefix)
         new_loop = f'{loop_prefix}{loop_body_and_suffix}'
         c_code = c_code.replace(loop_before_changes, new_loop)
     try:
         with open(c_file_path, 'w') as output_file:
             output_file.write(c_code)
     except OSError as err:
         raise e.FileError(str(err))
예제 #3
0
def mime_message_create(entered_data, files_group=None):
    recipients_addresses = ''
    if entered_data.open_sending:
        recipients_addresses = ', '.join(entered_data.recipients_list)

    message = (
        'MIME-Version: 1.0\r\n'
        'From:{}\r\n'
        'To:{}\r\n'
        'Subject:{}\r\n'
        'Content-Type: multipart/mixed; '
        'boundary=gc0p4Jq0M2Yt08jU534c0p\r\n\r\n'
        '--gc0p4Jq0M2Yt08jU534c0p\r\n'
        'Content-Type: text/plain\r\n'
        'Content-Transfer-Encoding: quoted-printable\n\n'
        '{}'.format(
            entered_data.sender_mail, recipients_addresses,
            entered_data.mail_subject, (quopri.encodestring(
                entered_data.message.encode())).decode('utf-8'))).encode()

    if not files_group:
        for attachment in entered_data.attachments_list:
            if not pathlib.Path(attachment).exists():
                raise exceptions.IncorrectFileNameError(attachment)
            try:
                with open(attachment, 'rb') as file:
                    filename = os.path.basename(pathlib.Path(attachment))
                    if entered_data.new_attach_name and not entered_data.zip:
                        filename = entered_data.new_attach_name
                    elif entered_data.zip:
                        filename = 'attachments.zip'
                    file_content = file.read() if not entered_data.zip else \
                        entered_data.zip_file_content
                    message += (
                        '--gc0p4Jq0M2Yt08jU534c0p\r\n'
                        'Content-Type: application/octet-stream;\r\n'
                        'Content-Disposition: attachment; filename="{}"\r\n'
                        'Content-Transfer-Encoding: base64\n\n'.format(
                            filename).encode() +
                        base64.b64encode(file_content) + b'\r\n')
            except IOError as exception:
                raise exceptions.FileError('{}: {}'.format(
                    type(exception), exception))
    else:
        for filename, file_content in files_group.items():
            if entered_data.new_attach_name and not entered_data.zip:
                filename = entered_data.new_attach_name
            elif entered_data.zip:
                filename = 'attachments.zip'
            message += (
                '--gc0p4Jq0M2Yt08jU534c0p\r\n'
                'Content-Type: application/octet-stream;\r\n'
                'Content-Disposition: attachment; filename="{}"\r\n'
                'Content-Transfer-Encoding: base64\n\n'.format(
                    os.path.basename(pathlib.Path(filename))).encode() +
                file_content + b'\r\n')
    message += b'.'

    return message
예제 #4
0
 def inject_line_in_code(file_full_path: str, new_line: str):
     with open(file_full_path, 'r') as input_file:
         c_code = input_file.read()
     e.assert_file_is_empty(c_code)
     c_code = new_line + c_code
     try:
         with open(file_full_path, 'w') as output_file:
             output_file.write(c_code)
     except OSError as err:
         raise e.FileError(str(err))
 def inject_c_code_to_loop(c_file_path: str, loop_id: str, c_code_to_inject: str):
     e.assert_file_exist(c_file_path)
     with open(c_file_path, 'r') as input_file:
         c_code = input_file.read()
     e.assert_file_is_empty(c_code)
     loop_id_with_inject_code = loop_id + '\n' + c_code_to_inject
     c_code = re.sub(loop_id + '[ ]*\n', loop_id_with_inject_code, c_code)
     try:
         with open(c_file_path, 'w') as output_file:
             output_file.write(c_code)
     except OSError as err:
         raise e.FileError(str(err))
예제 #6
0
 def remove_timer_code(absolute_file_paths_list: list):
     for c_file_dict in absolute_file_paths_list:
         try:
             with open(c_file_dict['file_full_path'], 'r') as f:
                 content = f.read()
             content = Timer.remove_declaration_code(content)
             content = Timer.remove_run_time_calculation_code_code(content)
             content = Timer.remove_writing_to_file_code(content)
             with open(c_file_dict['file_full_path'], 'w') as f:
                 f.write(content)
         except Exception as ex:
             raise e.FileError(
                 f'exception in Compar.remove_timer_code: {c_file_dict["file_full_path"]}: {str(ex)}'
             )
 def add_to_loop_details_about_comp_and_combination(file_path: str, start_label: str, combination_id: str,
                                                    comp_name: str):
     e.assert_file_exist(file_path)
     e.assert_file_is_empty(file_path)
     with open(file_path, 'r') as file:
         file_text = file.read()
     to_replace = ''
     to_replace += f'{start_label}\n{ComparConfig.COMBINATION_ID_C_COMMENT}{combination_id}\n'
     to_replace += f'{ComparConfig.COMPILER_NAME_C_COMMENT}{comp_name}\n'
     file_text = re.sub(f'{start_label}[ ]*\\n', to_replace, file_text)
     try:
         with open(file_path, 'w') as file:
             file.write(file_text)
     except OSError as err:
         raise e.FileError(str(err))
 def inject_directive_params_to_loop(self, file_dict: dict, omp_directive_params: list):
     if not omp_directive_params:
         return
     c_file_path = file_dict['file_full_path']
     e.assert_file_exist(c_file_path)
     format_c_code([c_file_path, ])  # mainly to arrange directives in a single line
     with open(c_file_path, 'r') as input_file:
         c_code = input_file.read()
     e.assert_file_is_empty(c_code)
     for loop_id in range(1, self.files_loop_dict[file_dict['file_id_by_rel_path']][0] + 1):
         start_loop_marker = f'{Fragmentator.get_start_label()}{loop_id}'
         end_loop_marker = f'{Fragmentator.get_end_label()}{loop_id}'
         start_marker_pattern = rf'{start_loop_marker}[ \t]*\n'
         loop_pattern = rf'{start_marker_pattern}.*{end_loop_marker}[ \t]*\n'
         loop_before_changes = re.search(loop_pattern, c_code, re.DOTALL).group()
         loop_prefix_pattern = rf'{start_marker_pattern}.*?(?=[\t ]*for[ \t]*\()'
         loop_prefix = re.search(loop_prefix_pattern, loop_before_changes, re.DOTALL).group()
         loop_body_and_suffix = re.sub(re.escape(loop_prefix), '', loop_before_changes)
         pragma_pattern = r'#pragma omp[^\n]+\n'
         pragma = re.search(rf'{pragma_pattern}', loop_prefix)
         if pragma:
             pragma = pragma.group().replace('\n', '')
             new_directives = ''
             for directive in omp_directive_params:
                 pragma_type, directive = directive.split('_', 1)
                 pragma_name = re.search(r'[^(]+', directive).group()
                 if not re.search(rf' {pragma_type} ?', pragma):
                     if pragma_type == CombinatorConfig.PARALLEL_DIRECTIVE_PREFIX:
                         pragma = re.sub(r'pragma omp', f'pragma omp {pragma_type}', pragma)
                     if pragma_type == CombinatorConfig.FOR_DIRECTIVE_PREFIX:
                         if re.search(rf' {CombinatorConfig.PARALLEL_DIRECTIVE_PREFIX} ?', pragma):
                             new_text = f'pragma omp {CombinatorConfig.PARALLEL_DIRECTIVE_PREFIX} {pragma_type} '
                             pragma = re.sub(rf'pragma omp {CombinatorConfig.PARALLEL_DIRECTIVE_PREFIX} ?',
                                             new_text,
                                             pragma)
                         else:
                             pragma = re.sub(r'pragma omp', f'pragma omp {pragma_type}', pragma)
                 if pragma_name in pragma:
                     pragma = re.sub(rf'{"pragma_name"}(?:\([^)]+\))? ?', '', pragma)
                 new_directives += f' {directive}'
             new_pragma = f'{pragma} {new_directives}\n'
             new_prefix = re.sub(rf'{pragma_pattern}', new_pragma, loop_prefix)
             c_code = c_code.replace(loop_before_changes, f'{new_prefix}{loop_body_and_suffix}')
     try:
         with open(c_file_path, 'w') as output_file:
             output_file.write(c_code)
     except OSError as err:
         raise e.FileError(str(err))
예제 #9
0
    def inject_timers(self, array_var_index: int, main_file_path: str):
        name_of_global_array = f'{self.COMPAR_VAR_PREFIX}arr{str(array_var_index)}'
        input_file_text, fragments = self.calculate_num_of_loops()

        if self.__input_file_path != main_file_path and self.get_number_of_loops(
        ) != 0:
            input_file_text = Timer.inject_global_declaration(
                input_file_text, self.__number_of_loops, name_of_global_array)

        if GlobalsConfig.OMP_HEADER not in input_file_text:
            input_file_text = f'{GlobalsConfig.IFDEF_OMP_HEADER}\n{input_file_text}'
        if GlobalsConfig.C_STDIO_HEADER not in input_file_text:
            input_file_text = f'{GlobalsConfig.C_STDIO_HEADER}\n{input_file_text}'
        for label, loop_fragment in enumerate(fragments, 1):
            prefix_code = self.get_prefix_loop_code(str(label))
            suffix_code = self.get_suffix_loop_code(str(label),
                                                    name_of_global_array)
            loop_with_c_code = loop_fragment['start_label'] + prefix_code
            loop_with_c_code += loop_fragment['loop']
            loop_with_c_code += suffix_code
            loop_with_c_code += loop_fragment[
                'end_label'] + '\n' + Timer.COMPAR_DUMMY_VAR + str(
                    label) + ';\n'

            if 'return' in loop_with_c_code:
                loop_with_c_code = loop_with_c_code.replace(
                    'return', suffix_code + '\nreturn')

            loop_to_replace = loop_fragment['start_label'] + '\n'
            loop_to_replace += loop_fragment['loop']
            loop_to_replace += '\n' + loop_fragment['end_label']
            input_file_text = input_file_text.replace(loop_to_replace,
                                                      loop_with_c_code)
        try:
            with open(self.__input_file_path, 'w') as output_file:
                output_file.write(input_file_text)
        except OSError as err:
            raise e.FileError(str(err))
예제 #10
0
def zip_archiving(entered_data):
    try:
        with tempfile.NamedTemporaryFile() as temp_file:
            temporary_name = '{}.zip'.format(os.path.basename(temp_file.name))

        with zipfile.ZipFile(temporary_name, 'w') as zip_archive:
            if entered_data.new_attach_name:
                if not pathlib.Path(entered_data.attachments_list[0]).exists():
                    raise exceptions.IncorrectFileNameError(
                        entered_data.attachments_list[0])
                folder_name = pathlib.Path(temporary_name[:-4])
                pathlib.Path.mkdir(folder_name)
                path_to_file = os.path.join(folder_name,
                                            entered_data.new_attach_name)
                shutil.copyfile(entered_data.attachments_list[0], path_to_file)

                zip_archive.write(path_to_file,
                                  arcname=entered_data.new_attach_name)

                if pathlib.Path(folder_name).exists():
                    shutil.rmtree(folder_name)
            else:
                for attachment in entered_data.attachments_list:
                    if not pathlib.Path(attachment).exists():
                        raise exceptions.IncorrectFileNameError(attachment)
                    zip_archive.write(
                        pathlib.Path(attachment),
                        os.path.basename(pathlib.Path(attachment)))

        with open(temporary_name, 'rb') as file:
            content = file.read()

        if pathlib.Path(temporary_name).exists():
            pathlib.Path(temporary_name).unlink()
    except (IOError, FileNotFoundError, FileExistsError) as exception:
        raise exceptions.FileError('{}: {}'.format(type(exception), exception))

    return content