aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/check_code_style_conversion.py
blob: 8f9ad7755318fb9e9a5b33adbafbdcd349320a85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# This is a script that helps checking the initial patches that converted
# libsamsung-ipc to the kernel code style.
#
# As there is a massive amount of files to convert, and that the number of
# patches is also big, this script can hopefully make testing and reviewing
# easier.
#
# Copyright (C) 2020 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

import os
import re
import sh
import sys

def usage(progname):
    print("Usage: {} <revision_range> [-f]".format(progname))
    print("       -f\tfull check: "
          "verify if all the repository files have been checked")
    sys.exit(1)

def run(*args):
    # print(*args)
    return sh.sh("-c", " ".join(args))

def git(*args):
    return sh.git("--no-pager", *args)

def git_log_oneline(revision_range, *args):
    return git("show",
               "--oneline",
               "-s",
               "--no-decorate",
               '--color=never',
               revision_range,
               *args).split(os.linesep)[:-1]

def git_get_diffed_files(commit1, commit2):
    return git("diff", "--name-only", commit1, commit2).split(os.linesep)[:-1]

def git_get_commit_list(revision_range):
    return git_log_oneline(revision_range, '--reverse', '--format=%h')

def get_revision_files(revision):
    return git("ls-tree", "--name-only", '-r', revision).split(os.linesep)[:-1]

def check_file(filepath):
    try:
        file_report = run("scripts" + os.sep + "checkpatch.pl",
                          "-f", filepath)
        print("  [  OK  ] {}".format(filepath))
    except:
        print("  [  !!  ] {}".format(filepath))

def checkpatch(revision_range, full_check=False):
    repository_files = []
    if full_check:
        last_commit = git_get_commit_list(revision_range)[-1]
        repository_files = get_revision_files(last_commit)

    for commit in git_get_commit_list(revision_range):
        print("Checking {}".format(git_log_oneline(commit)[0]))

        # Check the commit
        try:
            diff = run("scripts" + os.sep + "checkpatch.pl", "-g", commit)
            print("  [  OK  ] Commit")
        except:
            print("  [  !!  ] Commit")

        # Check the files of the commit
        modified_files = git_get_diffed_files(commit, commit + "~1")
        for modified_file in modified_files:
            check_file(modified_file)
            if full_check:
                if modified_file in repository_files:
                    repository_files.remove(modified_file)

    if full_check:
        if len(repository_files) > 0:
            print("Files not in {}:".format(revision_range))
            for path in repository_files:
                check_file(path)

if __name__ == '__main__':
    if len(sys.argv) == 2:
        revision_range = sys.argv[1]
        checkpatch(revision_range)
    elif len(sys.argv) == 3 and sys.argv[2] == "-f":
        revision_range = sys.argv[1]
        checkpatch(revision_range, full_check=True)
    else:
        usage(sys.argv[0])