1import re
2import subprocess
3
4import checksymbolslib.br as br
5import checksymbolslib.kconfig as kconfig
6import checksymbolslib.makefile as makefile
7
8
9file_types = [
10    kconfig,
11    makefile,
12]
13
14
15def get_list_of_files_in_the_repo():
16    cmd = ['git', 'ls-files']
17    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
18    stdout = p.communicate()[0]
19    processed_output = [str(line.decode().rstrip()) for line in stdout.splitlines() if line]
20    return processed_output
21
22
23def get_list_of_files_to_process(all_files):
24    files_to_process = []
25    for f in all_files:
26        if br.file_belongs_to_an_ignored_diretory(f):
27            continue
28        for t in file_types:
29            if t.check_filename(f):
30                files_to_process.append(f)
31                break
32    return files_to_process
33
34
35def get_list_of_filenames_with_pattern(all_files, exclude_list, pattern):
36    re_pattern = re.compile(r'{}'.format(pattern))
37    matching_filenames = []
38    for filename in all_files:
39        if re_pattern.search(filename):
40            if filename not in exclude_list:
41                matching_filenames.append(filename)
42    return matching_filenames
43
44
45def read_file(filename):
46    file_content_raw = []
47    with open(filename, 'r', errors='surrogateescape') as f:
48        for lineno, text in enumerate(f.readlines()):
49            file_content_raw.append([lineno + 1, text])
50    return file_content_raw
51
52
53def cleanup_file_content(file_content_raw):
54    cleaned_up_content = []
55    continuation = False
56    last_line = None
57    first_lineno = None
58    for cur_lineno, cur_line in file_content_raw:
59        if continuation:
60            line = last_line + cur_line
61            lineno = first_lineno
62        else:
63            line = cur_line
64            lineno = cur_lineno
65        continuation = False
66        last_line = None
67        first_lineno = None
68        clean_line = line.rstrip('\n')
69        if clean_line.endswith('\\'):
70            continuation = True
71            last_line = clean_line.rstrip('\\')
72            first_lineno = lineno
73            continue
74        cleaned_up_content.append([lineno, clean_line])
75    return cleaned_up_content
76
77
78def populate_db_from_file(db, filename):
79    file_content_raw = read_file(filename)
80    file_content_to_process = cleanup_file_content(file_content_raw)
81    for t in file_types:
82        if t.check_filename(filename):
83            t.populate_db(db, filename, file_content_to_process)
84