scripts/check_license_header.py

Tue, 08 Apr 2025 23:20:52 -0500

author
Gary Kramlich <grim@reaperworld.com>
date
Tue, 08 Apr 2025 23:20:52 -0500
changeset 43225
f0ffa584435b
parent 43088
cbd733874bd4
permissions
-rwxr-xr-x

IRCv3: Fix privmsg going to the status window before registration is complete

This got broken when some of this code was reorganized.

Testing Done:
Ran `ninja scan-build` and verified the error was gone. Also connected to libera and verified that I didn't get a conversation with the server's name anymore.

Reviewed at https://reviews.imfreedom.org/r/3963/

#!/usr/bin/env python3

# Purple - Internet Messaging Library
# Copyright (C) Pidgin Developers <devel@pidgin.im>
#
# Purple is the legal property of its developers, whose names are too numerous
# to list here. Please refer to the COPYRIGHT file distributed with this
# source distribution.
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this library; if not, see <https://www.gnu.org/licenses/>.

import difflib
import os
import sys


if len(sys.argv) < 4:
    print('usage: directory template-file file...')
    sys.exit(1)

path = sys.argv[1]

header = ""
with open(os.path.join(path, sys.argv[2]), 'r', encoding='utf-8') as f:
    header = f.read().splitlines(keepends=True)
if header[-1] != '\n':  # Always separate header from body with a blank line.
    header.append('\n')
header_lines = len(header)

error = False
debug = False

for filename in sys.argv[3:]:
    if filename.endswith('.o'):
        # On Windows, we sometimes have object files in sources, which we don't
        # want to try to read.
        continue
    file_data = None
    with open(os.path.join(path, filename), 'r', encoding='utf-8') as f:
        file_data = f.read().splitlines(keepends=True)

    # Look for a starting commenting ignoring newlines, if we find code, abort.
    comment_start = False
    comment_end = 0
    for i, line in enumerate(file_data):
        if line.startswith('/*'):
            comment_start = True
        elif comment_start and line.endswith('*/\n'):
            comment_end = i
            continue

        if line.strip() == '':
            continue

        if comment_start and comment_end != 0:
            header_end = i
            break

    if comment_start:
        new_data = header + file_data[header_end:]
    else:
        new_data = header + file_data

    if debug:
        sys.stdout.writelines(new_data[:30])
        print(f'comment_start: {comment_start}')
        print(f'comment_end: {comment_end}')
        print(f'header_end: {header_end}')
        sys.stdout.writelines(file_data[:25])

    diff = [*difflib.unified_diff(file_data, new_data,
                                  fromfile=f'a/{filename}',
                                  tofile=f'b/{filename}')]
    if diff:
        sys.stdout.writelines(diff)
        error = True

    if debug:
        sys.exit(1)

if error:
    sys.exit(1)

mercurial