Exemplo n.º 1
0
def add_header(file_descriptor, header):
    """
    Add the copyright / license message to a file.

    The copyright / license is placed below an optional shebang line as
    well as an optional coding line.
    It is preceded by an empty line if not at the beginning of the file
    and always followed by an empty line.
    """
    begin_index = scan_past_coding_and_shebang_lines(file_descriptor.content)
    end_index = scan_past_empty_lines(file_descriptor.content, begin_index)

    # inject copyright message
    comment = get_comment(file_descriptor.path, header)
    inserted_block = '%s\n\n' % comment
    if begin_index > 0:
        inserted_block = '\n' + inserted_block
    content = file_descriptor.content[:begin_index] + inserted_block + \
        file_descriptor.content[end_index:]

    # output beginning of file for debugging
    # index = end_index + len(inserted_block)
    # for _ in range(3):
    #     index = get_index_of_next_line(content, index)
    # print('<<<')
    # print(content[:index - 1])
    # print('>>>')

    with open(file_descriptor.path, 'w') as h:
        h.write(content)
Exemplo n.º 2
0
def add_header(file_descriptor, header):
    """
    Add the copyright / license message to a file.

    The copyright / license is placed below an optional shebang line as
    well as an optional coding line.
    It is preceded by an empty line if not at the beginning of the file
    and always followed by an empty line.
    """
    begin_index = scan_past_coding_and_shebang_lines(file_descriptor.content)
    end_index = scan_past_empty_lines(file_descriptor.content, begin_index)

    # inject copyright message
    comment = get_comment(file_descriptor.path, header)
    inserted_block = '%s\n\n' % comment
    if begin_index > 0:
        inserted_block = '\n' + inserted_block
    content = file_descriptor.content[:begin_index] + inserted_block + \
        file_descriptor.content[end_index:]

    # output beginning of file for debugging
    # index = end_index + len(inserted_block)
    # for _ in range(3):
    #     index = get_index_of_next_line(content, index)
    # print('<<<')
    # print(content[:index - 1])
    # print('>>>')

    with open(file_descriptor.path, 'w') as h:
        h.write(content)
Exemplo n.º 3
0
def add_copyright_year(file_descriptors, new_years, verbose):
    if verbose:
        print('Adding the current year to existing copyright notices:')
        print()

    for path in sorted(file_descriptors.keys()):
        file_descriptor = file_descriptors[path]

        # ignore files which do not have a header
        if not getattr(file_descriptor, 'copyright_identifier', None):
            continue

        index = scan_past_coding_and_shebang_lines(file_descriptor.content)
        index = scan_past_empty_lines(file_descriptor.content, index)

        if file_descriptor.filetype == SOURCE_FILETYPE:
            block, block_offset = get_comment_block(file_descriptor.content,
                                                    index)
            if not block:
                assert False, "Could not find comment block in file '%s'" % file_descriptor.path
        else:
            block = file_descriptor.content[index:]
            block_offset = 0
        copyright_span, years_span, name_span = search_copyright_information(
            block)
        if copyright_span is None:
            assert False, "Could not find copyright information in file '%s'" % \
                file_descriptor.path

        # skip if all new years are already included
        years = get_years_from_string(block[years_span[0]:years_span[1]])
        if all([(new_year in years) for new_year in new_years]):
            if verbose:
                print(' ', file_descriptor.path)
            continue
        print('*' if file_descriptor.exists else '+', file_descriptor.path)

        for new_year in new_years:
            years.add(new_year)
        years_string = get_string_from_years(years)

        # overwrite previous years with new years
        offset = index + block_offset
        global_years_span = [offset + years_span[0], offset + years_span[1]]
        content = file_descriptor.content[:global_years_span[0]] + years_string + \
            file_descriptor.content[global_years_span[1]:]

        # output beginning of file for debugging
        # index = global_years_span[0]
        # for _ in range(3):
        #     index = get_index_of_next_line(content, index)
        # print('<<<')
        # print(content[:index - 1])
        # print('>>>')

        with open(file_descriptor.path, 'w') as h:
            h.write(content)
Exemplo n.º 4
0
def add_copyright_year(file_descriptors, new_years, verbose):
    if verbose:
        print('Adding the current year to existing copyright notices:')
        print()

    for path in sorted(file_descriptors.keys()):
        file_descriptor = file_descriptors[path]

        # ignore files which do not have a header
        if not getattr(file_descriptor, 'copyright_identifier', None):
            continue

        index = scan_past_coding_and_shebang_lines(file_descriptor.content)
        index = scan_past_empty_lines(file_descriptor.content, index)

        if file_descriptor.filetype == SOURCE_FILETYPE:
            block, block_offset = get_comment_block(file_descriptor.content, index)
            if not block:
                assert False, "Could not find comment block in file '%s'" % file_descriptor.path
        else:
            block = file_descriptor.content[index:]
            block_offset = 0
        copyright_span, years_span, name_span = search_copyright_information(block)
        if copyright_span is None:
            assert False, "Could not find copyright information in file '%s'" % \
                file_descriptor.path

        # skip if all new years are already included
        years = get_years_from_string(block[years_span[0]:years_span[1]])
        if all([(new_year in years) for new_year in new_years]):
            if verbose:
                print(' ', file_descriptor.path)
            continue
        print('*' if file_descriptor.exists else '+', file_descriptor.path)

        for new_year in new_years:
            years.add(new_year)
        years_string = get_string_from_years(years)

        # overwrite previous years with new years
        offset = index + block_offset
        global_years_span = [offset + years_span[0], offset + years_span[1]]
        content = file_descriptor.content[:global_years_span[0]] + years_string + \
            file_descriptor.content[global_years_span[1]:]

        # output beginning of file for debugging
        # index = global_years_span[0]
        # for _ in range(3):
        #     index = get_index_of_next_line(content, index)
        # print('<<<')
        # print(content[:index - 1])
        # print('>>>')

        with open(file_descriptor.path, 'w') as h:
            h.write(content)